game making troubles

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?
+/- on the axis. how would i do the trigonometry one?

p.s i know trig so all i need is a quick explanation of how to implement it.
Last edited on
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;
}
ok i put that in to a finction and all the person does is move left and right unless i turn the camera then he is back and forth.
Can you post the code for your movement function and anything related?
here is when the user presses a button

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
case 'w':
GetCamAngle();
depthz -= zdif;
depthx -= xdif;
break;
case 's':
GetCamAngle();
depthz += zdif;
depthx += xdif;
break;
case 'a':
camera -= 2.0;
if(camera < 0)
camera += 360;
GetCamAngle();
break;
case 'd':
camera += 2.0;
if(camera > 360)
camera -= 360;
GetCamAngle();
break;


here is the get cam angle function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int GetCamAngle()
{
	xdif = sin(camera);
	zdif = cos(camera);
	if(zdif < 0)
		zdif = zdif * -1.0;
	if(zdif > 0)
		zdif = zdif * 1.0;
	if(xdif < 0)
		xdif = xdif * -1.0;
	if(xdif > 0)
		xdif = xdif * 1.0;
	cout << zdif << " " << xdif << " ";
	return 1;
}


the cout is for debugging.
Last edited on
you're working in degrees, but sin() and cos() use radians.

Also I'm a little puzzled as to why you're taking the absolute value of xdif and zdif. That pretty much stops the camera from pointing south or west.
Last edited on
ok how do i change it to rads?
o_O didn't you just claim to know trig? =P

rad = deg*PI/180
grade 10 trig lol.
the thing cos sin thing works but when i turn a second time it goes all random.
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.
we just set our calcs to rads.
Sigh...
You should have been taught that 2*pi=1 revolution. From that you could infer that deg*pi/180, since 180°=pi/2 radians.
you could have found out how he should move by drawing 2 axes and a turned guy on paper
Not to mention I paraphrased it in my first post:


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)
i noticed that after i posted it. i feel really stupid right now. lol
Topic archived. No new replies allowed.