4
\$\begingroup\$

I'm creating a simple particle explosion but am stuck in finding the correct angle to rotate my particle. The effect I'm going for is similar to this:

enter image description here

Where each particle is going outwards from the point of origin and at the correct angle. This is what I currently have:

enter image description here

As you can see, each particle is facing the same angle, but I'm having a little difficulty figuring out the correct angle. I have the vector for the point of emission and the new vector for each particle, how can I use this to calculate the angle?

Some code for reference:

 private Particle CreateParticle()
        {
...

            Vector2 velocity = new Vector2(2.0f * (float)(random.NextDouble() * 2 - 1), 2.0f * (float)(random.NextDouble() * 2 - 1));

            direction = velocity - ParticleLocation;

            float angle = (float)Math.Atan2(direction.Y, direction.X);

...

            return new Particle(texture, position, velocity, angle, angularVelocity, color, size, ttl, EmitterLocation);
        }

I am then using the angle created as so in my particles Draw method:

spriteBatch.Draw(Texture, Position, null, Color, Angle, origin, Size, SpriteEffects.None, 0f);
\$\endgroup\$

2 Answers 2

4
\$\begingroup\$

First figure out the direction based on where the particle is in reference to where it came from (the explosion). Then you take the arc-tangent of that to get the angle.

Vector2 direction = particlePosition - explosionPosition;
float angle = Math.Atan2(direction.Y, direction.X);
\$\endgroup\$
2
  • \$\begingroup\$ This is was I had originally done, but it hasn't made any difference. The original sprite image is a small image with the particle drawn horizontally. Check my edits for code. \$\endgroup\$
    – Sun
    Commented Sep 6, 2012 at 20:23
  • \$\begingroup\$ No matter, I've solved it. I was using the original velocity value rather than the updated velocity of each particle.Thx. \$\endgroup\$
    – Sun
    Commented Sep 6, 2012 at 20:28
3
\$\begingroup\$

Since your particles have a velocity you must have a velocity vector. Let's call the components vxand vy. You can then get the angle using:

atan2(vy, vx)
\$\endgroup\$

You must log in to answer this question.

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