# What is the proper way to maintain the angle of a gun mounted on a car?

So I am making a simple game. I want to put a gun on top of a car. I want to be able to control the angle of the gun. Basically it can go forward all the way so that it is parallel to the ground facing the direction the car is moving or it can point behind the car and any of the angles in between these positions.

I have something like the following right now but its not really working. Is there an better way to do this that I am not seeing?

``````#This will place the car
glPushMatrix()
glTranslatef(self.position.x,1.5,self.position.z)
glRotated(self.rotation, 0.0, 1.0, 0.0)
glScaled(0.5, 0.5, 0.5)
glCallList(self.model.gl_list)
glPopMatrix()

#This will place the gun on top
glPushMatrix()
glTranslatef(self.position.x,2.5,self.position.z)
glRotated(self.tube_angle, self.direction.z, 0.0, self.direction.x)
print self.direction.z
glRotated(45, self.position.z, 0.0, self.position.x)
glScaled(1.0, 0.5, 1.0)
glCallList(self.tube.gl_list)
glPopMatrix()
``````

This almost works. It moves the gun up and down. But when the car moves around, the angle of the gun changes. Not what I want.

-
You mean to say you want the angle of the gun to be relative to the angle of the car? So if the gun is parallel to the body of the car and pointed toward the front of the car, when the car goes up hill or whatever, the gun is still pointed toward the front and parallel to the car? – Byte56 Apr 1 '12 at 7:27

Make sure your glCallist does not include any kind of transforms, then

``````#This will place the car
glPushMatrix()
glTranslatef(self.position.x,1.5,self.position.z)
glRotated(self.rotation, 0.0, 1.0, 0.0)
glScaled(0.5, 0.5, 0.5)
glCallList(self.model.gl_list)

#This will place the gun on top
glPushMatrix()
glTranslatef(0, 1, 0)
glRotated(updownangle, 1, 0, 0)
glRotated(leftrightangle, 0, 1, 0)
glScaled(1.0, 0.5, 1.0)
glCallList(self.tube.gl_list)
glPopMatrix()

glPopMatrix()
``````
-