1
\$\begingroup\$

I'm in the process of creating a simple platform game using Cocos2D 2.0 and Box2D. I'm trying to create a kind of crate object that cannot be moved by the player, but that can be moved by an elephant object. When the player runs up to the crate, it should stop him dead in his tracks (as if he is running into a static Box2D body). If the elephant runs into it it should get knocked out of the way (as if the elephant was running into a dynamic Box2D body of much smaller mass/density). I can't use collision bitmask because of course I want collision to occur in both cases, I just want that collision to result in different things depending on which type of object is hitting the crate.

Anyone have a hint as to how to make this work?

Thanks in advance!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Checkout pre-solve events: gamedev.stackexchange.com/questions/15985/…. Then you can decide how to respond to the contact based on the object doing to contact. \$\endgroup\$
    – House
    Commented Jul 4, 2013 at 20:30
  • \$\begingroup\$ That seems like the right way to address this, but I'm not looking to eliminate any contacts, I just want the physics to work differently depending on what is making contact with the blocks. There's undoubtedly a way to use PreSolve events to tackle this, but I don't know how. \$\endgroup\$
    – DavidH
    Commented Jul 5, 2013 at 14:26

1 Answer 1

1
\$\begingroup\$

Depending on the rest of your logic, you could either use collision filtering to create a case where players ignore the box, but react to an invisible unmovable box, while elephants ignore the invisible box, but react to the visible box.

Or you could use postSolvers, using the contact object passed by postSolvers in this way:

if (contact.GetFixtureA().GetBody().itemType == 'player' && contact.GetFixtureB().GetBody().itemType == 'box' ) {
contact.GetFixtureB().GetBody().setMass(999);
}

That's an oversimplified javascript example, your implementation might be different, but the concept is that you can manipulate the bodies referenced by the contact object passed to the postSolver you set.

\$\endgroup\$
3
  • \$\begingroup\$ Thanks for your answer! As far as I can tell, this actually can't be solved by categoryBits and maskBits. As I mentioned above "I can't use collision bitmask because of course I want collision to occur in both cases, I just want that collision to result in different things depending on which type of object is hitting the crate." In other words, I'm not looking to eliminate any contacts, I just want the physics to work differently depending on what is making contact with the blocks. \$\endgroup\$
    – DavidH
    Commented Jul 6, 2013 at 11:22
  • \$\begingroup\$ Sorry for the bad answer ;), I just edited it, hinting two possible approaches. \$\endgroup\$
    – Jaibuu
    Commented Jul 7, 2013 at 7:42
  • \$\begingroup\$ Cool, thanks! The invisible box trick worked well. I have two bodies: one static and one dynamic. I used collision filtering so that the elephant knocks against the dynamic and the player against the static. The static body's position is updated as part of the standard update() loop to have the same position as that of the dynamic body. \$\endgroup\$
    – DavidH
    Commented Jul 7, 2013 at 16:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .