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'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);
share|improve this question
add comment

2 Answers 2

up vote 4 down vote accepted

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);
share|improve this answer
    
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. –  Sun Sep 6 '12 at 20:23
    
No matter, I've solved it. I was using the original velocity value rather than the updated velocity of each particle.Thx. –  Sun Sep 6 '12 at 20:28
add comment

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)
share|improve this answer
add comment

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.