I have a walking stick:

enter image description here

Some information:

  • The handle is fixed, it cannot move
  • The bottom part can move

Using the mouse click position and mouse movement, how can I rotate this stick with OpenGL?

  • \$\begingroup\$ I removed irrelevant text from your question, but it remains vague. To improve it: 1) Add an image of your stick being rendered, 2) add more useful "information": is the stick being held by a character, do you want a physics simulation, or just run the stick through an animation loop, 3) try to explain more carefully what mouse clicks and moves should do to the stick (preferably with a drawing), and 4) explain what you tried to fix this problem. \$\endgroup\$ – Eric Nov 16 '12 at 9:38
  • \$\begingroup\$ Try to make question titles as generic as possible. It helps for people searching in the future. I doubt "walking stick" is common enough to be searched for. \$\endgroup\$ – MichaelHouse Nov 17 '12 at 3:32

Generally, to rotate an object you'll want to use a rotation matrix. However, this will rotate your object around its origin - you want to rotate it around the handle.

So, if the handle isn't at the origin, you will want to apply a transformation matrix that moves the stick so that the handle is at the origin, then apply your rotation around the handle, and then possibly apply another transformation to position the stick in the world again.

Now, how you want to rotate your stick is entirely up to you, you could just map x/y movement to the theta/phi angles of a spherical coordinate system.

| improve this answer | |
  • \$\begingroup\$ (T) * (R) * (T^(-1)) \$\endgroup\$ – Gustavo Maciel Nov 16 '12 at 16:07
  • \$\begingroup\$ I suppose so :) \$\endgroup\$ – user13213 Nov 16 '12 at 16:58


glTranslatef(250,250,0.0); // 3. Translate to the object's position.

glRotatef(angle,0.0,0.0,1.0); // 2. Rotate the object.

glTranslatef(-250,-250,0.0); // 1. Translate to the origin.

// Draw the object


| improve this answer | |
  • 1
    \$\begingroup\$ You should really explain why this code works and the general idea behind how it's working. \$\endgroup\$ – MichaelHouse May 6 '14 at 22:22
  • \$\begingroup\$ This worked, but I haven't learned a thing \$\endgroup\$ – The Coding Wombat Jun 5 '18 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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