Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

So I have a "cannon" object (basically a long cylinder) and I want to be able to control its angle. The code I have now is:

    glPushMatrix()
    glTranslatef(self.position.x,2.5,self.position.z)
    glRotated(self.tube_angle, 1.0, 0.0, 1.0)
    glScaled(1.0, 0.5, 1.0)
    glCallList(self.tube.gl_list)
    glPopMatrix()

This rotates it around the axis, obviously. I want the bottom to stay in the spot it is (moving object) and just change the angle.

share|improve this question
1  
You can teach yourself :) blog.wolfire.com/2010/07/… –  Byte56 Mar 9 '12 at 3:09
2  
rotate around point P: (1) translate by -P (2) rotate around origin (3) translate by P (4) Profit!!! –  Jimmy Mar 9 '12 at 3:55

1 Answer 1

Since glRotated() rotates a specified angle around the desired axies (x and z in your example), you'll need to translate your object first. This is important.

Note: The rotation center is always considered (0,0,0).

Example: Rotate 20° About Point (0, 5, -10)

// Save the current display for other items.
glPushMatrix();

// Translate by the opposite of the desired point of rotation.
glTranslate(0, -5, 10);

// perform the desired rotation.
glRotate(20, 1, 0, 0);

// Put the object back where it was.
glTranslate(0, 5, -10);

// Now, draw the object with proper rotation!
...draw code...

// Finally, reset our transformation matrix to where we left it.
// It's the polite thing to do, after all.
glPopMatrix();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.