Skip to main content
GameDev.net gamedev.net
🔒 Locked

Propper Movement *SOLVED*

Started by Darobat Nov 9, 2004 at 7:05 PM 16 replies 2.9k views
Original Post
Darobat
Darobat
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]
--------------------C++ Home - Check it out!Lol... - Amazing video
Zakwayda
Zakwayda
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.
Darobat
Darobat
Thats what I was thinking but I wasn't sure if that would actuall work. Thanks.
--------------------C++ Home - Check it out!Lol... - Amazing video
Darobat
Darobat
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.
--------------------C++ Home - Check it out!Lol... - Amazing video
Darobat
Darobat
lol, I new it was something stupid.
--------------------C++ Home - Check it out!Lol... - Amazing video
Darobat
Darobat
That didn't change anything... :( Al the angles are 90° off CW.
--------------------C++ Home - Check it out!Lol... - Amazing video
Charles Thomas
Charles Thomas
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.
Darobat
Darobat
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...
--------------------C++ Home - Check it out!Lol... - Amazing video
Charles Thomas
Charles Thomas
ok, so did that work out . . . you still having problems or is it all good now?
drreagan
drreagan
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;}
------------------------------------------------------- A headache, ancillary; an hourglass auxiliary."something witty, blah, blah, blah" --That One Witty Guy (remember? that one dude?)(author of DustBot)
Darobat
Darobat
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.

[Edit] 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.
--------------------C++ Home - Check it out!Lol... - Amazing video
Charles Thomas
Charles Thomas
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:

guy.y += sin(DEG_TO_RAD(-guy.rot+90));
guy.x += cos(DEG_TO_RAD(-guy.rot+90));

tell me if it works or not.
Darobat
Darobat
Nope. I can't even see a pattern this time
--------------------C++ Home - Check it out!Lol... - Amazing video
Charles Thomas
Charles Thomas
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?
Zakwayda
Zakwayda
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.
Darobat
Darobat
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;	}}
--------------------C++ Home - Check it out!Lol... - Amazing video
Darobat
Darobat
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.
--------------------C++ Home - Check it out!Lol... - Amazing video

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.