当物体被磁化时 box2d 抖动

发布于 2024-12-10 01:52:52 字数 739 浏览 1 评论 0原文

所以我有一些物体(我可以在运行时创建越来越多的物体),我需要它们被磁化到屏幕中心。让它在世界空间中为 (480/2 / WORLD_SCALE, 320/2 / WORLD_SCALE)。我是 box2d 的新手,所以也许有一种简单的方法可以实现这一点,但我正在尝试执行以下操作:

  1. 我减去每个身体的位置和屏幕中心位置 - 以获得我需要将身体移动到的方向。
  2. 标准化方向向量(我在第 1 页中找到的)
  3. 将此标准化向量乘以我的重力功率值
  4. 将向量(我在第 3 页中找到的)添加到当前身体的速度

所以之后我得到了什么我想要 - 物体确实会磁化到屏幕中心,我还添加了一些摩擦力和阻尼,以便物体可以停止。但是,正如我假设的那样,由于我的重力矢量是恒定长度 - 我无法强迫物体完全停止移动。它总是试图将自己移动到屏幕中心,并且它们可能的最小速度 - 是该重力矢量的长度。

如果我只有一个身体,当身体距离屏幕中心比重力大小更近时,我不会执行步骤 2,3。但我无法将物体的位置绑定到磁场中心,因为两个物体永远不会占据精确的屏幕中间位置,因为它们会相互碰撞。所以我不能使我的重力矢量可变,并且玩弄任何摩擦力都不起作用,也许一些步骤黑客可以帮助,但我认为我需要的是实现某种弹跳反作用力或类似的东西。

我是 box2d 的新手,对物理不太了解。

同样重要的是我使用 SetLinearVelocity 方法设置物体速度。

也许我只是错过了一些东西,也许有一些 box2d 原生的方法来实现这一点。任何帮助表示赞赏。

So i have some bodies(which i can create more and more at runtime), and i need them to be magnetized to the screen center. Let it be a (480/2 / WORLD_SCALE, 320/2 / WORLD_SCALE) in the world space. I am new to box2d so maybe there is an easy way to achieve this, but i am trying to do it as following:

  1. I subtract each bodie's position and screen center position - to get a direction to where i need to move my body to.
  2. Normalize the direction vector(which i've found in p.1)
  3. Multiply this normalized vector to my gravity power value
  4. Add the vector(that i've got in the p. 3) to the current bodie's speed

So after that i get what i want - bodies do magnetize to the screen center, i also add some frictions, and dampings so that bodies could ever stop. But, as i assume, because of my gravity vector to be constant length - i cannot force the bodies to stop moving completely. It always tries to move itself to the screen center and the smallest possible speed for them - is the length of this gravity vector.

If i only had one body, i would not perform steps 2,3 when the body is closer to the screen center then the gravity size is. But i can not bind bodie's position to the magnetic field center because the two bodies will never take the exact middle screen place since they will collide with each other. So i cannot make my gravity vector variable, and playing around with any frictions do not do the trick, maybe some step hack could help, but i think what i need is to implement some sort of bouncing counter force or something of that kind.

I am new to box2d, and not very good with physics.

What may also be important is that i set the bodies speed with the SetLinearVelocity method.

Maybe i am just missing something, maybe there is some native for box2d way to achieve this. Any help appreciated.

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

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

发布评论

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

评论(1

神妖 2024-12-17 01:52:52

确实通过改变世界规模解决了这个问题。 “当碰撞速度较小时,Box2D 也会使用非弹性碰撞。这样做是为了防止抖动。” http://www.box2d.org/manual.html
读完后,我发现我应该使加速度矢量足够小,以便 box2d 使用这种非弹性碰撞并防止其以“本机”方式抖动。因此,将 WORLD_SCALE 增加 4 倍可以使物体与屏幕中心之间的距离足够大。尝试使用新的引力和摩擦力数字就成功了。

问候,伊戈尔

Did solve this issue by changing world scale. "Box2D also uses inelastic collisions when the collision velocity is small. This is done to prevent jitter." http://www.box2d.org/manual.html
After reading that i figured out that i should make my acceleration vector small enough so that box2d used this inelastic collisions and prevent its jittering in "native" way. So increasing WORLD_SCALE by 4 times made the distances between bodies and the screen center big enough. Playing around with new numbers for gravitation and friction powers made the trick.

Regards, Igor

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