views:

265

answers:

1

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?

+1  A: 

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.

Staffan E
Well, they definitely aren't asleep, since they still move along the edge of the wall.
Ed Marty
Yeah, I meant the ones that stopped. ;) I had problems with premature sleeping myself until I rescaled my coordinates. Then it seemed to go away.
Staffan E
It looks much better now!
Ed Marty