Pergunta

I have a 2D side scrolling game that's kind of like a metroid-vania, I have some NPC's I want to add, and I want to simulate them in the world with gravity and such(they collide with the floors) but I don't want them to collide with the player.

Setting the fixture to isSensor = true, causes the NPC's to fall through the floor, and setting it to false causes them to collide with the player.

Is there any way I can accomplish this without simulating a different World and then checking positions?

Foi útil?

Solução

Using Box2D's built in filter you can filter collisions so that they do not collide

FixtureDef floorFixture
floorFixture.filter.categoryBits = 4

FixtureDef playerFixture
playerFixture.filter.catagoryBits = 2;
playerFixture.filter.maskBits = 4;

FixtureDef npcFixture
npcFixture.filter.catagoryBits = 2;
npcFixture.filter.maskBits = 4;

In this Example the Player and the NPC should not Collide with each other, but will collide with the floor.

Read more here(search for Filtering): http://www.box2d.org/manual.html

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top