The effect is very nice, especially the gravity wells. I think a fun game could probably be made based around just that concept
It made me think of other applications too -- wind tunnels (cylinders/boxes), jump tiles/speedups (boxes), decompressing airlocks (fill the space with convex hulls), underwater currents, the exhaust from spaceship thrusters, etc.
I take it the collision spheres you use there are very large, and the impulse applied (even for explosions)
I do wonder if this is the right method though. The most important questions are "Does it scale?" and "Is it annoying to have to break the flow of code between two simulation steps?"
The flow of code is currently divided between "I want an
explosion here" and "I want to apply
this impulse to a nearby object". It would be nice if we could just write code like this pseudocode:
Code: Select all
for (Body b in physics->intersect(x,y,z,btSphere(r))) {
btScalar dist = b->distanceFrom(x,y,z);
btVector3 dir = b->directionFrom(x,y,z);
b->applyImpulse(magnitude/(dist*dist) * dir);
}
Alternatively, it could be like this:
Code: Select all
for (Body b in physics->allBodies()) {
if (!b->intersects(x,y,z,btSphere(r))) continue;
btScalar dist = b->distanceFrom(x,y,z);
btVector3 dir = b->directionFrom(x,y,z);
b->applyImpulse(magnitude/(dist*dist) * dir);
}
In both cases we're talking about an instantaneous query that would ideally make use of the broadphase and midphase algorithms to be nice and fast, and immediately return bodies for client-side processing. There will naturally be other things happening during explosions -- non-physical things, like stuff catching fire, fires blown out, people dying, things turning black or whatever.
It may be best if there is a separate method for explosions (which add an instantaneous impulse) and things like gravity wells and wind tunnels which act like a continuous force. The latter is probably better done with an internal step callback of some kind, as then you can create the gravity well collision object (maybe even subclass the collision object?) and leave it there, affecting other bodies in the scene. You could even constrain it to e.g. be always behind a ship.
Alternatively it is also possible to write something like the above code that attracts objects and just execute that every internal step in the right place. This allows for easy pulsing of the parameters or whatever. I think it depends on the exact situation.