如何在Box2D中打台球并防止球卡在墙上?

发布于 2024-09-03 19:19:05 字数 405 浏览 5 评论 0原文

快速概述:我正在尝试使用 Box2D 作为基础制作一款撞球游戏。现在我的问题是:

我应该如何设置台球和边缘以使它们正常工作?有时,球似乎无缘无故地粘在边缘上。我有四个静态墙壁对象,桌子边缘的恢复值为 1。

每个球具有以下属性:

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

母球的密度为 101.356358 (kg/m^2)

当向球杆施加 1.2 kg-m/s 的脉冲时,球似乎以正常台球速度移动,并且从墙上弹起的效果基本正确。然而,有时当球撞到墙壁时,它根本不会反弹,它只是停止,或者只是继续沿着墙壁移动。这看起来很奇怪而且似乎不正确。有更好的方法来设置这个吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小霸王臭丫头 2024-09-10 19:19:05

Box2D 的设计最适合使用 0.1 到 10 之间的长度单位(如果愿意,可以是米),超出此范围越多,就越容易出现数值不准确的情况。

尝试扩大您的系统,使球的半径为 1.0,并相应地缩放其余部分。您可以保持材料属性不变,但冲量需要放大。

当速度低于某个阈值时,身体进入睡眠状态也可能导致粘滞。您可以通过在 b2BodyDef 结构中将 allowSleep 设置为 false 来防止这种情况发生。但请注意,从不进入睡眠状态的身体比进入睡眠状态的身体消耗更多的 CPU 时间,因此应谨慎使用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文