# Propper Movement *SOLVED*

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

## Recommended Posts

Greetings. I'm not quite sure how I would make a game where you would actually travel in the direction you are facting... If your traveling at 0, 90, 180 etc. its easy, but how would I figure out how much to move the player on the x and y axis' if he was moving at a 58° angle? Thanks. [Edited by - Darobat on November 11, 2004 10:21:25 AM]

##### Share on other sites
Learn some basic math and trig and you will be off to a good start. To get you started:

x += cos(angle) * speed;
y += sin(angle) * speed;

Note that the angle should be in radians.

##### Share on other sites
Thats what I was thinking but I wasn't sure if that would actuall work. Thanks.

##### Share on other sites
Greetings,
I tried implementing your suggestion but it so far is unsuccesful. Heres what I have:
if(keys[VK_UP])	{		guy.y += sin(DEG_TO_RAD(guy.rot));		guy.x += cos(DEG_TO_RAD(guy.rot));	}	else if(keys[VK_DOWN])	{		guy.y -= sin(DEG_TO_RAD(guy.rot));		guy.x -= cos(DEG_TO_RAD(guy.rot));	}

When I have an angle of 0 I move left and right. not up and down. DEG_TO_RAD is defined as #define DEG_TO_RAD(x) (x * (180 / 3.14159)) Any ideas? Thanks.

##### Share on other sites
Try changing the DEG_TO_RAD(x) to (x*(PI/180)).

##### Share on other sites
lol, I new it was something stupid.

##### Share on other sites
That didn't change anything... :( Al the angles are 90° off CW.

##### Share on other sites
When you have an angle of 0, the cos value is 1 so you will move left and right, to counter that effect just swap the sin and cos.

##### Share on other sites
Ooook... 0° makes me move down. 90° moves me correctly. 180° moves me up. 45° moves me 135°. The closer I get to 90° the more accurate it is...

##### Share on other sites
ok, so did that work out . . . you still having problems or is it all good now?

##### Share on other sites
for one thing, you aren't modifying the value of guy.rot. You should rotate the guy by changing this value...not by changing the sign of the equations.

instead you should do something like;

if(keys[VK_UP]){   guy.rot += 2; //2 is just an example}else if(keys[VK_DOWN]){   guy.rot -= 2;}move();//here is the implementation of move()void move(){   guy.y += sin(DEG_TO_RAD(guy.rot)) * guy.speed;   guy.x += cos(DEG_TO_RAD(guy.rot)) * guy.speed;}

##### Share on other sites
No its not working. 0° should move me up. 180° should move me down and 45° should move me 1 up on the x and 1 up on the y.

 didn't see your post. I didn't post the angle changing part because it works. Its the part where I move at the angle that doesnt.

##### Share on other sites
Ah, I see. The problem is that the unit circle travels in a counter clock wise direction while you want it to work the other way . . . the other problem is that you want 0 to move you up. This should be an easy solution. Try adding 90 to the current angle, and negate the current angle as well, so the code for up might look like:

tell me if it works or not.

##### Share on other sites
Nope. I can't even see a pattern this time

##### Share on other sites
well, then . . . i'm not sure what to say. I thought for sure the last bit would work. Hmmm . . . because if you think about it, adding 90 to the angle will make sin(0+90) = 1 therefore you move forward, and sin(90-90) is 0 while cos(90-90) is 1, so you move right, and so on and so forth.

Edit: Purhaps something is wrong elsewhere?

##### Share on other sites
Some combination of positive and negative sin and cos should be working for you. How are your world axes oriented (i.e. y up and x right)? Which direction (clockwise or counterclockwise) is 'positive' rotation for you? Given this information, we should be able to figure out your problem.

##### Share on other sites
Ok, I'm using OpenGL with ortho turned on so that the top left of the screen is 0, 0. Rotating CW will give me a positive rotation while rotating CCW will give me a negative rotation.
   0270  45  180
y is up and x is right. Heres my source to date. It draws the rotation properly but I won't move the right direction.

void Update(){	if(keys[VK_UP] || keys['W'])	{		guy.y += cos(DEG_TO_RAD(-guy.rot + 90));		guy.x += sin(DEG_TO_RAD(-guy.rot + 90));	}	else if(keys[VK_DOWN] || keys['S'])	{		guy.y -= cos(DEG_TO_RAD(-guy.rot + 90));		guy.x -= sin(DEG_TO_RAD(-guy.rot + 90));	}	if(keys[VK_LEFT] || keys['A'])	{		guy.rot--;		if(guy.rot < 0)			guy.rot += 360.0f;	}	else if(keys[VK_RIGHT] || keys['D'])	{		guy.rot++;		if(guy.rot > 359)			guy.rot -= 360.0f;	}}

##### Share on other sites
Just thought I'd mention I got it working. I changed DEG_TO_RAD to #define DEG_TO_RAD(x) (((x) * 3.14159) / 180)

Then I took te - off of guy.rot and it worked! Thanks.