# How do I implement friction in a billiards game?

I'm having trouble implementing friction in a billiards game.

Currently, the ball only slows on collision with the edges, but I also want some friction with the background.

How can I implement friction? (I'm using PhysicsJS.)

What you're looking for is some dampening function, i.e. friction with the table.

Dampening is actually a rather easy function:

v *= factor;


Where factor is a number between 1 (no dampening) and 0 (immediate halt).

The actual number for factor depends on your surfaces. For example, 0.99 could be ice, 0.9 could be some grass, 0.8 could be sand.

You'll have to experiment a bit till you get the numbers the way you want.

As for PhysicsJS: I only had a quick look at the documentation and I think the best way to solve this would be modifying the balls' acceleration directly. I think you can "subclass" bodies so it shouldn't be too hard to create a body that's always going to slow down on its own.

• But It won't decreasing to infinity? It will be still smaller and smaller number (1, 0.1, 0.001, ...) or there's some limit of JS or int where it be 0? I need to somehow detect the ball just stoped. Apr 21, 2014 at 12:31
• I just tutored someone on physics, so I thought I would add my two cents. There are two types of friction, kinetic and static. When your ball is moving, you are using kinetic friction, tl;dr, you can use the v *= factor for that, however, there should be some speed at which the speed is small enough for static friction to completely stop it, so put a stop if(speed < 0.001) speed = 0; moving = false, etc. Otherwise the ball will keep on moving and moving (very slowly mind you), and you won't really know when it is supposed to stop. Apr 21, 2014 at 15:51
• Yea, I succesfully try to use this solution after I wrote the comment. I set some limit after it the moving is done and the ball is stopped. Apr 21, 2014 at 18:11

After looking through the documentation for PhysicsJS I couldn't really find anything but I would access your velocity vector and over time reduce that velocity to simulate friction.

//Java (yes i know its not Javascript)
public void update(){
if(vx > 0){
vx -= 0.01f;
}
if(){
vy -= 0.01f;
}
}


Thats how I would do it in a Java game.

• Small detail to think about it. What happens if my velocity is 0.009? The ball will never stop, since after subtracting 0.01 you're stuck at -0.001, which you'll never remove again. Apr 21, 2014 at 7:27
• I've formerly solving this by detection if vx and vy isn't negative now (my first solution without some physics engine), but still is there problem how to stop the ball symetricly. It isn't good solution. Apr 21, 2014 at 12:37
• Well there are two ways I fix this, one being use and if statement to test if the value is within 0.001 and -0.001 then setting it to zero (or something like that) or just subtract in a friction value that will always equal zero to the case in hand. (This not being possible in most cases) May 4, 2014 at 2:20