# Vector-driven rotation (In pygame)

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

## Recommended Posts

I want to make a sprite that rotates to face the mouse in pygame. I've been trying to figure this out for months but I can't seem to grasp it... I can get Vectors to move sprites well enough for now, but I simply cannot rotate them based on vectors. Basically, the approach (In psuedocode) I was using was somewhat like the following:
lastvec = vector
vector = Vector.from_points(sprite.pos, pygame.mouse.get_pos())
roto = (lastvec.magnitude**2 + vector.magnitude**2 - distance-between-vectors)
roto = math.cos(roto)
pygame.transform.rotate(sprite)

Based on the equation: cos(angle) = (a**2 + b**2 - c**2) Obviously, this failed. I'm really out of ideas and I want help. I even created a whole program that draws a vector and its components in rather detailed views hoping I could help myself understand it, but the one entry I couldn't fill was the angle.

##### Share on other sites
I didn't quite follow your example, but for computing the angle that will orient one object towards another in 2-d, try atan2(), e.g.:
angle = atan2(mouse.y - sprite.pos.y, mouse.x - sprite.pos.x);

1. 1
Rutin
47
2. 2
3. 3
4. 4
5. 5

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632992
• Total Posts
3009756
• ### Who's Online (See full list)

There are no registered users currently online

×