Jump to content
  • Advertisement
Sign in to follow this  
DarkFlame7

Vector-driven rotation (In pygame)

This topic is 3070 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!