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.)
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.Sign up to join this community
What you're looking for is some dampening function, i.e. friction with the table.
Dampening is actually a rather easy function:
v *= factor;
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.
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.
Thats how I would do it in a Java game.