Sign in to follow this  

Vector-driven rotation (In pygame)

This topic is 2855 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

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 this post


Link to post
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);

Share this post


Link to post
Share on other sites

This topic is 2855 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this