Jump to content
  • Advertisement
Sign in to follow this  

Rotation and movement

This topic is 4323 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm coding an application in OpenGL in which you can move around the camera with a cube somewhere in the middle. I wanted the cube so that the environment not became too empty. Everything works so far in this program except the rotation when you press left or right. When I press the LEFT or RIGHT button which are supposed to rotate the camera, rotation occurs but it seems only the cube in the middle rotates. What I want to do is something similiar to movement in a FPS game, now it only seems to rotate the cube itself. I thought that maybe if I increase/decrease the x-position at the same time as I increase/decrease the angle it may work. Well it did work better, but when I rotate for too long the "player" moves farther and farther away from the cube instead of actually staying at the place he were in the beginning of the rotation. I kind of know why this happens and would guess that triognomethry could solve this problem. The only problem is that my knowledge about trig is very limited right now since I haven't come that far yet. In order to make it easier for you to understand what I want to do, I'll post some code: (Input code)
	if (keys[SDLK_UP]) zPos+=0.08;
	if (keys[SDLK_DOWN]) zPos-=0.08;
	if (keys[SDLK_d] && (!keys[SDLK_a] && !keys[SDLK_d])) xPos-=0.08;
	if (keys[SDLK_a] && (!keys[SDLK_a] && !keys[SDLK_d])) xPos+=0.08;
	if (keys[SDLK_LEFT]) { angle-=0.49; xPos+=???; }
	if (keys[SDLK_RIGHT]) { angle+=0.49; xPos-=???; }

I have tried to replace xPos in the last two lines with xPos=xPos*cos(angle) and some different values. Draw code:
    glTranslatef(xPos, -2.0, zPos);
    glRotatef(angle, 0.0, 1.0, 0.0);
    glScalef(4.0, 1.0, 6.0);
    DrawRoom(); // actually draws the cube, I named it DrawRoom though.

I've only been learning opengl for a couple of days now so maybe i've misunderstood how the rotatef function really works, does it rotate the cube or the camera? My guess is that it is the cube that rotates so I thought of the idea that maybe I have to adjust the position according to the rotation of the cube. Any ideas how I can make this work?

Share this post

Link to post
Share on other sites
What you have to understand is that the matrix operations take place in the reverse order you define them(that's not OpenGL specific, it's just math). So, what you're currently doing is:

>>glTranslatef(xPos, -2.0, zPos);
>>glRotatef(angle, 0.0, 1.0, 0.0);

1)Rotate the cube by "angle" around origin(0,0,0)
2)Move the cube to (xPos,-2.0,zPos)

this is not what you want. In contrast, what you want is:

>>glRotatef(angle, 0.0, 1.0, 0.0);
>>glTranslatef(-xPos, +2.0, -zPos);

Notice how I have invert the glTranslatef. If (xPos,-2.0,zPos) is the coords of the camera position, the glTranslatef must use (-xPos,2.0,-zPos).

1)Move the cube to (-xPos,+2.0,-zPos)
2)Rotate the cube around origin(0,0,0)

This is how you'll achieve to rotate the "camera" instead of the cube.

Also, your moving functions need correction. Right now, you're just moving along the z-Axis, but in an FPS you need to move where you're actually looking.
One way is to use sin() and cos(), but keep in mind those take angle in radians, not degrees like OpenGL does. So:

#define PI 3.14159265358979323846

float degtorad(float angle)
return 2*PI*(angle/360.0);

if (keys[SDLK_UP])
float moving_dist=0.08;

However, I strongly suggest studing at least the basics of trigonometry and linear algebra, since you won't go far into 3D programming without them.

[Edited by - mikeman on February 13, 2007 8:00:08 AM]

Share this post

Link to post
Share on other sites
Thank you, now I can continue.. : )

For those who are interested. The input code for walking LEFT/RIGHT worked with this method as well but I had to add a 90 degree angle like this:

if (keys[SDLK_d]) {
if (keys[SDLK_a]) {

[Edited by - password on February 13, 2007 9:37:07 AM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!