views:

176

answers:

4

I have a rotating sphere that the user rotates by applying a virtual force, like a virtual accelerator. I want to be able to simulate a nice momentum effect so that when they lift off the accelerator the ball winds down in speed in a natural and realistic way, as if due to friction and/or gravity. I don't want to get into any deep physics equations. I'd like to do this quick so if I could find a code sample that shows how to do this, or even a page of formulas clear enough that I could encode, that would be great.

I'd like a formula that has one or two adjustable coefficients that I could tune to make the ball decelerate faster or slower depending on my needs. I don't want to get into anything heavy like an open source physics library or the like. Just something simple.

I'm using Delphi 6 Pro but I also know C/C++, Basic, Java, and Javascript.

+2  A: 

Velocity is change in displacement. Acceleration is change in velocity.

Gravity or friction just causes an acceleration (possibly negative).

So all you need to do is apply a negative acceleration when they do not activate the accelerator.

So lets assume you have an angle that is changing. Applying the accelerator increases the amount the angle changes by each iteration or time step. If your angle is t and your change in angle is called dt (angular velocity) then when the accelerator is applied you'll have:

t = t + dt
dt = dt + a

where a depends on how much force you're applying, or how much they've 'pressed' the accelerator (i.e. this is the acceleration).

You'll probably want to limit dt (i.e. speed of rotation) - if you only want to spin in one direction you'll have an upper positive limit and a lower limit of 0. If you want both directions, you can have a lower negative limit and an upper positive limit.

All you need to do is make a some negative number when the accelerator is not applied (if dt is positive - make a positive if dt is negative), and ensure you don't 'wrap' (i.e. make dt 0 when it gets near 0).

sje397
+1  A: 

As long as you are not solving stellar problems, I don't see how gravity has much to do with decelerating the rotation.

Friction is almost proportional to the current rotation speed (actually the speed on the surface of the sphere).

So a formula for the current rotation speed over time w(t) may be something like this:

w(t) = w0*exp(-c*(t - t0))

with t0 being the time the friction starts, wt being the rotation speed at that time. The coefficient c > 0 determines how fast the speed will decrease - the higher c, the faster the speed will go down. This formula is valid for all t >= t0.

Note that for t = t0 the exp function returns 1 and you get the initial speed, while for t -> ∞ the exp function (and so the resulting speed) returns -> 0 (the minus in front of the c guarantees this).

Uwe Raabe
You have to be careful about the choice of values for `a` and `b` - e.g. if `a` > 0 and `b` > 1, speed will increase over time (supposing it's positive to start with, which it should be unless we're talking velocity).
sje397
I cannot agree here: you can see that the second term is subtracted and the Power function is the denominator? For this to work properly, a > 0 and b > 1 is a must. So you are right that I didn't specify better constraints for a and b, but your conclusion is just wrong. I changed the constraints.
Uwe Raabe
As t increases, Power(b, t-t0) increases (when b > 1) - which decreases what you subtract from w(t0), meaning w(t) grows.
sje397
Point taken! I changed the formula.
Uwe Raabe
+2  A: 

It has been some time ago, but according to my dynamics study books the Mass Moment of Inertia for a sphere is defined as I=(2/5)m*r^2. m is the mass, r is the radius of the sphere (all in SI untis). On this page you'll find some examples to use the mass moment of inertia to calculate deceleration of the sphere as a result of the applied negative torque. This toqrue is a result of the friction. Since you are not defining the material of the surface of the sphere and the surroudings you cannot calculate the friction and will have to choose a good force yourself.

birger
I've scanned that resource and that's an excellent reference on torque. I have not gone into in depth yet so pardon this question if it is answered in the text, but is there a place in the formulas to "plug in" the drag coefficients of different materials to dynamically provide/change the friction?
Robert Oschler
I think you should just apply the formula and experiment with values for the torque untill you have a realistic result. There won't be much difference between different materials, as long as their mass is comparable.
birger
A: 

You've already accepted an answer, but I'll put in my piece.

I'll assume you already know how to make the sphere spin at a constant rate, and how to make it accelerate under the applied torque. Making it decellerate is just a matter of applying another torque, one that must be calculated.

When an object slides on a solid surface, the rate of deceleration is constant. The force is in the direction opposite to motion, and its magnitude depends on a couple of things but not speed. When the object comes to a full stop, the force vanishes. The same applies to a globe turning on a solid pivot.

When an object passes through a fluid, the force of decelleration increases with speed, so the faster the object the greater the drag. As the object slows down, decelleration grows weaker, and the object keeps slowing down but never stops. This describes a globe spinning in air or water. At reasonably high speeds, drag is proporional to v2, and at very low speeds it is proportional to v (I don't know about the transition between these domains).

So I suggest t = -a wb, where w is angular velocity. The parameter a is the strength of the friction, and b describes the kind of decelleration; b=0 is like friction on a solid pivot, b=2 is like spinning in air, and b=1 is like rotating in syrup. Other values of b may or may not look realistic or be realistic.

Beta