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

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

## 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 on other sites
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 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...

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

1. 1
2. 2
frob
15
3. 3
4. 4
5. 5
Rutin
12

• 12
• 12
• 58
• 14
• 15
• ### Forum Statistics

• Total Topics
632115
• Total Posts
3004198

×