Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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!

share|improve this question
1  
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. –  Byte56 Jul 4 '13 at 20:30
    
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. –  DavidH Jul 5 '13 at 14:26

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
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. –  DavidH Jul 6 '13 at 11:22
    
Sorry for the bad answer ;), I just edited it, hinting two possible approaches. –  Jaibuu Jul 7 '13 at 7:42
    
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. –  DavidH Jul 7 '13 at 16:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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