Jump to content

View more

Image of the Day

Inventory ! Va falloir trouver une autre couleur pour le cadre D: #AzTroScreenshot #screenshotsaturday https://t.co/PvxhGL7cOH
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

[Python / pyGame] Rotate image towards mouse pointer

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
3 replies to this topic

#1 viper34j   Members   

122
Like
0Likes
Like

Posted 08 October 2010 - 09:17 AM

Oh great internetmenz, I need your help. My decade old math skills are failing me.

I'm attempting to rotate an image towards a point using pygame. The point is the mouse cursor in this case. Here is an example of my issue (this is only a snippet):


src_image, rect = pygame.image.load('c:\\testimage.png')
screen = pygame.display.get_surface()

# Example values
player_angle = 0
player_position = (100,100)

# Get the mouse point (Something between (0,0) and (1024,800)).
mouse_point = pygame.mouse.get_pos()

# Set the old_center for repositioning after the rotate.
old_center = self.rect.center

# *** Heres my problem, how do I determine what the angle is from the player_angle
# to the mouse_point with only these three peices of information.
angle_to_mouse = ??

image = pygame.transform.rotate(self.src_image, angle_to_mouse)
rect = image.get_rect()
rect.center = old_center




How do I determine the angle from the center point of the player_point to the mouse_point?

Thanks!

#2 unbird   Members   

8307
Like
0Likes
Like

Posted 08 October 2010 - 09:52 AM

Never used pygame, but what you are looking for is calculating an angle given a difference vector, in your case player_position - mouse_point. This will give you a (relative) x and y which you feed to atan2. Search the python math lib for this function, I bet it exists. This will give you the angle in radians which you can then feed to the rotate function (angle_to_mouse). Don't know what coordinate system you are using (left/right-handed), so probably you will have to negate y prior to the atan2 call (or negate angle_to_mouse).

#3 Esys   Members   

156
Like
0Likes
Like

Posted 08 October 2010 - 10:08 AM


angle_to_pointer = math.degrees(math.atan2(objectY-mouseY, objectX-mouseX))+180



#4 viper34j   Members   

122
Like
0Likes
Like

Posted 08 October 2010 - 02:22 PM

Thanks guys, that helped out a lot.




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.