Jump to content
  • Advertisement
Sign in to follow this  
jagguy2

keyboard movement calculation

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am confused about the calculations to move an object on screen with the keyboard. Now I have X,Y value for position where 0,0 is top left. I can move the object using arrow keys in up,down,left and right in a direction of value of 10PX each time. I can move it but my problem is this. I want to just use UP key in the direction it is facing and hold either right or left key to change direction say 10 deg. If I hold the right key down the object will rotate 10deg and continue to move forward if up arrow is still pressed. So the UP key means forward and down key means turn around 180 deg only. I am not sure of the calculations . ps polar coords! [Edited by - jagguy2 on July 2, 2008 4:36:42 AM]

Share this post


Link to post
Share on other sites
Advertisement
I'm a little confused about what your asking about. What program are you using? And I'm not sure this should be in the Visual Arts section : /

Share this post


Link to post
Share on other sites
you need to have the angle the character is facing and some trig...

each time you press the right or left key, you add or remove 10 degrees to your angle.

when you press the up key, you simply add some speed to the character

float angle; // your character angle (IIRC 0 = pointing right)
int speed; // number of pixels forward

// this will move your character in the position it's pointing
character.x += cos(angle) * speed;
character.y += sin(angle) * speed;




disclaimer: this code might not compile, it hasn't been tested

hope that helps !

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!