Generating an Index from a 3d Vector
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:
I've did a smaller scale version (2d) with a grid and my formula was :
And this worked perfectly.
When I tried to scale it up to 3d, I solved to:
But this causes collisions (i.e. like x*y*z does).
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 YadaI've did a smaller scale version (2d) with a grid and my formula was :
Code:
index=x+y*wWhen 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).
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.
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.
Figured it out:
Thus:
So it all agrees.
Code:
index=x+y*w+z*w*hCode:
z=0
x+y*w = x+y*w+z*w*h
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| SQLite3 question: accessing by column instead of index? | Toontingy | 3 | 3,246 |
Apr 8, 2010 06:19 PM Last Post: OneSadCookie |
|
| Generating Corner points of Rectangular Box using center point and forward vector | 3DPat | 6 | 4,889 |
Nov 13, 2006 09:40 PM Last Post: 3DPat |
|

