用 box2d 检测物体碰撞的干净方法
我最近一直在研究 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
子类
b2ContactListener
类来处理碰撞并重新实现碰撞回调。然后只需:Subclass
b2ContactListener
class to handle the collision and reimplement collision callbacks. Then just:请注意,有些解决方案不像往常一样有捷径,但您可以找到正确的解决方案,并且可能有一百种方法来检测碰撞,您知道这一点,不是吗?例如,这就像 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/