Advertisement Jump to content
Sign in to follow this  

Rolling ball rotations

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm trying to write some opengl code to simulate a ball rolling. The game is essentially 2D (overhead orthogonal view), which simplifies things somewhat but I'm still having some problems. Calculating the position (based on intertia, etc) is straightforward enough, but rotating the object correctly is proving problematic.... I can obtain the euler angles required to correctly rotate the ball i.e for each axis, I do: (distance / circumference) * 360 However, I'm stumped when it comes to actually applying these rotations. Using just a succession of glRotatef calls results in only 1 of the rotations working as expected... which makes sense as the origin is changing after the rotation, of course... I've since then tried fetching the new axis vectors from the rotated matrix after performing a glRotatef and plugging them into the subsequent glRotatef calls as the origin, but this still doesn't get the results I'm looking for. I suspect that all of this is because I essentially want to perform each rotation in the global coordinate system (in order to display the ball rolling) and the code I've written so far is doing all the rotations in local coords. I've tried googling around for some tips on how to accomplish this, but I'm still not any wiser really..... Would certainly be grateful for some help with this! Thanks

Share this post

Link to post
Share on other sites
I know this has been covered on the forums before - you might try searching the archives for e.g. 'rolling ball' and see what comes up.

In short, you'll need to store the ball's orientation somehow (e.g. in matrix or quaternion form), and then update it incrementally from frame to frame.

You already know how to compute the angle of rotation for a given update given the ball's circumference and the distance traveled. Now you need the axis of rotation, which can be computed by taking the cross product of the ball's motion vector (i.e. velocity) and the surface normal (the global up vector in this case, since the simulation is in 2-d).

Once you've computed the axis and angle, you construct a matrix or quaternion from them and apply it to the current orientation to yield the new orientation (don't forget to normalize the result to prevent numerical drift).

That's the basic idea, but post back if you have further questions.

Share this post

Link to post
Share on other sites
Thanks for posting. I actually did do a search and found some interesting threads, but couldn't find anything that exactly addressed the issue I'm facing...

Your advice sounds good, although I'm still not entirely clear about a few things... Btw also, I should have mentioned that I'm technically not quite doing (distance / circumference) * 360, but actually using the player's position instead of 'distance' (as its an overhead game set on a single screen) to calculate absolute values for rotation, so that I wouldn't have to keep updating previous rotation values (which i belive can introduce errors over time..?)

Anyway, I think your advice makes sense.... just have to try and understand it a bit more i guess :)

Share this post

Link to post
Share on other sites
Grrr... Thanks for your help guys but I still haven't had any joy with this, so I''m giving up on this for the moment. I've spent all day trying to work it all out but just can't seem to nail it! Bit frustrating to be honest but will revisit it later, perhaps when I'm armed with a better understanding of tranformations, matrices, etc... perhaps I'm still a bit too much of a newbie really!

@DraganO: Did actually come across your post earlier (was one of the more useful ones) but was a little put off by the DirectX implementation, as I haven't worked with the API yet and thus was unwaware of the differences. That said, I did end up porting your code as one of the many things I tried (learning along the way that DirectX matrices are row major!) but still not getting correct results (perhaps due to in error elsewhere in my code...) Thanks anyway though!

Share this post

Link to post
Share on other sites
Hi again,

So... after tinkering with other game code for a couple of days, I returned to this problem with some fresh determination and, as it turns out, the code I wrote DOES seem to work!

Sort of, anyway :)

I previously had commented out the translation commands for positioning the ball (as it would include some other side-effects when testing, due to nature of the game) but, after adding them back in, I noted that the rotation does actually seem to be working correctly.. but only when the ball is rolling away from the centre of screen. Unfortunately, I'm stumped again as I can't seem to pinpoint a fix for this... as is typical for a newbie I suppose!

Any ideas? I've included the appropriate bits of code below for cleverer minds to have a look:

// 0,0 is screen center
float XX = (0 - playerX) * (0 - playerX);
float YY = (0 - playerY) * (0 - playerY);
float rotAngle = (XX + YY);
rotAngle = sqrtf(rotAngle);
rotAngle = (rotAngle / circumference) * 360;

GLfloat motionVec[3] = {playerX - previousPlayerX, 0, playerY - previousPlayerY};
GLfloat surfNormal[3] = {0,1,0}; // This will be changed later when supporting undulating surfaces
float rotAxis[3];
crossProduct(rotAxis, motionVec, surfNormal);

Quaternion tmpQuat;
tmpQuat.CreateFromAxisAngle(rotAxis[0], rotAxis[1], rotAxis[2], rotAngle);

GLfloat quatMat[16];
glTranslatef(playerX, 0, playerY);


Share this post

Link to post
Share on other sites
Original post by tago

What you've posted doesn't look right to me, but it would take me a while to confirm that it's incorrect (and even then there's too much code missing to say for sure).

So all I can really do is tell you how I'd do it. I would:

1. Store the ball's orientation using a quaternion or matrix.

2. Store the ball's position using a vector.

3. For each update, update the ball's position vector. Compute the direction and distance traveled, and derive the resulting axis and angle of rotation (it looks like you have this part right, more or less). Apply this axis-angle rotation to the orientation quaternion or matrix, and then normalize the quaternion or matrix to prevent drift.

4. When it's time to render, build a matrix from the orientation quaternion or matrix and the position vector, and upload it to OpenGL.

If there are specific aspects of the above that are unclear, post back and I'll try to explain in more detail.

[Edited by - jyk on January 27, 2009 9:24:12 PM]

Share this post

Link to post
Share on other sites
Finally sorted it out :) My mistake was basically thinking of the rotation in terms of absolute rotation values, i.e assuming the distance travelled from the center would always provide correct rotational values for that position. After switching to measuring the distance moved since the last update and constantly multiplying/normalizing the ball's orientation, it all works perfectly!

Anyway, thanks for the pointers, definitely helped! Dunno if you're the type to care about such things, but I 'rated' your profile accordingly anyway :)

Share this post

Link to post
Share on other sites
Hi Tago,

Would you be able to paste the code or email it to me (including the vector and quaternion math)? I am facing the same problem and it would be a huge help to me, thanks. Blue

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!