View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Rolling Ball

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1Bretttido  Members

Posted 17 October 2001 - 12:54 AM

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)

### #2Terran Marine  Members

Posted 18 October 2001 - 05:19 AM

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.

### #3Gillies  Members

Posted 18 October 2001 - 12:15 PM

...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.

### #4masonium  Members

Posted 18 October 2001 - 03:52 PM

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

### #5Peeper  Members

Posted 18 October 2001 - 04:21 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?

### #6masonium  Members

Posted 23 October 2001 - 01:22 PM

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

### #7Anonymous Poster_Anonymous Poster_*  Guests

Posted 24 October 2001 - 02:16 AM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.