Sign in to follow this  
Darobat

Propper Movement *SOLVED*

Recommended Posts

Darobat    100
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 this post


Link to post
Share on other sites
jyk    2094
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 this post


Link to post
Share on other sites
Darobat    100
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 this post


Link to post
Share on other sites
Darobat    100
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 this post


Link to post
Share on other sites
drreagan    122
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 this post


Link to post
Share on other sites
Darobat    100
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.

Share this post


Link to post
Share on other sites
Charles Thomas    130
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.

Share this post


Link to post
Share on other sites
Charles Thomas    130
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 this post


Link to post
Share on other sites
jyk    2094
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 this post


Link to post
Share on other sites
Darobat    100
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.
   0
270 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 this post


Link to post
Share on other sites
Darobat    100
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this