Jump to content
Posted 13 March 2013 - 01:04 PM
Posted 13 March 2013 - 01:26 PM
I don't know pygame specifics, but you'll have to rotate your player's image to face the mouse, so I assume there's some method to do this on sprites in pygame.
In a top-down action shooter I made in C++, I have this code for setting the player's body towards the mouse:
// Finally, find location of Mouse and point player towards it cpFloat MouseX = (cpFloat)Input.GetMouseX(); cpFloat MouseY = (cpFloat)Input.GetMouseY(); cpFloat Angle = atan2(MouseY - (cpFloat)(mpApp->GetHeight()/2), MouseX - (cpFloat)(mpApp->GetWidth()/2)); cpBodySetAngle(mpBody, Angle);
The crux of it comes from the atan2() call, which is arc tangent. There should be a python function that performs it, and the parameters given to it are the Y and X distances between the mouse and the player.
You can take a look at the whole blog post here to see how I also include firing the bullets (and rotating the bullets to be facing the correct way).
Posted 13 March 2013 - 01:28 PM
If this post or signature was helpful and/or constructive please give rep.
// C++ Video tutorials
// Easy to learn 2D Game Library c++
SFML2.2 Download http://www.sfml-dev.org/download.php
SFML2.2 Tutorials http://www.sfml-dev.org/tutorials/2.2/
// Excellent 2d physics library Box2D
// SFML 2 book
Posted 13 March 2013 - 02:44 PM
Posted 13 March 2013 - 02:56 PM
Here it is again with more of a generic-C-dialect feel:
// Finally, find location of Mouse and point player towards it MouseX = GetMouseX(); MouseY = GetMouseY(); Angle = atan2(MouseY - WindowHeight / 2, MouseX - WindowWidth / 2); body.SetAngle(Angle);
Posted 13 March 2013 - 02:57 PM
Thanks but I have no idea how to read c++ so I have no idea what's going on lol
It should be obvious what it is doing by looking at the lines:
The 1st 2 lines are getting the X and Y coordinates of the mouse on the screen, storing them in variables MouseX and MouseY
The next line performs the atan2 function using the difference of the location of the mouse and the player on the screen (the player is ALWAYS at the center of the screen, that's why it uses mpApp->GetHeight()/2 and mpApp->GetWidth()/2
Think of it like this
// PlayerX and PlayerY are the x and y coordinates of the player on the screen Angle = atan2(MouseY - PlayerY, MouseX - PlayerY); // Take the Angle and apply it to your player's sprite or physical body PlayerSprite->Rotate(Angle)
Edited by BeerNutts, 13 March 2013 - 02:58 PM.
Posted 13 March 2013 - 02:59 PM
Posted 13 March 2013 - 03:20 PM
Posted 13 March 2013 - 04:47 PM
Posted 14 March 2013 - 05:29 AM
I can't waste the opportunity to say that many of these things are better implemented using vector algebra than angles. Angles are very intuitive, but code that uses them is often full of special cases that have to be dealt with using `if' statements, and they require using expensive calls to trigonometric functions.
I can't agree more: you may not know, but there is a little need of "school trigonometry" even in 3D, because everything can be done very elegantly using vectors. And all of it works on 2D, you just don't use Z axis.
If you store the locations for object a and b as vectors, you can:
- Get a distance between them by subtracting them and returning distance vector's length: (A-B).length() or (B-A).length()
- Get a direction A must go to to eventually get to B: (B-A).normalize() ("normalizing" always returns vector of length 1)
- Angle between two normalized vectors C and D is acos(C dot D).
And there are math libraries which already have all these operations written for vectors.
So if you need a rotation vector for your object to face your mouse, you would write something like this (assuming object position is in same space as mouse position):
#mousePos = ... #objectPos = ... initialLookDirection = Vector2(0, 1) # intitally your object looks "up", if Y axis is "up" targetLookDirection = (mousePos - objectPos).normalize() rotationInRadians = acos(initialLookDirection * targetLookDirection) # "*" is "dot" product in pygame Vector2
For pygame, you would use pygame.math.Vector2.
Edited by Nercury, 14 March 2013 - 06:02 AM.
Posted 14 March 2013 - 04:56 PM
Posted 14 March 2013 - 05:15 PM
Looks like pymunk tutorial is here:
However looks like pymunk uses compiled C lib, my knowledge ends here.
Posted 14 March 2013 - 06:32 PM
Posted 14 March 2013 - 08:14 PM
Edited by MiniKong, 14 March 2013 - 08:43 PM.
Posted 14 March 2013 - 08:15 PM
Looks like pymunk tutorial is here: