3
\$\begingroup\$

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?

\$\endgroup\$

3 Answers 3

15
\$\begingroup\$

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.

\$\endgroup\$
8
  • \$\begingroup\$ Nice answer but could be better. \$\endgroup\$ Commented Feb 1, 2012 at 18:40
  • \$\begingroup\$ @Gtoknu he asked for simplest not best! \$\endgroup\$
    – Ali1S232
    Commented Feb 1, 2012 at 18:42
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ Commented Feb 1, 2012 at 18:58
  • 10
    \$\begingroup\$ An answer doesn't have to show you the codez to be a good answer. \$\endgroup\$
    – Jimmy
    Commented Feb 1, 2012 at 22:46
  • 1
    \$\begingroup\$ @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! \$\endgroup\$
    – Ali1S232
    Commented Feb 2, 2012 at 9:58
6
\$\begingroup\$

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 Vt 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 Vt/2*pib, where b is the ball's radius, and build the quaternion q=(Rsin(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.

\$\endgroup\$
2
  • \$\begingroup\$ Isn't this going towards the more complicated inertia tensor approach? +1 \$\endgroup\$
    – teodron
    Commented Jun 4, 2012 at 21:07
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ Commented Jun 4, 2012 at 21:15
0
\$\begingroup\$

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;
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .