Rolling Ball

Started by
5 comments, last by Bretttido 22 years, 6 months ago
I''m trying to create a ball that can roll along the ground in any direction, and whose path can change. For it''s rotation to look correct, i need some knowledge of it''s current rotation and how to update it through each frame. Anyone have any ideas of how I might go about storing and updating this information? (I''m a bit weak on my linear algebra)
Advertisement
Try this:


void rotate_ball(float x, float y, float z)
{
glPushMatrix();

glRotatef(x, y, z);

draw_ball(); //Or whatever your ball drawing code is

glPopMatrix():

}

Then call rotate_ball() and replace x, y and z with the rotation values.
...except that those aren''t the parameters to glRotatef().

You''ll want to get a vector normal to your direction of travel to rotate the ball about - unless the ball can slip on the ground.

In the simplest case you can rotate the ball depending on how far it has moved since the last frame, about your vector normal to the direction it has travelled in.
You also have to make sure that it seems like the rotation of the ball is synchronized with it's movement. After rotating 360º, the ball will be 2 * r * pi. Therefore, after x degrees of rotation, the ball should move: (r = radius)

x * 2 * r * pi
--------------
360

You could have the ball rotate at a certain number of degrees per second or per frame. If it were per frame, you could do something like this (assuming the ball was rolling down the x-axis):

      void MoveBall(float degrees){     ball_angle += degrees;          /* check to see if ball_angle is over 360 degrees */          // move the ball     ball_pos += (degrees * 2 * ball_radius * PI) / 360;          glPushMatrix();          glTranslatef(ball_pos, 0.0f, 0.0f);          glRotatef(ball_angle, 0.0f, 0.0f, -1.0f);          DrawBall();     glPopMatrix();}    


The variable names should be pretty self explanatory.


Edited by - masonium on October 18, 2001 10:53:25 PM

Edited by - masonium on October 23, 2001 8:24:19 PM
Naive, newbie comment:

Would it be simpler to leave the vertex''s where they are and just adjust the texture coords of the verts to match the rotation that should have occured?
You don't really have to adjust the vertices manually if you're drawing a sphere through your own function or glutSolidSphere or something. glRotatef rotates the vertices for you.

Edited by - masonium on October 23, 2001 8:23:40 PM
Peeper: calling glRotatef doesn''t actually rotate the vertices. It updates the modelview matrix. The original vertices stay the same. During rendering, all vertices get multiplied by the matrix anyway, so apart from the one call to glRotatef, rotation really is free.

This topic is closed to new replies.

Advertisement