4
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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 \$\endgroup\$
    – Quonux
    Commented Aug 22, 2013 at 14:10

3 Answers 3

5
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .