Archived

This topic is now archived and is closed to further replies.

Rolling Ball

This topic is 6111 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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)

Share on other sites
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.

Share on other sites
...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.

Share on other sites
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

Share on other sites
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?

Share on other sites
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

Share on other sites
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.

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 33
• 41
• 23
• 13
• Forum Statistics

• Total Topics
631744
• Total Posts
3001995
×