开发者

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

开发者 https://www.devze.com 2023-01-02 15:16 出处:网络
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

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 开发者_StackOverflow社区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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消