Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a 2D platformer. There are multiple particles that are just 1x1 colored pixels. Let's assume there are 256 particles laid out as a 16x16 cube. Say I popped an explosion right in the middle. All of these particles should fly out all over the place.

My question is how would I calculate the angle at which they have to be propelled toward and find what the initial velocity vector right after the blast has to be set to. I already have gravity and friction so that they would be naturally attracted to the ground and slow down after they have traveled a good distance along the X axis.

share|improve this question
    
generally, don't use trigonometry for such simple stuff, even if you think you need to use it, try to express the stuff you want to do as simple vector operations, it works 90% the time and is much faster to calculate –  Quonux Aug 22 '13 at 14:10

3 Answers 3

up vote 5 down vote accepted

You don't need to use any trigonometry for this. If you want the objects to be propelled straight away from the explosion, just give them a velocity proportional to the vector between the explosion point and the object's position.

Additionally, you'd probably want to make objects near the center fly faster, so one approach would be to find the vector from the explosion point to the object, calculate its length, normalize it, then scale it by some falloff function applied to the previous length.

share|improve this answer

Luckily this is really easy. Just take the case of one particle and one explosion.

Say the particle is at (5, 5) and the explosion is at (0, 0). Just by drawing this and looking at the picture, you can tell that the particle should move along the imaginary line between the origin and its location.

So what you need is a vector that points from the explosion to your particle. Here is how you calculate this vector:

Vector direction = particle.position - explosion.position

Then you should take your direction vector and normalize it (divide the components of the vector by the vector's length. After normalization the length of the vector is 1, and you can scale this vector however you like.

Once you have your scaled direction, just use it to directly move the particles position, thus treating the scaled vector as a velocity.

share|improve this answer

You should look into 'impulse force' since you are using physics...Impulse is calculated as force * time...or mass * change in velocity...if your particles are motionless before the explosion, an impulse from the explosion will give your particles dynamic motion( less linear )...

impulse link

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.