Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make billiards in Box2D and prevent balls from getting stuck on the walls?

Tags:

physics

2d

box2d

As a quick overview: I'm trying to make a game of pool using Box2D as a basis. Now my question:

How should I set up the billiard balls and edges so that they act normally? The balls sometimes seem to stick to the edges for no apparent reason. I've got four static wall objects with a restitution of 1 around the edges of the table.

Each ball has the following properties:

friction: 0.3
restitution: 0.3
density: 58.474822 (kg/m^2)
radius: 0.028575 (m)

and the cue ball has a density of 101.356358 (kg/m^2)

When applying an impulse to the cue of 1.2 kg-m/s, the ball seems to move at a normal pool ball speed, and bounces off the walls mostly correctly. However, sometimes when a ball hits a wall it doesn't bounce off at all, it just stops, or just continues to travel along the wall. This looks weird and seems incorrect. Is there a better way to set this up?

like image 899
Ed Marty Avatar asked Jun 02 '10 23:06

Ed Marty


1 Answers

Box2D is designed to work best with length units between 0.1 and 10 (meters if you will), and the more you step outside this range, the more susceptible it becomes to numerical inaccuracies.

Try scaling up your system so that the ball has radius 1.0 and scale the rest accordingly. You can keep the material properties as they are but the impulse needs to be upscaled.

The sticking might also be caused by the body going to sleep when its speed goes below some threshold. You can prevent this by setting allowSleep to false in the b2BodyDef structure. But be aware that bodies that never go to sleep consume a lot more CPU time than bodies that do, so this should be used with care.

like image 176
Staffan E Avatar answered Sep 24 '22 18:09

Staffan E