Archived

This topic is now archived and is closed to further replies.

Subotron

3D movement in space with OpenGL

Recommended Posts

Subotron    182
Ok, here we go... I have some problems with the movement in OpenGL in 3D. I use the glTranslatef and glRotatef commands for the movement in the code I made so far. glTranslatef to get another coord like this: glTranslatef(player.x, player.y, player.z); player.x is increased when pressing W, and decreased when pressing S player.z is increased when pressing D, and decreased when pressing A This uses strafing. But now, I want to rotate, and move forward again using the new angel. If I use glRotatef, I can rotate, say, 45 degrees on the x axis. When I press W though, to move again, it increases the player.x value, which doesn''t move me forward with the new angle, but with the old one! So I can''t really make the player move towards the new set angle. And even worse, I have absolutely NO CLUE how to do it in 3D!!! Add the point that space-movement is even harder, because there is no thing like gravity and other forces to let you stop, you can see that I am absolutely lost! Oh, I program with Visual C++ and use OpenGL, not GLUT! If anyone can help me on this, please mail me about it or post it here. My e-mail address is HNSProgramming@hotmail.com (old address ) Source examples are also welcome. It doesn''t matter if you use other commands than glRotatef and glTranslatef, as long as it works

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
true moving like so...

player.x += sin(angle*3.1415/180) * speed;
player.z += cos(angle*3.1415/180) * speed;

//not depending on how you do things, you may need to use - instead of + or switch cos/sin around.

Share this post


Link to post
Share on other sites
Subotron    182
hmmmmmm yeah I thought about sin and cos for a little while... I just thought there''d be an OpenGL-implemented function for this movement though, so I don''t have to calculate it all...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
WTF? How lazy can you get.. it''s 2 calls, 1 to cos, and 1 to sin. How is opengl going to hide that for you?

I''ll make it nice and simple for you:

glMovePlayer(PlayerS &p, float Speed)
{
p.x += sin(p.angle*3.1415/180) * Speed;
p.z += cos(p.angle*3.1415/180) * Speed;
}

Simply call glMovePlayer(Player,1), or glMovePlayer(Player,-1) to move your player forward/backwards.. how much simpler can it get?

Share this post


Link to post
Share on other sites