Simulating a "falling chopstick" with glRotate()


I’m writing a program to simulate a falling chopstick. And now I stuck at the easiest part…

I’ve completed:

  • All the physics stuff; Torque, gravity, angular acceleration and displacement, game loop, Timestep, etc.
  • I have a chopstick drawn on the screen as a quad starting at (0, 0, 0) and pointing towards the screen.
  • angleX and angleY variables are solved and ready to use.

Now I got stuck at:

  • using the angleX and angleY with glRotate() to simulate the falling chopstick.

The chopstick fall OK on X-axis when I use only this
gl.glRotatef(angleX, 0, 1, 0);

and on Y-axis when I use only this
gl.glRotatef(angleY, 1, 0, 0);

*** Problem is here ***
But when I use them together
gl.glRotatef(angleX, 0, 1, 0);
gl.glRotatef(angleY, 1, 0, 0);

It just doesn’t fall diagonally. At both angleX and angleY at 90 degree, the chopstick lies completely on Y axis.

However, I understand the reason well. It’s because using glRotate 2 times like that. At 90 degree, the second glRotate will just rotate the chopstick around itself…

Please guide me on how should I use these 2 variables to properly simulate a falling chopstick.

Thank you in advance!

Try reverse order :

gl.glRotatef(angleY, 1, 0, 0);
gl.glRotatef(angleX, 0, 1, 0);

I often have to do this, reverse transformation order compared to my intuitive feeling.

Hi Zbuffer,
Thank you for your fast response.

Unfortunately I’ve tried that and the final result(at both angles 90 degree) is the chopstick lying on X-axis(instead of Y-axis before reversing glRotate order). :’(

I think you’re going to have to use quaternions.

Here’s the problem. Imagine the chopstick pointing along the x-axis falling down the z-axis. As it’s falling, you rotate it along z which gives it a bit of a spin just as if it were spinning around on a xy-plane kitchen table. You also want it to rotate along the y-axis like if it were catching air it might point up and down so it might stick into the ground when it landed. The problem is one rotation affects the other. You can use quaternions or if your problem is simple enough do this.

Choose one axis to rotate along. Let’s say z in this case and do your rotation, which may bring the the stick “more towards” the y-axis. Right now you’re rotating what you just applied in z against the y-axis, which isn’t what you want. You want to rotate against where your new pseudo-y-axis is to point it down. This means you just have to rebuild your axis.

  1. Do you glRotate along one axis
  2. Calculate your new axis by applying the rotation of the first axis (you do this yourself, not in OpenGL) just like rotating a point
  3. Do another glRotate along the new axis you just calculated

Make sense?

Here’s a horrendously bad sketch I did in gimp. On the left is what you’re doing and on the right is what I think you want to do.