Advertisement

keyboard movement calculation

Started by July 02, 2008 01:33 AM
1 comment, last by matchu 16 years, 6 months ago
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]
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 : /
Advertisement
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 pointingcharacter.x += cos(angle) * speed;character.y += sin(angle) * speed;


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

hope that helps !

This topic is closed to new replies.

Advertisement