Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Problem with OpenGL

jmusyjjmusyj Member Posts: 33
I have to program a scene that animates a truck going across the screen. As it moves forwards the wheels rotate in the proper direction and the same happens if it is moving in reverse. I have everything working properly except for the second and third wheels on the truck. The second wheel seems to be rotating around the first one and the third wheel seems to be rotating around the second one. Here's the code for the drawing of the wheels:
[code]
/*=====DRAW FIRST(LEFT) WHEEL======*/
wheel1.setX(15);
wheel1.setY(110);

glColor3f(0.0f,0.0f,1.0f); // set the drawing color to blue

glTranslatef(15+tx+wheel1.getX(), 15+ty+wheel1.getY(), 0.0); //translate
if(direction) //moving forwards
glRotatef(-angle, 0.0, 0.0, 1.0); //rotate needed amount
else //moving backwards
glRotatef(angle, 0.0, 0.0, 1.0); //rotate needed amount
glTranslatef(-(15+tx+wheel1.getX()), -(15+ty+wheel1.getY()), 0.0); //translate back

glTranslatef(wheel1.getX()+tx,wheel1.getY(), 0); //translate to position of wheel1

glBegin(GL_POLYGON);
for(i=0; i<wheel1.getNumVertices(); i++) {
glVertex2i(wheel1.getPointX(i), wheel1.getPointY(i));
}
glEnd();

glTranslatef(-(wheel1.getX()+tx),-(wheel1.getY()),0); //translate back
if(direction)
glRotatef(angle, 0.0, 0.0, 1.0); //rotate back
else
glRotatef(-angle, 0.0, 0.0, 1.0); //rotate back

glFlush();



/*=====DRAW SECOND(MIDDLE) WHEEL=====*/

wheel2.setX(145);
wheel2.setY(110);

glColor3f(0.0f,0.0f,1.0f); // set the drawing color to blue

glTranslatef(15+tx+wheel2.getX(), 15+ty+wheel2.getY(), 0.0);
if(direction) //moving forwards
glRotatef(-angle, 0.0, 0.0, 1.0); //rotate needed amount
else //moving backwards
glRotatef(angle, 0.0, 0.0, 1.0); //rotate needed amount glTranslatef(-(15+tx+wheel2.getX()), -(15+ty+wheel2.getY()), 0.0);
glTranslatef(wheel2.getX()+tx,wheel2.getY(), 0);

glBegin(GL_POLYGON);
for(i=0; i<wheel2.getNumVertices(); i++)
glVertex2i(wheel2.getPointX(i), wheel2.getPointY(i));
glEnd();

glTranslatef(-(wheel2.getX()+tx),-(wheel2.getY()),0); //translate back
if(direction)
glRotatef(angle, 0.0, 0.0, 1.0); //rotate back
else
glRotatef(-angle, 0.0, 0.0, 1.0); //rotate back


/*=====DRAW THIRD(RIGHT) WHEEL=====*/

wheel3.setX(245);
wheel3.setY(110);

glColor3f(0.0f,0.0f,1.0f); // set the drawing color to blue

glTranslatef(15+tx+wheel3.getX(), 15+ty+wheel3.getY(), 0.0);
if(direction) //moving forwards
glRotatef(-angle, 0.0, 0.0, 1.0); //rotate needed amount
else //moving backwards
glRotatef(angle, 0.0, 0.0, 1.0); //rotate needed amount glTranslatef(-(15+tx+wheel2.getX()), -(15+ty+wheel2.getY()), 0.0);
glTranslatef(-(15+tx+wheel3.getX()), -(15+ty+wheel3.getY()), 0.0);

glTranslatef(wheel3.getX()+tx,wheel3.getY(), 0);

glBegin(GL_POLYGON);
for(i=0; i<wheel3.getNumVertices(); i++)
glVertex2i(wheel3.getPointX(i), wheel3.getPointY(i));
glEnd();

glTranslatef(-(wheel3.getX()+tx),-(wheel3.getY()),0); //translate back
if(direction)
glRotatef(angle, 0.0, 0.0, 1.0); //rotate back
else
glRotatef(-angle, 0.0, 0.0, 1.0); //rotate back

glFlush();
[/code]
If anyone has any ideas why it's not working it would be greatly appreciated...
Thanks,
Jay

Comments

  • gautamgautam Member Posts: 642
    You probably need to call push and pop matrix while doing the rotation. I have modified the source slightly. Hope it helps. (I have not tested this but OpenGL is a state machine, so if you make the transformation for eg - rotate on y axis, the axis is transformed and not the model. So you will have to take that into consideration when doing your next transformation).

    : I have to program a scene that animates a truck going across the screen. As it moves forwards the wheels rotate in the proper direction and the same happens if it is moving in reverse. I have everything working properly except for the second and third wheels on the truck. The second wheel seems to be rotating around the first one and the third wheel seems to be rotating around the second one. Here's the code for the drawing of the wheels:
    : [code]
    : /*=====DRAW FIRST(LEFT) WHEEL======*/
    : wheel1.setX(15);
    : wheel1.setY(110);
    :
    : glColor3f(0.0f,0.0f,1.0f); // set the drawing color to blue
    :
    : glTranslatef(15+tx+wheel1.getX(), 15+ty+wheel1.getY(), 0.0); //translate
    : if(direction) //moving forwards
    [b]glPushMatrix();[/b]
    : glRotatef(-angle, 0.0, 0.0, 1.0); //rotate needed amount
    [b]glPopMatrix()[/b]

    : else //moving backwards
    : glRotatef(angle, 0.0, 0.0, 1.0); //rotate needed amount
    : glTranslatef(-(15+tx+wheel1.getX()), -(15+ty+wheel1.getY()), 0.0); //translate back
    :
    : glTranslatef(wheel1.getX()+tx,wheel1.getY(), 0); //translate to position of wheel1
    :
    : glBegin(GL_POLYGON);
    : for(i=0; i<wheel1.getNumVertices(); i++) {
    : glVertex2i(wheel1.getPointX(i), wheel1.getPointY(i));
    : }
    : glEnd();
    :
    : glTranslatef(-(wheel1.getX()+tx),-(wheel1.getY()),0); //translate back
    : if(direction)
    [b]glPushMatrix();[/b]
    : glRotatef(angle, 0.0, 0.0, 1.0); //rotate back
    [b]glPopMatrix();[/b]
    : else
    [b]glPushMatrix();[/b]
    : glRotatef(-angle, 0.0, 0.0, 1.0); //rotate back
    [b]glPopMatrix();[/b]
    :
    : glFlush();


    //////////////DO THE SAME PUSH AND POP FOR THE REST ///////////////////
    :
    :
    :
    : /*=====DRAW SECOND(MIDDLE) WHEEL=====*/
    :
    : wheel2.setX(145);
    : wheel2.setY(110);
    :
    : glColor3f(0.0f,0.0f,1.0f); // set the drawing color to blue
    :
    : glTranslatef(15+tx+wheel2.getX(), 15+ty+wheel2.getY(), 0.0);
    : if(direction) //moving forwards
    : glRotatef(-angle, 0.0, 0.0, 1.0); //rotate needed amount
    : else //moving backwards
    : glRotatef(angle, 0.0, 0.0, 1.0); //rotate needed amount glTranslatef(-(15+tx+wheel2.getX()), -(15+ty+wheel2.getY()), 0.0);
    : glTranslatef(wheel2.getX()+tx,wheel2.getY(), 0);
    :
    : glBegin(GL_POLYGON);
    : for(i=0; i<wheel2.getNumVertices(); i++)
    : glVertex2i(wheel2.getPointX(i), wheel2.getPointY(i));
    : glEnd();
    :
    : glTranslatef(-(wheel2.getX()+tx),-(wheel2.getY()),0); //translate back
    : if(direction)
    : glRotatef(angle, 0.0, 0.0, 1.0); //rotate back
    : else
    : glRotatef(-angle, 0.0, 0.0, 1.0); //rotate back
    :
    :
    : /*=====DRAW THIRD(RIGHT) WHEEL=====*/
    :
    : wheel3.setX(245);
    : wheel3.setY(110);
    :
    : glColor3f(0.0f,0.0f,1.0f); // set the drawing color to blue
    :
    : glTranslatef(15+tx+wheel3.getX(), 15+ty+wheel3.getY(), 0.0);
    : if(direction) //moving forwards
    : glRotatef(-angle, 0.0, 0.0, 1.0); //rotate needed amount
    : else //moving backwards
    : glRotatef(angle, 0.0, 0.0, 1.0); //rotate needed amount glTranslatef(-(15+tx+wheel2.getX()), -(15+ty+wheel2.getY()), 0.0);
    : glTranslatef(-(15+tx+wheel3.getX()), -(15+ty+wheel3.getY()), 0.0);
    :
    : glTranslatef(wheel3.getX()+tx,wheel3.getY(), 0);
    :
    : glBegin(GL_POLYGON);
    : for(i=0; i<wheel3.getNumVertices(); i++)
    : glVertex2i(wheel3.getPointX(i), wheel3.getPointY(i));
    : glEnd();
    :
    : glTranslatef(-(wheel3.getX()+tx),-(wheel3.getY()),0); //translate back
    : if(direction)
    : glRotatef(angle, 0.0, 0.0, 1.0); //rotate back
    : else
    : glRotatef(-angle, 0.0, 0.0, 1.0); //rotate back
    :
    : glFlush();
    : [/code]
    : If anyone has any ideas why it's not working it would be greatly appreciated...
    : Thanks,
    : Jay
    :

Sign In or Register to comment.