Public Group

# Rotation and movement

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

## 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 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.14159265358979323846float degtorad(float angle){  return 2*PI*(angle/360.0);}...if (keys[SDLK_UP]) {  float moving_dist=0.08;  xPos=xPos+sin(degtorad(angle))*moving_dist;  zPos=zPos-cos(degtorad(angle))*moving_dist;}...

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 on other sites
Thank you, now I can continue.. : )

Edit:
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]) { 	    xPos=xPos-sin(degtorad(angle+90))*moving_dist;        zPos=zPos+cos(degtorad(angle+90))*moving_dist;	}	if (keys[SDLK_a]) {        xPos=xPos-sin(degtorad(angle-90))*moving_dist;        zPos=zPos+cos(degtorad(angle-90))*moving_dist;	}

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

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
18
5. 5

• 9
• 12
• 9
• 33
• 13
• ### Forum Statistics

• Total Topics
632589
• Total Posts
3007241

×