Moving object in OpenGL using WASD

Started by
10 comments, last by KieranW 15 years, 2 months ago
Now I have googled this and checked various places but I couldn't seem to find anything much on the subject. I have a cube that I would like to move using arrowkeys. Now I can do something like this:
			if(keys['W'])
			{
				player.TranslateZ(player.GetZPosition() - 0.2f);
			}

			if(keys['S'])
			{
				player.TranslateZ(player.GetZPosition() + 0.2f);
			}

			if(keys['A'])
			{
				player.TranslateX(player.GetXPosition() - 0.2f);
			}

			if(keys['D'])
			{
				player.TranslateX(player.GetXPosition() + 0.2f);
			}
However that only allows the cube to move sideways by strafing. I am wanting it to be able to rotate with the A and D keys (which I can do) but then I need it to adjust the speed at which the object moves on the X and Z axis accordingly. Now from what I have heard this will be best done with trig (sine and cosine) however I actually don't know trig so I am not sure how to do this. Help is appreciated :)
Advertisement
try:

player.x += cosf(player.facingangle);
player.z += sinf(player.facingangle);

should move the player forward. you can add/subtract pi/2 to the angle to get strafing movement.
thanks for the help, however it isn't working very well.

if(keys['W'])			{				player.TranslateX(player.GetXPosition() + cosf(player.GetYAngle()));				player.TranslateZ(player.GetZPosition() + sinf(player.GetYAngle()));			}			if(keys['S'])			{				player.TranslateZ(player.GetZPosition() + 0.2f);			}			if(keys['A'])			{				player.RotateY(player.GetYAngle() - 2.0f);			}			if(keys['D'])			{				player.RotateY(player.GetYAngle() + 2.0f);			}


What is happening basically is that even the slightest movement completely changes the direction, it isn't going in the correct directions at the right speed.
Think about what these lines of code do:
player.TranslateX(player.GetXPosition() + cosf(player.GetYAngle()));player.TranslateZ(player.GetZPosition() + sinf(player.GetYAngle()));
Basically, you're not just adding the direction vector to the player position, you're adding the current player position to the player position as well! (It should be fairly obvious why this is wrong, and what needs to be done to fix it.)
Actually TranslateX doesnt add to the position, it changes it to a new position so I needed to grab the current position and then add to that.

So if the player was at X:20 then I moved foward 1 unit I would need 20 + 1 to get to 21 otherwise I would be back at X:1
Quote:Original post by KieranW
Now from what I have heard this will be best done with trig (sine and cosine) however I actually don't know trig so I am not sure how to do this.


IMO, google vectors and learn vector algebra and trigonometry. There's no reason to be scared of learning it and if you want to actually write non-trivial games you'll use it a thousand times over. In the long run, it's not useful to just copy and paste someone else's code without knowing the basics.
Is the rotation degrees or radians?
You may need to convert the radians to degrees if your using degrees when your drawing.
It seems to be in degrees.
I actually managed to do this once with a long complex equation without trig but it was ineffiecient and yes I would be better to use trig.
Ok well the problem was the degrees/radians thing so I just had to multiply by pi/180. Also Cosine and Sine were on the wrong axis, I had to change them around.

Thanks all for your help :)
Quote:Original post by KieranW
Actually TranslateX doesnt add to the position, it changes it to a new position so I needed to grab the current position and then add to that.

So if the player was at X:20 then I moved foward 1 unit I would need 20 + 1 to get to 21 otherwise I would be back at X:1
Oh, ok. The function names TranslateX() and TranslateY() are a little misleading then (in this context at least, a translation is generally considered to be a relative change in position).

This topic is closed to new replies.

Advertisement