Rotating Images towards the mouse (pygame)
Author |
Message |
BBil
|
Posted: Thu Nov 11, 2010 5:01 pm Post subject: Rotating Images towards the mouse (pygame) |
|
|
So i have run into a problem with rotating images using the mouse. I am making a game and the idea is to have the arms rotate so that they will point directly at the mouse at all times. What is the best way of doing this? It would be best if the solution used pygame because that is what I have been using for the project. I have tested out pygame.transform.rotate but the picture does not stay in its x,y coordinates when it rotates, and the image becomes extremely distorted. Any help would be very appreciated!
![Canada Canada](images/smiles/canada.gif) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
BBil
|
Posted: Fri Nov 19, 2010 11:27 pm Post subject: RE:Rotating Images towards the mouse (pygame) |
|
|
oh great internet menz and womenz provide me with your wisdom!!! |
|
|
|
|
![](images/spacer.gif) |
kctan
|
Posted: Tue Nov 30, 2010 12:52 am Post subject: Re: Rotating Images towards the mouse (pygame) |
|
|
Post your code? |
|
|
|
|
![](images/spacer.gif) |
paragonz
|
Posted: Fri Dec 24, 2010 2:24 am Post subject: Re: Rotating Images towards the mouse (pygame) |
|
|
ummm not the best way to do it, but it is an easy fix. make sure that your arm is centered in the picture, it is easier to rotate pictures from the center ;
mx, my = mouse.get_pos()
ang = atan2((my-300),-(mx-400)) # (300,400) is the location where the picture is centered
rPic = transform.rotate(pic,degrees(ang))
x = 400 - rPic.get_width()/2
y = 300 - rPic.get_height()/2
# this gets the dimensions of the new rect that is created when you transform a picture
# you then find the middle of the new rect and subtract it from the center of your picture
screen.blit(rotPic,(x,y)) # you blit the centered image at pos x & y
if your original arm is centered and pointing to the left , this will move it in the correct direction
if it is pointing the other way, change the and to be "ang = atan2(-(my-300),(mx-400))"
btw sorry that i dont know how to put it in code mode, im new to the forums |
|
|
|
|
![](images/spacer.gif) |
|
|