I am using PGELua for the PlaystationPortable. I would just like to know how to make a 2D sprite move in the direction that the sprite is facing.

When the sprite is blitted to the screen it should be heading down the y axis (like it's moving up the screen) but it doesn't appear to be working that way. As far as I am aware, the angles of the images use radians for the rotations.

I have this in a function

function FUNC_MOVE_SHIP(speed,angles)
    VAR_SHIPX = VAR_SHIPX + pge.math.sin(angles) * speed
    VAR_SHIPY = VAR_SHIPY + pge.math.cos(angles) * speed

and I call it in my main program like

    if pge.controls.held(PGE_CTRL_LEFT) then
    elseif pge.controls.held(PGE_CTRL_RIGHT) then
  • 2
    \$\begingroup\$ How DOES it appear to be working? \$\endgroup\$ Jan 9 '12 at 23:11

A common problem with using radians is that they start to the right. Look at this picture (from Wikipedia):


As you can see, it starts at 0 on the right of the circle and goes counter-clockwise around the circle until it hits 2 * PI. So if your sprite isn't pointing to the right with a rotation of 0, you might have to add some constant. Eg. if your sprite is pointing up, you have to add PI * 0.5 to the calculated angle. Also, instead of adding and subtracting 0.01 from the angle, I'd rather choose some fraction of PI instead. Eg. PI / 90 equals 2 degrees of rotation per update.


I believe that your movement formula should only work if the angles value is in degrees. To convert radians to degree simply multiply the radian value by 180 and divide it by pi.

  • \$\begingroup\$ This is very helpful, I never understood (looking at other code) why there was a lot of 180/pge.math.pi() \$\endgroup\$ Jan 10 '12 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.