用 box2d 检测物体碰撞的干净方法

发布于 2024-12-04 20:40:11 字数 478 浏览 0 评论 0原文

我最近一直在研究 box2d (在带有 libgdx 的 java 中),并一直在尝试创建自己的自定义弹跳效果(我在第一次弹跳后增加了 Restitution

为了将此作为测试,我简单地检查了物体的位置并等待第一次反弹。

但现在我想实际实现这个并遇到一个问题:如何检测 box2d 中 2 个特定对象的碰撞?

我找到了这个教程: box2d 碰撞检测 - 但我非常不愿意使用该代码。必须有一种更简单、更清晰的方法来检测两个对象之间的碰撞(无需设置用户数据并使用巨大的 if() 条件检查所有碰撞...)

任何人都可以帮助我吗? (假设我不仅充满希望而且实际上有更好的方法)

I have been looking into box2d (in java with libgdx) lately and have been trying to create my own custom bounce effect (I increase the Restitution after the first bounce)

To do this as a test I simply checked the location of the object and waited for the first bounce.

But now I wanted to actually implement this and came across a problem: How to detect the collision of 2 specific object in box2d?

I found this tutorial:
box2d collision detection - but I am very reluctant to use that code. There must be a simpler and cleaner way to detect a collision between 2 objects (without having to set user data and checking all collisions with giant if() conditions...)

Can anyone help me out? (assuming I am not just hopeful and there actually is a better way)

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

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

发布评论

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

评论(2

吹梦到西洲 2024-12-11 20:40:11

子类 b2ContactListener 类来处理碰撞并重新实现碰撞回调。然后只需:

MyContactListener *listener = new MyContactListener();
myB2World->SetContactListener(listener);

Subclass b2ContactListener class to handle the collision and reimplement collision callbacks. Then just:

MyContactListener *listener = new MyContactListener();
myB2World->SetContactListener(listener);
三生池水覆流年 2024-12-11 20:40:11

请注意,有些解决方案不像往常一样有捷径,但您可以找到正确的解决方案,并且可能有一百种方法来检测碰撞,您知道这一点,不是吗?例如,这就像 box2d 碰撞检测程序的解决方案一样。

http://blog .allanbishop.com/box2d-2-1a-tutorial-%E2%80%93-part-4-collision-detection/

And take note, some solutions don't have shortcuts as always but you can find the right solution and there is possibly a hundred ways to detect a collision and you know it, don't you? This is like in the solution of box2d collision detection program for example.

http://blog.allanbishop.com/box2d-2-1a-tutorial-%E2%80%93-part-4-collision-detection/

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