### How do I make a ball stop wit friction? (For a golf type game)

In physics, friction is accounted for using a coefficient. Suppose that you have a ball rolling along the ground, and that you treat it like a box sliding on the ground (good enough).

The ball's position as a function of its acceleration allows you to take friction into consideration before it starts moving.

Let integral( t0, t, F ) be a method that integrates function F from t_0 to t. Let x ( t ) be the position of the ball, and let a and v be acceleration and velocity, respectively.

x ( t ) = integral ( t0, t, v ( t ) ) + x_0 v ( t ) = integral ( t0, t, a ( t ) ) + v_0 a ( t ) = ( some function in your game logic )

Now, let c be coefficient of friction. Then this becomes...

a ( t ) = c * ( some func )

The coefficient should be greater than zero and less than one. So, a coefficient of friction that counters 50% of acceleration is 0.5.

This only determines whether a ball starts moving. In fact, this is what is used if you have a sliding object and you want to see if you're applying enough acceleration to overcome friction. But you need to slow down and stop a ball.

So...

1. Have two states for the ball: in motion and at rest.
2. When the ball is in motion, decrement its velocity by integral ( t0, t, c ). In this case, friction is acting as an acceleration in the opposite direction, but it's exactly equal to the same friction that must be overcome to get a ball moving.

Since you have different surfaces, this can be written using a method c ( x ), which returns a coefficient of friction appropriate for the surface under the ball's current position.

Now, how exactly you encode different coefficients or provide acceleration is up to you to figure out in your game logic. But if you write this stuff correctly, then it will do the rest for you.

Note that your integration should be applied only once a magnitude is available for acceleration.

Pardon my abuse of c. Usually it would be light speed, but we defined it differently here just to explain this.

If you have difficulty writing your integration methods, here's what happens when you integrate a scalar. Let s be some scalar, and let integrate ( s, t ) mean "integrate s with regard to t).

F_0 = s_0 F_1 = integrate ( s_0, t ) = s_0 * t + s_1 F_2 = integrate ( s_1, t ) = ( 1 / 2 ) s_0 * t2 + s_1 * t + s_2

Notice that every time you integrate, there's a new scalar involved. That's because we're doing the opposite of derivation and the derivative of a scalar is zero. So, when you integrate acceleration, you have to add in an initial velocity the ball already had (suppose it's hit while already in motion, or more likely, the grade of the incline it's on increases).

Similarly, when you integrate velocity to get position, you have to add an initial position.

In each of these cases, your game logic should tick() your calculations, so that the x_0 and v_0 for any tick is the x(t) and v(t) resulting from the prior tick.

If this confused you, fake it by simply decrementing velocity by a set amount each tick.