8
\$\begingroup\$

I know this sounds silly but I would like some objects to follow physics while others not to collide each other.

I tried to achieve them by setting their position exclusively. But then it will ignore all physics.

Is what I am trying to do even possible?

\$\endgroup\$
  • \$\begingroup\$ I'm not sure what you want to achieve, but kinematic bodies might help as well. \$\endgroup\$ – ThatOneGuy Jun 22 '13 at 15:53
11
\$\begingroup\$

Read up on collision filtering in the Box2D manual:

Collision filtering allows you to prevent collision between fixtures. For example, say you make a character that rides a bicycle. You want the bicycle to collide with the terrain and the character to collide with the terrain, but you don't want the character to collide with the bicycle (because they must overlap). Box2D supports such collision filtering using categories and groups.

Box2D supports 16 collision categories. For each fixture you can specify which category it belongs to. You also specify what other categories this fixture can collide with. For example, you could specify in a multiplayer game that all players don't collide with each other and monsters don't collide with each other, but players and monsters should collide

Link to manual here. (Search for Filtering) .

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

If you have two kinds of objects that should not collide with another, then the following works.

var kindA = -1;
var kindB = -2;

var bodyDef = new b2BodyDef();
var body = world.CreateBody(bodyDef);
var fixtureDef = new b2FixtureDef();
fixtureDef.shape = new b2Shape();
fixtureDef.filter.groupIndex =
    entity instanceof EntityA
        ? kindA
        : kindB;
body.CreateFixture(fixture);
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.