Sign in to follow this  
Lirben

rotation environnement

Recommended Posts

hello. When I move with my mouse through my environnement, the environnement is turning. (like it should be). I thought when I do it like this, the Z-axis will always be in front of you and the X-axis will always be horizontal to the camera viewport. But it isn't. When I turn my cam 90 degrees and I press key_up, my camera is strafing. When I strafe, my cam goes forward. How do I solve this problem? this is my code. (drawGLscene) GLfloat xtrans = -xpos; GLfloat ztrans; GLfloat ytrans = -walkbias-0.25f; GLfloat sceneroty = -yrot; glLoadIdentity(); glRotatef(sceneroty,0,1.0f,0); glTranslatef(xtrans, ytrans, ztrans); glBindTexture(GL_TEXTURE_2D, texture[0]); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(-50.0f, 0.0f, 50.0f); glTexCoord2f(50.0f, 0.0f); glVertex3f( 50.0f, 0.0f, 50.0f); glTexCoord2f(50.0f, 50.0f); glVertex3f( 50.0f, 0.0f, -50.0f); glTexCoord2f(0.0f, 50.0f); glVertex3f(-50.0f, 0.0f, -50.0f); glEnd(); (mouse_move) POINT mousePos; mousePos.x = 0; mousePos.y = 0; int mid_x = Width >> 1; int mid_y = Height >> 1; float speedy = 0.0f; float angle_z = 0.0f; GetCursorPos(&mousePos); SetCursorPos(mid_x, mid_y); speedy = float((mid_x - mousePos.x)) /1000; yrot -= speedy*100; (keycontrols) if (GetKeyState(VK_UP) & 0x80){ zpos -= camspeed; } if (GetKeyState(VK_DOWN) & 0x80){ zpos += camspeed; } if(GetKeyState(VK_LEFT) & 0x80){ xpos -= camspeed; } if (GetKeyState(VK_RIGHT) & 0x80){ xpos += camspeed; } Thanks

Share this post


Link to post
Share on other sites
There is no "the" Z axis. Every frame-of-reference has its own Z axis. When you move or rotate, your coordinate system (commonly called view or eye space) no longer matches the world's coordinate system (commonly called world space). In your case, movement is relative to (or "in") world space. Forward is the same direction (relative to the world) no matter which way you are facing.

It boils down to this: you do two transformations -- translation followed by rotation. Since the translation is done first, it is done in world space and the rotation is done in the translated space (you rotate around your current position). If you reverse the order, then you will translate the way you want but the rotation would be in world space so you will rotate around the world origin (and you probably don't want that).

The simplest solution is to maintain a matrix that contains your orientation. Then use the 1st, 2nd, and 3rd columns (x, y, and z) as the axes to translate along. In other words if you want to move 5 units in the Z direction, then multiply the vector from the 3rd column (first three elements only) by 5 and add that to your position. This means that while you can use calls like glRotate and glTranslate move and rotate objects in the world, you must also do the same math yourself in order to keep track of your current position and orientation.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this