Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

rather than just moving a sphere across a plane, I want to make it roll like a ball. What is the simplest way of doing this? I assume it will have something to do with the circumference of the ball and rotating + moving it at a calculated rate.

What is the relevant maths here?

share|improve this question
add comment

3 Answers

up vote 15 down vote accepted

The simplest way is to calculate how much ball is moved and rotate the ball that much divided by it's radius!

And the reason: we all know how they calculate PI. imagine a circle with radius of one unit, it's perimeter is 2*PI as shown in this picture:

circle

Now in your case the ball may move in any direction. but it's movement speed is always a 2d vector (note that it's rolling not flying!) and to rotate a ball you need a 2d vector. these 2d vectors have the same value except you need to divide speed vector by sphere diameter.

and as a sidenote, I've assumed that rotating the ball doesn't need any force. if you want a real physical simulation thing get a little more harder than this.

share|improve this answer
    
Nice answer but could be better. –  Gustavo Maciel Feb 1 '12 at 18:40
    
@Gtoknu he asked for simplest not best! –  Ali.S Feb 1 '12 at 18:42
2  
I don't mean if it is the best aproach, I mean the quality of the answer. It is a nice one, but you could give more details about how to achieve it. Not just a bunch of theory. –  Gustavo Maciel Feb 1 '12 at 18:58
9  
An answer doesn't have to show you the codez to be a good answer. –  Jimmy Feb 1 '12 at 22:46
1  
@bummzack: no exactly by the radius, as I explained you need a value between 0..2*pi but dividing distance by circumference will result in a number between 0..1! check image again, I hope you understand since my english is not good enough to explain more! –  Ali.S Feb 2 '12 at 9:58
show 3 more comments

Gajet's answer does an excellent job of covering the '2d' case, where your ball moves in one dimension; if you want to roll it across a plane then things get decidedly more complicated, but still very manageable. Diaqgram of a rolling ball

Here the ball is rolling across a plane in the direction of the green arrow, with (instantaneous) velocity V; the rotation is around the axis of the red arrow, and the normal to the plane is represented by the blue arrow (N). The instantaneous axis of rotation can be found by finding the cross product of the velocity vector with the normal vector, R=VxN; by combining this with something like Gajet's method (using V*t as the amount moved, where t is the time from the last frame to this one) you can construct a quaternion q representing the rotation the ball underwent over the course of the frame: basically, normalize your axis-of-rotation vector R to get R_n, find the amount of rotation Theta as V*t/2*pi*b, where b is the ball's radius, and build the quaternion q=(R*sin(Theta/2), cos(Theta/2)). Then you can compose this quaternion with the ball's previous rotation to get its new rotation.

But be careful - for most rolling-ball style games, this approach is actually exactly backwards! For instance, if you determine your ball's rotation by how far it's moved in contact with the ground, then you'll never see any 'spin' in the ball while it's in the air. Instead, I've found it works better to go the other way: keep track of your ball's angular velocity, have the player's controls actually (behind the scenes) control that angular velocity, and then while the ball is in contact with a surface, use the presumption that you're rolling without slipping to 'unroll' the motion like in Gajet's diagram and figure out where your ball ought to move to next frame. This gives you better physics because it gives you the option to roll with slipping, or to spin along the ground, or a number of other subtleties.

share|improve this answer
    
Isn't this going towards the more complicated inertia tensor approach? +1 –  teodron Jun 4 '12 at 21:07
1  
@teodron It's definitely leaning thataway; it really comes down to what the poster is after in the end (something that's not wholly clear in the original question). 'Background animation of a small ball rolling around' or something like the ball beneath the circus-bear in the Okami games probably barely needs something like this; any marble-rolling game will want the heavy-duty physics approach. –  Steven Stadnicki Jun 4 '12 at 21:15
add comment

For 2D could use something like:

totalMovementVector = previousPosition - currentPosition;
distanceMoved = totalMovementVector.magnitude;
circumfrenceOfCircle = circleRadius * 2 * PI;
amountToRotateInDegrees = distanceMoved / circumfrenceOfCircle * 360;
thingThatIsBeingRotated.rotation += amountToRotateInDegrees;

if you don't have vectors i.e. flash

totalXMovement = previousX - currentX;
totalYMovement = previousY - currentY;
distanceMoved = squareRoot((totalXMovement * totalXMovement) + (totalYMovement * totalYMovement));
circumfrenceOfCircle = circleWidth * PI;
amountToRotateInDegrees = distanceMoved / circumfrenceOfCircle * 360;
thingThatIsBeingRotated.rotation += amountToRotateInDegrees;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.