Skip to main content
gamedev.net
🔒 Locked

Rolling Ball

Started by Bretttido 5 replies 450+ views
Original Post
Bretttido
Bretttido
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)
Terran Marine
Terran Marine
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.
Gillies
Gillies
...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.
masonium
masonium
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
Peeper
Peeper
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?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.