Hi all,
I’ve looked at about 100 or more forums about translations and rotations and a lot of them say the same thing that i’ve been doing in my code. However, I’m still unable to do what i want.
What I have is a large plane that is to be my area where my object is able to move. I then have a cube that is drawn and starts off at a certain position on the plain. It then has the ability to rotate 360 degrees about its local Y-axis and then translate up and down its local Z-axis.
I am able to draw my cube on its own local coordinate system. Before i command it to move, it rotates correctly about its own Y-axis. Once i command it to translate down the positive Z-axis, that is also correct. The problem is once I try to rotate my cube again, it rotates relative to the Y-axis at the initial position Z=0. Basically, I want the coordinate system to follow the cube where ever it goes, so that it can rotate and move about the plane freely.
Relevant code is posted below. If you need more information, please let me know!!! ANY HELP IS APPRECIATED!! THANKS!
CODE FOR DRAWING AND MOVING THE CUBE
cube.cpp
void cube::drawCube(GLfloat x, GLfloat y, GLfloat z, int spin){
glRotatef((GLfloat) _spin, 0.0, 1.0, 0.0);
glTranslatef(_x, _y, _z);
if (!_texLoaded)
setupTexture();
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBindTexture(GL_TEXTURE_2D, _robotTex);
glBegin(GL_QUADS);
// front
glTexCoord2f(0.0f, 0.0f); glVertex3f(-8.0f, 0.0f, 8.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(8.0f, 0.0f, 8.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(8.0f, 16.0f, 8.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-8.0f, 16.0f, 8.0f);
glEnd();
glBindTexture(GL_TEXTURE_2D, _robot2Tex);
glBegin(GL_QUADS);
// left side
glTexCoord2f(0.0f, 0.0f); glVertex3f(-8.0f, 0.0f, -8.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-8.0f, 0.0f, 8.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-8.0f, 16.0f, 8.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-8.0f, 16.0f, -8.0f);
// backside
glTexCoord2f(0.0f, 0.0f); glVertex3f(8.0f, 0.0f, -8.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-8.0f, 0.0f, -8.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-8.0f, 16.0f, -8.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(8.0f, 16.0f, -8.0f);
// right side
glTexCoord2f(0.0f, 0.0f); glVertex3f(8.0f, 0.0f, 8.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(8.0f, 0.0f, -8.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(8.0f, 16.0f, -8.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(8.0f, 16.0f, 8.0f);
// top
glTexCoord2f(0.0f, 0.0f); glVertex3f(-8.0f, 16.0f, 8.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(8.0f, 16.0f, 8.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(8.0f, 16.0f, -8.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-8.0f, 16.0f, -8.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void cube::moveForward(){
_z += 3.5f;
}
void cube::moveBack(){
_z -= 3.5f;
}
void cube::rotateLeft(){
_spin = (_spin + 20) % 360;
}
void cube::rotateRight(){
_spin = (_spin - 20) % 360;
}
CODE FOR DRAWING THE PLANE
plane.cpp
void plane::drawPlane(){
if (!_texLoaded)
setupTextures();
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBindTexture(GL_TEXTURE_2D, _terrainTex);
// plain
GLfloat cellZ = 0.0f;
GLfloat cellX = 0.0f;
for (int row = 1; row <= 19; row++){
cellX = 0.0f;
if (row != 19){
for (int cell = 0; cell < 25; cell++){
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(cellX, 0.0f, cellZ+32.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(cellX+32.0f, 0.0f, cellZ+32.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(cellX+32.0f, 0.0f, cellZ);
glTexCoord2f(0.0f, 1.0f); glVertex3f(cellX, 0.0f, cellZ);
glEnd();
cellX += 32.0f;
}
}
else{
for (int cell = 0; cell < 25; cell++){
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(cellX, 0.0f, cellZ+24.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(cellX+32.0f, 0.0f, cellZ+24.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(cellX+32.0f, 0.0f, cellZ);
glTexCoord2f(0.0f, 1.0f); glVertex3f(cellX, 0.0f, cellZ);
glEnd();
cellX += 32.0f;
}
}
cellZ += 32.0f;
}
glDisable(GL_TEXTURE_2D);
}
CODE FOR DRAWING THE SCENE
main.cpp
void drawScene(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0f, 800.0f/ 600.0f, 1.0f, 750.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(400.0f, cameraY, cameraZ, 400.0f, 0.0f, 300.0f, 0.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
_plane.drawPlane();
glPopMatrix();
glTranslatef(400.0f, 0.0f, 300.0f);
glPushMatrix();
_cube.drawCube(_cube.getX(), _cube.getY(), _cube.getZ(), _cube.getSpin());
glPopMatrix();
glTranslatef(_cube.getX(), 0.0f, _cube.getZ());
glFlush();
glutSwapBuffers();
}