# exploding pieces

This topic is 3995 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone i have a enemy which i want to make explode when he dies, i have split him into two parts, and all ready have the explosion working, now i just need to figure out the math to make one of the pieces fly away on a smooth curve. At the moment i have it so the piece rotates about its Z axis and moves along its Y axis, this does look ok, and i get the right general idea with the piece moving, however it looks a bit pap, there must be a cheap and chearful way of modeling this behaviour?

##### Share on other sites
Do you have a physics system? One way would be to associate the detached part with a rigid body, and just start that with some initial velocity and let the physics system take care of it.

##### Share on other sites
Cheap and cheerful? If your appliation isn't large enough to justify using a full blown physics engine, you can whip it up using a bit of vector maths:

Try this:

step 1) Calculate the vector D between the middle of each piece and the centre of the explosion.

step 2) Create velocity V where V=(force_of_explosion / D.length).

step 2a) Your formula may vary, the point is that there is SOME relation between distance from explosion and force applied to object. Try (force / D^2) as an alternative.

step 5) Normalise D to create the direction Dn that each piece should travel in.

step 6) On each frame, add vector (Dn * V) to the position of the object. the object will move at velocity V along vector Dn away from the epicentre of the explosion. This assumes the explosion is spherical and the pressure is evenly distributed.

For dramatic effect why not:
7) Create a few particles and apply the same principle?
8) Make the object rotate by a random amount in two axis rather than 1? (for 3d simulations this looks as if they are rotating in 3 axis, without the hassle)
9) Apply wind resistance: multiply V by 0.9 every second. When V is sufficiently small, stop the object from moving.

For a 2d system this kind of thing is all you need. If you want to make a 3d simulation, try a physics engine. I'm currently using ODE.

##### Share on other sites
Quote:
 Original post by speciesUnknownCheap and cheerful? If your appliation isn't large enough to justify using a full blown physics engine, you can whip it up using a bit of vector maths:Try this:step 1) Calculate the vector D between the middle of each piece and the centre of the explosion.step 2) Create velocity V where V=(force_of_explosion / D.length). step 2a) Your formula may vary, the point is that there is SOME relation between distance from explosion and force applied to object. Try (force / D^2) as an alternative.step 5) Normalise D to create the direction Dn that each piece should travel in.step 6) On each frame, add vector (Dn * V) to the position of the object. the object will move at velocity V along vector Dn away from the epicentre of the explosion. This assumes the explosion is spherical and the pressure is evenly distributed. For dramatic effect why not:7) Create a few particles and apply the same principle?8) Make the object rotate by a random amount in two axis rather than 1? (for 3d simulations this looks as if they are rotating in 3 axis, without the hassle)9) Apply wind resistance: multiply V by 0.9 every second. When V is sufficiently small, stop the object from moving.For a 2d system this kind of thing is all you need. If you want to make a 3d simulation, try a physics engine. I'm currently using ODE.

This works very well for what i need, i just need to figure out a way to incorperate gravity into the formula i need the tureet to move towards the ground, i've got the turret flying up using this formula but it never returns :) lol i'll see what i can do suggestions are greatly appreciated.

Im not using a physics engine since its a small project i am just learning :)

##### Share on other sites

Gravity is an acceleration of two objects towards each other. We can ignore the second object (the earth) because its mass is so huge, and just apply a force to one object (the one in question). This is an adequate video game approximation of gravity.

The method I use for this is simple.
step 0) So you have your object, with a normalised movement direction Dn. You also have the velocity V that the object moves at.

step 1) Take the direction vector Dn and multiply it by V to get the vector D back.

step 2) Take the vector G, representing gravity, divide it by the number of frames per second, and add this to the result. A good example is G=(0.0, -9.81, 0.0)/fps (earth) or G=(0.0, -1.635, 0.0)/fps (the moon, IIRC, has 1/6 earths gravity)
---Note that the number of frames per second is important to include in the gravity vector, adding it later is far harder since the end product of the entire calculation is also divided by the framerate.
--Note also that you can add together a series of environmental forces, such as wind, to this vector before continuing.

step 3) Calculate the length of the result and store it in V, and renormalise the vector and store it in Dn. Then continue the algorithm as per my last post.

n.b. I quickly get into a mess if I try to add extra variables to these calculations. So, if for example you want to divide V by the number of frames per second, therefore ensuring that V represents units per second, do this shortly before moving the object, rather than as part of the calculations up to that point. e.g.,

position = position + (Dn * V);

If you want a fixed framerate, use this in the final calculation:

position = position + (Dn * V * timestep);

If you want to have a speed control on the game, use this:

position = position + (Dn * V * timestep * speed_factor);

and so on. Try to integrate those into the previous calculations - pretty difficult.

[Edited by - speciesUnknown on February 7, 2008 6:20:26 PM]

##### Share on other sites
I welcome the correction of any mistakes I have made here, since I didn't pay attention in physics class and I flunked maths. This is a system I have used, and it works.

##### Share on other sites
You'r method works wonders for the gravity although i did have too tweak some values :)

The only problem i'm having now is i need to move a character between three or more waypoints, which i can do, although there is an instant turn involved, i'd like the characters to turn gradually whilst moving until they face the desired direction, i have done this by simply rotating the Y axis to make the character face the point whilst moving, this has one draw back though, the character could end up spiralling around a waypoint forever, is there any other alternatives to my method?

##### Share on other sites
Quote:
 Original post by LordFalloutYou'r method works wonders for the gravity although i did have too tweak some values :)

Would you mind telling me what tweaks you made and posting a bit of code, since I did go to all that effort typing it out? It may help me touch up on my method. (I frequently recommend it to my friends at Uni when they ask similar questions)
Quote:
 The only problem i'm having now is i need to move a character between three or more waypoints, which i can do, although there is an instant turn involved, i'd like the characters to turn gradually whilst moving until they face the desired direction, i have done this by simply rotating the Y axis to make the character face the point whilst moving, this has one draw back though, the character could end up spiralling around a waypoint forever, is there any other alternatives to my method?

That might belong in a separate thread, in the AI forum? Anyway, do you mean the AI navigation nodes? If you distinguish between primary and secondary nodes, you can have the character search via primary nodes, and travel along secondary nodes, and have the secondary nodes use a higher number of lines around corners, enough so it looks smooth. You can also provide more information on characters behaviour as they come to a corner (such as a node where they consider peering round the corner before going to the next node, rather than running blindly round and getting shot)

I've not done any AI navigation yet, but there are some pretty experienced people in the Artificial Intelligence forum.

##### Share on other sites
No problem, im away from my computer for a little while but as soon as i get back, i'll post my tweaks, although there mainly tweaks to fit your method around my code. It should be later tomorrow peel your eyes ? :)

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 9
• 13
• 9
• 9
• 15
• ### Forum Statistics

• Total Topics
634076
• Total Posts
3015353
×