Explosions, implosions and attractors

mreuvers
Posts: 69
Joined: Sat May 10, 2008 8:39 am

Explosions, implosions and attractors

Post by mreuvers »

Hi there,

Is there any standard way in Bullet to create explosions or inverse explosions: implosions? For example, stick a bomb in some kind of stack, detonate it and watch the stack explode. You get the point ;-)

Same goes for some kind of attraction mechanism (e.g., a magnet). Is any of this already built-in in Bullet? If not, any pointers on where to start with this would be greatly appreciated.

Thanks!

Cheers,

Martijn
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Explosions, implosions and attractors

Post by Erwin Coumans »

This effect has been applied in a few games, using any mechanism to gather closeby objects, and then applying impulses on the objects, based on distance.

There are many different mechanisms to gather overlapping pairs in a region, including adding a collision object without collision response, and using a contact callback, or using a custom pair cache (using registerPairCacheAndDispatcher) etc.

Which part do you want further information on? Gathering all objects that get influenced by the explosion/implosion, or calculating the actual impulse?

Thanks,
Erwin
mickey
Posts: 107
Joined: Fri Sep 19, 2008 6:08 pm

Re: Explosions, implosions and attractors

Post by mickey »

Hi!

I actually need to implement something similar, like a rocket missile hitting a ground and exploding thereby applying a force around all the object nearby to push/have them fly all over the place.

Forgive me to use this thread to ask a few questions of my own too.

Hi Erwin, you mentioned about gathering close by objects, if so, could you tell me how this is done? Is there a demo that I could look at that gathers close by objects and applies an impulse on them?

Havok has a sample demo called "fountain" wherein a phantom/invisible object applies a force to any objects that overlaps the phantom:

Code: Select all

//
//	apply a force on all objects
//
	for (int i = 0; i < m_phantom->getOverlappingCollidables().getSize(); i++ )
	{
		hkpCollidable* c = m_phantom->getOverlappingCollidables()[i];
		if ( c->getType() == hkpWorldObject::BROAD_PHASE_ENTITY )
		{
                     // Apply linear impulse on rigid bodies
                }
        }
Is that the equivalent of creating a big collision object (big enough to hit all the rigid bodies within a certain radius of the blast) in Bullet and have it not process any collision response as you suggested?

In any case I think the first one is a more an elegant solution, as it sounds strange to create a big collision object just to find the objects that overlapped the blast radius.

Answer to the other question would be nice too: 2. calculating the actual impulse?

Thanks in advance!

Cheers,

David
mreuvers
Posts: 69
Joined: Sat May 10, 2008 8:39 am

Re: Explosions, implosions and attractors

Post by mreuvers »

Hi there,
Erwin Coumans wrote:Which part do you want further information on? Gathering all objects that get influenced by the explosion/implosion, or calculating the actual impulse?
I'd like to receive information on both if possible. I like the 'adding a ghost object with no response' method to determine the closeby objects. However I'm not particularly fond of the contact callbacks, so if there is another way to determine that, please let me know.

Also I'd like to know how the impulse calculation based on distance works. Exactly how is that distance measured? I mean, you can't just compare the center of the object with the center of the explosion, simply because not all objects are spheres.

On a related note: perhaps it's an idea to add an explosion demo to the repository? I'm sure that a lot of people out there would be interested in that.

Thanks in advance!

Cheers,


Martijn
mreuvers
Posts: 69
Joined: Sat May 10, 2008 8:39 am

Re: Explosions, implosions and attractors

Post by mreuvers »

Never mind, I kinda figured it out myself by adding a kinematic sphere with no contact response and by using the internal tick callback and contact manifolds. It's all in a hackery-state, but I'll post something workable once it is less hackery ;-)

Cheers
sparkprime
Posts: 508
Joined: Fri May 30, 2008 2:51 am
Location: Ossining, New York
Contact:

Re: Explosions, implosions and attractors

Post by sparkprime »

Are you using the impulses caused by the "collision" of the explosion sphere, or just getting a list of things that intersect the sphere and applying some uniform impulse to them?

Using a collision callback seems to work but I don't like having to wait until the next step to get the collision. What might be useful is a generalisation of sweep to allow the sweeping of compound shapes, and also the generalisation of ray/sweep tests to allow 0 movement, i.e. a volume/point test. This would allow immediate answers. I would like this for telefrag detection as well, for instance.
mreuvers
Posts: 69
Joined: Sat May 10, 2008 8:39 am

Re: Explosions, implosions and attractors

Post by mreuvers »

For all interested I posted a movie containing a number of explosion/attractor examples on YouTube:

http://nl.youtube.com/watch?v=pt_n_V_Yl9Y

We use a kinematic object, with no contact response to detect the collision. In the internal tick callback we check whether one of the affected bodies is the kinematic object. If it is, we apply an impulse to it based on the distance to the center. In case of the explosions the impulse is applied to the object relative to the contactpoints (using ApplyImpulse). That didn't work very well for the attractors (instable), so we applied the impulse to the center of the object (using ApplyCentralImpulse).

Enjoy the movie ;-)

Cheers,


Martijn
sparkprime
Posts: 508
Joined: Fri May 30, 2008 2:51 am
Location: Ossining, New York
Contact:

Re: Explosions, implosions and attractors

Post by sparkprime »

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.
sparkprime
Posts: 508
Joined: Fri May 30, 2008 2:51 am
Location: Ossining, New York
Contact:

Re: Explosions, implosions and attractors

Post by sparkprime »

Ah you did actually use the contacts generated. That would be hard to do with a simple query.
mreuvers
Posts: 69
Joined: Sat May 10, 2008 8:39 am

Re: Explosions, implosions and attractors

Post by mreuvers »

Yep, I used the generated contacts. So in effect I'm a 'frame behind', but for our tests that isn't that much of a problem.
sparkprime
Posts: 508
Joined: Fri May 30, 2008 2:51 am
Location: Ossining, New York
Contact:

Re: Explosions, implosions and attractors

Post by sparkprime »

Yeah, 1 frame shouldn't matter for something like this. I was more worried about programmability.
mickey
Posts: 107
Joined: Fri Sep 19, 2008 6:08 pm

Re: Explosions, implosions and attractors

Post by mickey »

Hi Martijn

Would you share or have the code downloaded somewhere of your youtube explosion and attractors demo? The attractors for instance, is a pretty nice trick, wouldn't know how that would be implemented in Bullet. I would really love to see how you coded the explosion.

Thanks in advance.
sparkprime
Posts: 508
Joined: Fri May 30, 2008 2:51 am
Location: Ossining, New York
Contact:

Re: Explosions, implosions and attractors

Post by sparkprime »

He basically explained how to do it but a Bullet demo would probably be welcomed by all, I imagine :)
mickey
Posts: 107
Joined: Fri Sep 19, 2008 6:08 pm

Re: Explosions, implosions and attractors

Post by mickey »

yes, sample code is the best tutorial. wouldn't know how the attractors were implemented and would be really nice to see how the explosions were implemented in code.

thanks in advance.
sparkprime
Posts: 508
Joined: Fri May 30, 2008 2:51 am
Location: Ossining, New York
Contact:

Re: Explosions, implosions and attractors

Post by sparkprime »

Something has occured to me, there's nothing in the "collision sphere + contact points" approach for testing whether a wall gets in the way of the explosion, right? How could this be achieved? I'm thinking shooting a ray to each of the contact points from the centre of the explosion would work reasonably well for culling impulses on obstructed contacts, but there is a pathological case where the contact is the only obstructed part of the victim object.
Post Reply