Detect Collision but don't resolve if relative velocity > n?

I'm trying to create crates that are destroyed when hit by the player at a certain velocity, but I'd like the player's velocity to not be effected by the collision. This way the player can run and smash-through a row of boxes but still have the same speed when he reached the end.

I can't use sensors, because the crates should collide normally with the player in most cases. I thought about using ContactFilter->ShouldCollide to pre-detect collisions, but this is called when AABB's overlap, so my crates would blow up before a "real" contact.

What I'd like to be able to do is return false from ContactListener->Add to tell Box2d to just throw-out the contact. My attempts at hacking Box2d usually end in failure though. Any other suggestions?

Thanks.
Ok, so apparently I can do this right in the "add" callback:

Code:
crateShape.m_isSensor = true;
b2world.Refilter(crateShape);



Which is pretty awesome because you can kinda fake-destroy shapes right in the middle of a time step. I know meddling with stuff mid-timestep is frowned upon though... is this going to blow up in my face later? 🙂
Actually, I'm surprised that works. I was just looking at your post and thinking of doing something arcane to access the original b2Contact, but if replacing it works then great. I'd expect it to not catch you out if it not immediately broken, but I've only taken a cursory glance.

Then again, I recall when this question was raised before on this board, smarter heads than I generally indicated to the negative when attempting this. So it's quite possible I'm missing something.

I think the danger lies in that you are altering b2World.m_contactList while looping over it, obviously a major no-no. In fact you are deleting a contact, then doing contact.m_next on it. Which is actually fine in the Flash version, but not in the C++ version (though a fix to store that value would sort that out). And as the new contact is meant to be ignored, it doesn't matter where it is inserted into the list (if it appears there at all?).
Yeah, don't refilter inside the callback. I'll be adding an assertion.

You should store the initial velocity of your character in the callback, then after the physics step you can delete the crate and restore your character's velocity.
Erin, would you mind saying where this approach goes wrong, if it superficially appears to be working? Storing the velocity is not ideal, e.g. if other interactions are going on with the player at the same time, particularly joints.
Erin,

Thanks for your response. It's nice to hear from the authority on this stuff.

I considered your solution but didn't go that route because of the reasons Boris mentioned.

May I make a feature request? It would be nice if the ContactListener could "cancel" a contact, kind of like how event dispatching mechanisms work for other mechanisms. For example, javascript and actionscript can "cancel" a click event and prevent it's default behavior. The default behavior in the case of contacts would be solving the collision. I guess this would be sort of a hybrid "ContactFilter" / "ContactListener".

I like to keep my copies of the Box2d code free of hacks (look but don't touch) and so far I've been able to keep it "pure." But the "crate smashing" has ended up being such a fun element in my project I'm not going to let it go 🙂.
Actually I misspoke. What you should do is store the character velocity before the physics step. Then you would need to analyze all the results and restore the velocity accordingly.

Boris: we cannot refilter inside a callback because I make no guarantees about what the refilter function will do to Box2D data structures. For example, it could cause the current b2Contact to be deleted.

It sounds easy to support on-the-fly contact filtering, but it is not due to continuous physics. Box2D uses conservative advancement (CA) to move bodies close to each other without tunneling. CA occurs before the listener is called because the bodies might not touch. If I perform CA, but there is no solver response then this could lead to very small sub-steps.

Perhaps what I could do is add a shape or body flag that says that shape or body should be filtered every step and sub-step. I would like to see you make a good attempt at my suggestion before I add this to the TODO list because the feature will require adding some global overhead.
Here's my rationale why it should be possible to call Refilter from the the Add event, though I'm still not certain. I'm NOT asking you for anything to be added to the todolist though, that thing is way long already.

Erin Catto wrote:
Boris: we cannot refilter inside a callback because I make no guarantees about what the refilter function will do to Box2D data structures. For example, it could cause the current b2Contact to be deleted.

I don't think that actually matters in this case. I think the only reference using m_concactList while Add is called is b2ConcactSolver.Collide, and it's a simple loop to fix. We just need change the m_contactList loop to store the next contact in case the current one goes away.

Erin Catto wrote:
It sounds easy to support on-the-fly contact filtering, but it is not due to continuous physics. Box2D uses conservative advancement (CA) to move bodies close to each other without tunneling. CA occurs before the listener is called because the bodies might not touch. If I perform CA, but there is no solver response then this could lead to very small sub-steps.

Agreed, it will lead to inefficiency as it will do lots of steps to reach the shape, only for there not to be a collision there anyway. But if it is rare that we turn off sensors this way, rather than the existing way, then who cares. And it presumably had to do that many steps in the first place to cope with there being a collision, so the slowdown must be bearable.
But it wouldn't hang Box2D with little steps forever (as I think you are suggesting), as afterwards the shape has been marked as a sensor and the contact replaced with a non-solid one, so the contact won't resurface for calculating CCD toi. (assuming that we have successfully changed or replaced the contact).


EDIT: Agh, I didn't spot that second call to b2Contact.Update right in the middle of SolveTOI. It's getting a bit late to think about this, but that looks resolveable too, by continueing if true is returned.

Mebbe I'll try a patch (in Flash, i'm afraid) for this as a proof of concept.
Ok, so calling Refilter mid-timestep is bad. What about the solution below (this is inside the ContactListener's "Add" function)? Like the "Refilter" approach, it cancels the contact, but it doesn't seem as destructive / bad. All it does is make the contact look like a sensor contact.

Code:
/* cp is the contact point. find the other shape and "sensor-ize" it. */
var ce:b2ContactEdge = cp.shape1.m_body.m_contactList;
while(ce.other != cp.shape2.m_body) {
   ce = ce.next;
}
ce.contact.m_flags |= b2Contact.e_nonSolidFlag;
Hi there,

a little "up" on this topic, just to know if there have been any progress on cancelling a collision...
My case: making a little flash platform game with "going through" platforms a bit like in Mario, you can jump through from
below the platform, but as soon as you're above it, the character can collide with it...

At the moment, what I do is browsing all the current "cross platforms" available on screen when my character starts falling from his jump, and check if my hero is above it. If yes, I change groupfiltering, and collision occur.... if not I just leave it as it was...

Not very optimised, is it?

So I found this post about transforming sensor into shape.... would be great on my case... My platform becomes a sensor,
detect my character, could check its position and change my sensor if necessary, ONLY for that platform....

is this a good solution?
another idea?

Original thread: https://box2d.org/forum/viewtopic.php?t=1216