开发者

JBox2D collision filtering (categoryBits, maskBits)

开发者 https://www.devze.com 2023-03-06 04:57 出处:网络
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'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 开发者_StackOverflowand 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?

0

精彩评论

暂无评论...
验证码 换一张
取 消