views:

113

answers:

3

Here is the draw function which draws the parts of the car, in this function car rims is checked and flag is checked, and i need to rotate the tire rim as i move the car. Something is not working since the rims are rotated but taken out from the car model, when i press up arrow key, but the car does move.

I also initialized self.fFlag = "false" in initialize function:

def on_draw(self):
    # Clears the screen and draws the car
    # If needed, extra transformations may be set-up here
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)


    for name in self.parts:
        colors = self.colors
        color = colors.get(name, colors["default"])
        glColor3f(*color)

        if (name == 'Front Driver tire rim') & (self.fFlag == "true"):
            bodyFace = self.mini.group(name)
            glPushMatrix()
            glRotatef(45,1,0,0)

            # Drawing the rim
            for face in bodyFace:
                if len(face) == 3:
                    glBegin(GL_TRIANGLES) 
                elif len(face) == 4:
                    glBegin(GL_QUADS) 
                else: 
                    glBegin(GL_POLYGON)
                for i in face:
                    glNormal3f(*self.mini.normal(i))
                    glVertex3f(*self.mini.vertex(i))
                glEnd()



            glPopMatrix()
            self.fFlag == "false"

        else:
            bodyFace = self.mini.group(name)
            for face in bodyFace:
                if len(face) == 3:
                    glBegin(GL_TRIANGLES) 
                elif len(face) == 4:
                    glBegin(GL_QUADS) 
                else: 
                    glBegin(GL_POLYGON)
                for i in face:
                    glNormal3f(*self.mini.normal(i))
                    glVertex3f(*self.mini.vertex(i))
                glEnd()



def on_key_release(self, symbol, modifiers):
    """Process a key pressed event.
    """

    if symbol == key.UP:
        # Move car forward
        # TODO

        glTranslatef(0,-1,0)
        self.fFlag = "true"
        self.on_draw()

        pass

Edited: I am trying to make the car rims to rotate when i press the up arrow key, which moves the car forward.

+1  A: 

You're almost certainly applying the rotation and transformation in the wrong order, so that the rim is rotated about some point other than the center of the tire.

You might try doing the rotation in the MODELVIEW matrix and the translation in the PROJECTION matrix.

Ben Voigt
"You might try doing the rotation in the MODELVIEW matrix and the translation in the PROJECTION matrix." Huh? Why? The projection matrix is for transforming eye space to screen space, isn't it?
LarsH
Translation of the entire scene can be expressed as a change of camera, can't it?
Ben Voigt
OK, you're talking about translating the car, not the tire? That might work if he never adds any other objects to the scene... But then it's still more complicated than using the MODELVIEW mx, since he has to map the car's movement vector in world space to the corresponding vector in view space...
LarsH
+1  A: 

In order to rotate a part about its own center, you need to translate it to the origin, rotate it, and translate it back.

So your

        glRotatef(45,1,0,0) # rotate 45 deg about x axis (thru the world origin)

needs to be preceded and followed by translations.

See the accepted answer to this question.

LarsH
+2  A: 

I would highly suggest posting this to the class forum. I don't think TJ would really like to see this, and its very easy to find.

user