I'm working on an open source server side game engine and am tackling how to track game objects in 3d space.

The basic idea I am working with is that the server will track moving objects and record their location. I was thinking about dividing up the space into cubes of equal size, and doing enough math to figure out what cube an object is in whenever I record their current location. With something like this 'objects near me' becomes an efficient query, as I just get the objects in your cube plus adjacent cubes.

But having spent most of my career working with server side code, 3d math isn't something I've ever really had to deal with, so I'm starting from scratch as to what the best algorithm would be, and what libraries would help me the most. I'm using jruby, so libraries in java or C would work, although I'd prefer java.

I was also toying with the idea of using a 2d grid and a 3d space made of cubes. If I know the location of an object on the 2d grid, I'm thinking I can do some quick math to get objects that are within X number of 3d cubes away by just looking at the z axis of the objects. So if I have a player in a known cube, and I want to get all other players in that cube and all ajdacent cubes, I could just compare the z axis of all the objects I got from the 2d grid calculation. It wouldn't be an exact calculation, In order to get all players within range, I'd end up with some players outside of the range I really want if I use a number large enough to get all players within range.

But maybe I'm overthinking this, and the math isn't really that expensive to 'do it right'?

Chris