• Advertisement
Sign in to follow this  

anttempting to rotate a velocity to turn the direction a sprite moves

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

so im working on a particle system for my game and want to add modifiers, issues is some of these modifiers require the particle to turn until it is facing a certain direction. so i came up with the idea of multiplying the velocity (vector2) by a rotation matrix, and i found that the rotation is too broad, my particles were all going off screen and then rotation back and if i increased the rotation angle then they moved too fast. I want it to rotate tightly but maintain the current speed. any idea on how to do this?

Edit: Sorry this is in xna btw

Share this post


Link to post
Share on other sites
Advertisement
Your particle's velocity is a two-component vector so I assume you're talking about 2D then? If so, why don't you just calculate the dot product between your particle's direction (its velocity) and the target direction? Then multiply the velocity with the result of the dot product so that the larger the current and the target direction are apart from each other, the slower it moves. Should work! :)

In case you want to read up on dot products: http://freespace.virgin.net/hugo.elias/routines/r_dot.htm

Share this post


Link to post
Share on other sites
okay so i tried you suggestion in multiple ways and none worked.

I tried: (the position I want them all to move to is 1,1)



Vector2 goal = new Vector2(1,1);
float dot = Vector2.Dot(Velocity,goal);
Velocity = Vector2.Add(Velocity, new Vector2(dot, dot));



with that i get numbers > 200 for the dot value



Vector2 goal = new Vector2(image.position.X-1,image.position.Y-1);
float dot = Vector2.Dot(Velocity,goal);
Velocity = Vector2.Add(Velocity, new Vector2(dot, dot));



that one sets dot to "infinity" or so the debugger says

I also tried both of these with multiplication, i dont get it...

[Edit]

I did get some results like this:
Vector2 goal = new Vector2(1,1)-image.position;
goal.Normalize();
Vector2 t = image.position;
t.Normalize();
double angle = MathHelper.ToDegrees((float)Math.Acos(Vector2.Dot(t,goal)));
timer = 10000;
Velocity = Vector2.Transform(Velocity,Matrix.CreateRotationZ(MathHelper.ToRadians((float)-angle/125)));



now they seem to be rotation to the position i want, but they dont stop, they just keep going in circles. Edited by CubedGames

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement