[SDL] SDL OpenGL rotating cube (where is it?)
Justin Coleman
jmcoleman at gmail.com
Mon Apr 2 13:15:54 PDT 2007
On 4/2/07, Serjan Pruis <serjan at quicknet.nl> wrote:
<snip>
> What I want to know is how to know when a full image (a cube side) is on the
> screen and so that I can stop rotating for a short period of time on an
> image. And after a while continue rotating.
<snip>
Here's how I would do it. This is off the cuff, I don't have a
compiler handy at the moment.
1. Keep track of which direction the user wants it to go. One
direction will add to "rotate", the other will subtract from it, and a
third, "pause", will hold it still (add 0.0 instead of 0.1). An enum
might be useful for this, with a "left, right, pause" value set.
2. Keep your rotation values small enough to work with. glRotatef()
takes degrees, so you want to clamp the value between 0.0 and 360.0.
If it goes below 0.0, add 360.0 to it, and if it goes above 360.0,
subtract 360.0. Only do this when you actually modify the value of
"rotate".
3. Now, if "rotate" is a multiple of 90 then you should have a face
towards the camera.
4. If you want user pause/move, simply keep track of the last
direction requested by the user, and add the corresponding value to
rotate. When the user pauses rotation, you'd be adding 0 to the
rotation angle, effectively holding it still.
If you want pause/unpause on user command, you'll need another
variable to store last requested direction. This way, when user
pauses, you store current movement direction and pause rotation. Then
when user unpauses you can go back to the last requested direction.
If you want auto-pause when a face is towards the camera, you'll
either need an unpause keypress or you'll need a timer. With this, you
watch the rotation angle every time it's updated. If it's 0, 90, 180,
270, or 360 you pause rotation as above, and set your timer to however
long you want it to stay paused. When the timer ends, restore the last
requested direction and it will rotate until the next face is towards
the camera.
HTH,
-Justin
More information about the SDL
mailing list