Generating an Index from a 3d Vector

Member
Posts: 227
Joined: 2008.08
Post: #1
For one of my map formats I must index a 3d array of cubes.
The maps are large enough that normal multi-layered pointer arrays won't work (Far too much memory, rather than w*h*d*sizeof(char) it would be w*sizeof(char**)+w*h*sizeof(char*)+w*h*d*sizeof(char) , Not pretty).
So I need a function that will access an 1d array given a 3d Vector, like so:
Code:
unsigned char* validCube=calloc(1,w*h*d);
//later on.....
if(validCube[Index3d(x,y,z)])  //Yada Yada

I've did a smaller scale version (2d) with a grid and my formula was :
Code:
index=x+y*w
And this worked perfectly.

When I tried to scale it up to 3d, I solved to:
Code:
index=(x+y*w)*(z+1)

But this causes collisions (i.e. like x*y*z does).
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
It sounds like you basically want spatial hashing.

http://www.beosil.com/download/Collision..._VMV03.pdf

That paper has just such the function that you were looking for.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 227
Joined: 2008.08
Post: #3
Figured it out:
Code:
index=x+y*w+z*w*h
Thus:
Code:
z=0
x+y*w = x+y*w+z*w*h
So it all agrees.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  SQLite3 question: accessing by column instead of index? Toontingy 3 4,670 Apr 8, 2010 06:19 PM
Last Post: OneSadCookie
  Generating Corner points of Rectangular Box using center point and forward vector 3DPat 6 5,852 Nov 13, 2006 09:40 PM
Last Post: 3DPat