JBox2D碰撞过滤(categoryBits、maskBits)
我已阅读 Box2D 手册,并且了解屏蔽的一般工作原理。但我得到的结果并不遵循手册中的规定。
我有 3 类东西:玩家、障碍物和触发器。
玩家和障碍物会相互碰撞,但不会与自身碰撞。我通过设置来实现此功能:
player
circleDef.filter.categoryBits = 0x2;
circleDef.filter.maskBits = 0x4;
obstacle
shapeDef.filter.categoryBits = 0x4;
shapeDef.filter.maskBits = 0x2;
我还想要触发器类型...这些我仅用于检测玩家和触发事件,但应该不被任何东西碰撞。从逻辑上讲,通过将 maskBits 设置为 0x0 应该可以实现这一点。 JBox2D碰撞代码如下。
DefaultContactFilter.java
boolean collide = (filter1.maskBits & filter2.categoryBits) != 0 && (filter1.categoryBits & filter2.maskBits) != 0;
return collide;
因此,如果 maskBits = 0,则碰撞应该从不返回 true。我将类别位设置为下一个空闲类别 - 0x8。
trigger
shapeDef.filter.categoryBits = 0x8;
shapeDef.filter.maskBits = 0x0;
我认为这可能与 Java 的 int 工作方式与我的预期不同有关,但我测试并发现了以下结果:
0x2 & 0x2 = 2
0x2 & 0x0 = 0
触发器旨在用于触发事件。这是通过实现 CollisionListener 并使用触发器测试玩家碰撞,然后设置布尔标志来完成的。然而,当我运行模拟时,玩家与触发器发生碰撞,障碍物也是如此。
我还尝试将类别和掩码位设置为以下相应值:
0x0 0x0
0x8 0x8
0x2 0x4
0x4 0x2
并且玩家始终与触发对象发生碰撞。如何让玩家和障碍物从不与触发对象发生碰撞?
I've read the Box2D manual and I understand how masking works in general. But the results I'm getting are not following what is laid out in the manual.
I have 3 categories of things: players, obstacles and triggers.
players and obstacles collide with each other but not with themselves. I've got this working by setting:
player
circleDef.filter.categoryBits = 0x2;
circleDef.filter.maskBits = 0x4;
obstacle
shapeDef.filter.categoryBits = 0x4;
shapeDef.filter.maskBits = 0x2;
I want to also have trigger types... these I'm only using to detect the player and trigger events but should not be collided with by anything. Logically, that should be possible by setting the maskBits to 0x0. The JBox2D collision code is as follows.
DefaultContactFilter.java
boolean collide = (filter1.maskBits & filter2.categoryBits) != 0 && (filter1.categoryBits & filter2.maskBits) != 0;
return collide;
So if the maskBits = 0, then collide should never return true. I set the category bits to the next free category - 0x8.
trigger
shapeDef.filter.categoryBits = 0x8;
shapeDef.filter.maskBits = 0x0;
I thought this could be to do with Java's ints working differently to how I expected, but I tested and found the following results:
0x2 & 0x2 = 2
0x2 & 0x0 = 0
The triggers are meant to be used to trigger events. This is done by implementing the CollisionListener and testing for player collisions with triggers and then setting boolean flags. However, when I run the simulation, the player is colliding with the triggers, and so are the obstacles.
I've also tried setting the category and mask bits to the following respective values:
0x0 0x0
0x8 0x8
0x2 0x4
0x4 0x2
And the player always collides with the trigger object. How do I get the player and obstacles to never collide with the trigger object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论