i am making a game with opengl. its not actual a game. my idea is to just have a court yard and the user can walk around freely. the problem is i cant get the guy to move forward in the direction he is facing. what i have so far is the person can move back and forward turn but when he is turned he doesnt move back and forward any more its now side to side. can any one give me some ideas?
How are you changing the position? Are you just adding and subtracting to one axis, or are you using trigonometry to determine how much should each of two axes increase or decrease?
the direction the player is facing is represented as an angle. This angle starts facing right and circulates counter clockwise:
0 rads (0 deg) = facing right
PI/2 rads (90 deg) = facing forward ("up" from a bird's eye view)
PI rads (180 deg) = facing left
3*PI/2 rads (270 deg) = facing back ("down" from a bird's eye view)
Speed on each axis can be determined by using the sin/cos of the angle they're facing. sin( angle ) = Z speed, cos( angle ) = X speed.
Assuming Z axis is forward/backward (Y is usually up/down -- which doesn't really make sense unless the person can fly) and X is left/right.
sin/cos return a value between [-1..+1], so multiply this value by the desired speed you want the player to move, and presto:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <cmath>
// globals are ugly, but I'm trying to keep the example simple
double xunitspeed, zunitspeed;
double facing; // angle the player is facing
void GetUnitSpeeds() // call this when the player's direction rotates
{
xunitspeed = std::cos( facing );
zunitspeed = std::sin( facing );
}
void MovePlayer(double rate)
{
// rate is the rate at which to move them
player_x += xunitspeed * rate;
player_z += zunitspeed * rate;
}
I thought the first thing you learned when you learned sine and cosine was that they gave you radians...then you learned that you can convert radians to degrees.
0 rads (0 deg) = facing right
PI/2 rads (90 deg) = facing forward ("up" from a bird's eye view)
PI rads (180 deg) = facing left
3*PI/2 rads (270 deg) = facing back ("down" from a bird's eye view)