# Vector-driven rotation (In pygame)

## 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);

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627701
• Total Posts
2978709

• 21
• 14
• 12
• 10
• 12