## 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).
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
It sounds like you basically want spatial hashing.

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.
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.