Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Rotating a sprite and moving it

Ok,

I have an object. It has the following variables:

- rotation (in degrees, 0..360)
- xpos
- ypos

I want to move it forwards when the up arrow is pressed.

My brain is dead and I can't work out how to do it. It needs to move along x and y depending on what value rotation is.

Can someone put me out of my misery?
 
Ok I'll try work it out.

Basically I guess I have...

A right angle triangle. x and y are what I need to find out. I have the angle between h and y, which is rotation; the angle between x and y is a right angle, so I need trig. Oh, I see...

So.

I need h (hyp) to equal 1; I have h and I have angle, so CAH will give me the adjacent, or y.

A
C H


Hmmmmmn.

So y = cos(theta) * h?

y = cos(rotation) * 1
y = cos(rotation)


To get x use pythagoras,

x^2 = 1 - y^2


Does this sound right?
 
This is as far as I got, and nope, not right at all.

C:
<div class="c" id="{CB}" style="font-family: monospace;"><ol>            <span style="color: #993333;">float y = cos(player->Sprite->GetRotation());

            <span style="color: #993333;">float x = sqrt(<span style="color: #cc66cc;">1 - (y * y));

 

            <span style="color: #b1b100;">if (player->Sprite->GetRotation() > <span style="color: #cc66cc;">180)

            { x *= <span style="color: #cc66cc;">-1; }

            <span style="color: #b1b100;">if ((player->Sprite->GetRotation() > <span style="color: #cc66cc;">90) && (player->Sprite->GetRotation() < <span style="color: #cc66cc;">270))

            { y *= <span style="color: #cc66cc;">-1; }

 

            player->xpos += x;

            player->ypos += y;
 
radians = degrees * pi / 180.0
x = distance * cos(radians)
y = distance * sin(radians)

Depending on where is the 0 angle, x and y may be inverted and/or multiplied by -1.
 
Actually rotation is something like this:

Rotating a point from position (o.x, o.y) to position (oR.x, oR.y) through an angle "theta" about pivot point (oP.x, oP.y)

oR.x = oP.x + (o.x - oP.x) * cos(theta) - (o.y - oP.y) * sin(theta)

oR.y = oP.y + (o.x - oP.x) * sin(theta) + (o.y - oP.y) * cos(theta)

Thats the complete rotation for 2D around a point, you can find all you need about the rotation from that formulas, otherwise there might be some trouble in some cases.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top