# Moving in the direction of rotation?

This topic is 2130 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am working on a 2D project that requires rotation and moving in the direction of rotation. I have managed to get the image to rotate but I am not able to move the image in the direction of rotation. I have had this problem for a couple of days now. Here is a snippet of the source code:

Object Info

static float shipx =  300f;

static float shipy =  300f;

static int shipspeed = 20;


Update Method

if(input.isKeyDown(Input.KEY_UP))

{

// What I am using to move in the direction of rotation, but I'm not having any luck.
shipx +=  (float) (shipspeed * Math.toRadians(Math.cos(angle)));

shipy +=  (float) (shipspeed * Math.toRadians(Math.sin(angle)));

System.out.println("X: " + shipx + " " + "Y: " + shipy);

}

else if(input.isKeyDown(Input.KEY_DOWN))

{

shipy += shipspeed;

}

else if(input.isKeyDown(Input.KEY_RIGHT))

{

angle += rotation;

Ship.setCenterOfRotation(Ship.getWidth() / 2, Ship.getHeight() / 2);

Ship.setRotation((float) angle);

}

else if(input.isKeyDown(Input.KEY_LEFT))

{

angle -= rotation;

Ship.setCenterOfRotation(Ship.getWidth() / 2, Ship.getHeight() / 2);

Ship.setRotation((float) angle);

}



I am not using any vector math or anything, just simple trig. Feel free to ask for more information if needed. Thanks.

##### Share on other sites

It looks like toRadians() should be inside cos(), not outside. The same for sinus.

##### Share on other sites

shipx +=  (float) (shipspeed * Math.toRadians(Math.cos(angle)));

shipy
+=  (float) (shipspeed * Math.toRadians(Math.sin(angle)));

should probably be:

 shipx +=  (float) (shipspeed * Math.cos(Math.toRadians(angle)));

shipy +=  (float) (shipspeed * Math.sin(Math.toRadians(angle)));


EDIT: lol great minds. just a moment to late to be first with it =P

Edited by ryutenchi

##### Share on other sites

Hello guys, thanks for the quick responses I really appreciate it. I implemented what you guys suggested and I am still not getting the desired result. Here is extra information.

double rotation = 1.570796;
static double angle = Math.toRadians(Math.atan(shipx / shipy));


shipx +=  (float) (shipspeed * Math.cos(Math.toRadians(angle)));
shipy +=  (float) (shipspeed * Math.sin(Math.toRadians(angle)));


##### Share on other sites

I am not using any vector math or anything, just simple trig. Feel free to ask for more information if needed. Thanks.

Atan2 is not simple trig. Then you take the angle returned from atan2 and use it to call sin, cos.

This is one of my pet peeves.

USE VECTOR MATHS.

You turn a direction into an angle, then turn your angle into a direction. Is there any need for that? No USE VECTORS.

##### Share on other sites

What Paradigm Shifter said. Also, if you ever use angles, stick to radians. You seem to make lots of mistakes in converting between radians and degrees (the one pointed out by the first two responses, plus another one in converting the result of Math.atan to radians, when I am sure it was in radians to begin with). There is just no need to do any of those conversions.

If you don't know how to do something with vectors, ask.

##### Share on other sites

Alright so I am trying to utilize vectors math more in my projects. This is what I have so far...

Variables Data:

    // Ship;
static float shipx =  300f;
static float shipy =  300f;
static float shipspeedx = 1f;
static float shipspeedy = 1f;
Vector2f Current = new Vector2f(shipx, shipy);
Vector2f Velocity = new Vector2f(shipspeedx, shipspeedy);


Update Method:

if(input.isKeyDown(Input.KEY_UP))
{
}


Problem is though the ship is moving diagonally. My plan is to make my resultant vector vertical so the ship moves up, but I think I was once told that resultant vectors can't be vertical.

##### Share on other sites

Vectors can be vertical you only get problems with tangents as they approach infinity, which vectors avoid. Vector away, hip hip array.

##### Share on other sites

Yeah, you want some sort of vector that represents direction, preferably normalized, then you can just do things like

if(input.isKeyDown(Input.KEY_UP))
{

this.velocity += this.direction * this.thrustAmt;

}

Though technically it would probably work better if you were adjusting the acceleration, and you probably want a deltaTime in there, and a cap to the velocity.

##### Share on other sites

@ferrous, thanks for the reply.

I am confused at what the

this
keyword does in Java. From my understanding its similar to C++ reference operator (&) ? Is this correct?

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 9
• 15
• 9
• 11