Monday, April 2, 2012

Bubbles Mk 2

This is probably the 8th or 9th time I've rewritten the core of the scene graph. My girlfriend says its because I keep learning more and figuring out how to improve it, but I think it has more to do with my grandmother's old adage "If you've got a weak mind, you'd better have strong legs."

I've flipped back and forth between using a spatial partitioning method I've called bubbling, where space is carved up into sections which all have their own independent coordinate system. They divided space up into a three-dimensional grid, but only when objects were in their specific cell. In other words, the a bubble only existed when an object was in it. This was a pretty straightforward memory- and performance-saving optimization. 99.99999% of space is empty, so why should I bother keeping track of all that emptiness?

They last went away when I switched everything over to doubles for position vectors. However, trimesh collisions were getting a little wonky as distance from the origin increased, even with double precision. Once they got past a certain distance trimeshs stopped colliding all together.

So I spent a not-insignificant amount of time in the lab today bringing bubbles back.

Instead of creating a dynamically-spawning grid again, I anchored a static bubble at each planet, since I can pretty safely assume that's where most of the gameplay will happen. This gives each planet its own local coordinate system, which is helpful in a lot of ways. These static bubbles have a radius, and when nearest celestials are recomputed every 30 seconds each object is checked against this radius. If they have passed outside, they're considered in interplanetary space.

The next step in this is implementing interplanetary space. This will consist of a new bubble class, called DynamicBubble. Dynamic bubbles move around space as the average of the positions of all the objects they contain. If any objects get too far apart, the bubble splits in two. This should keep coordinate systems from getting too large and causing the wonky collision problem again.

I suspect that dynamic bubbles are going to be harder to implement than they sound. But they will be extremely useful.

No comments:

Post a Comment