Things firing out other things

I'm sure I read something about this somewhere, but after a hunt around the forums, the wiki and the documentation, I can't find it again...

I want bodies to be able to emit other bodies - my specific example involves a character throwing a brick. The brick body is overlapping the character's body when it's spawned, so obviously I need to filter the collision between the brick and the character until such time as the brick has moved far enough to not be colliding with the character any more. However, I want the character and the brick to be able to collide again as soon as the brick is clear. What's the best way to go about this? I'm a little hazy on the exact details of how collision filtering and collision listeners work so I'm not sure of the best way to put this all together.
You can manually refilter a shape. See b2World::Refilter. In your case it might not be necessary because refiltering is done automatically when the AABBs separate and then overlap again.
I haven't seen what you said you saw earlier, but I think a reasonable way to do this is to initialize the brick with a collision filtering setting that allows it to not collide with your character, and then, in the collision callbacks, the instant that the brick collides with anything OTHER than your character body, it will change the filtering so as to have it collide with the character body. I believe this will not require any additional data that you have to keep track of, but im sure there are cleaner ways to implement it and very hacky ugly ways to do it.

This'll make it so that if your dude is standing right in front of a wall and tosses a brick at it, the brick will hit the wall and then sorta push him away from the wall a little bit. If you still want it to not collide until it completely leaves him then it's a little tougher..
I think if I had a preference, I'd let brick and the character overlap until such time as they don't overlap anymore - ie, the character throws a brick against a wall, and it bounces back and lands at his feet, but is overlapping him all through that time. Only when he walks away and comes back (or does something like jump up and fall down on top of the brick) would I want to turn collision on in that case, I think. Popping or pushing the characters around seems like it would be a bit unnatural for what I want, although without implementing it I can't be sure.

Is there something that can be done with contact listeners, registering when the character and the brick no longer overlap because the contact point is removed? I'm not sure if contact listeners can work that way... I also noticed in the documentation that you can implement a b2ContactFilter, but I don't know how that differs from just setting the shape filter of the brick when it's created, and then resetting it when it's no longer touching the character.
well i think you might be able to get away with checking the brick's shapes against the character's shapes every frame, using these methods that collide polygon shapes (I can't seem to find it in the api at the moment but there was a thread with the info) and when they do not intersect anymore, change the collision filters.
Or make a small change to my other method where you check for intersection with the character shape when it hits something else, and if it does not intersect it then, switch it. That ought to do it.
Okay, so there's no built-in way to tell when shapes which were intersecting are no longer intersecting, then? My brick has to check every frame until it no longer finds the character in its contact point list? Once it's done that and refiltered its collision it shouldn't need to check collisions against any other specific thing during its lifetime, so bricks should still be relatively cheap to process.

Another question: My game is going to potentially have many bricks, and many characters in the same area. I only want the specific brick that's being thrown to filter collision with the specific character who is throwing it - I want the brick to collide with other characters and bricks, and I want the character to collide with other bricks. I'm not sure how to set this up.
Erin Catto wrote:
In your case it might not be necessary because refiltering is done automatically when the AABBs separate and then overlap again.

this is good to know! you should put that in the manual.
ElectroDruid wrote:
Okay, so there's no built-in way to tell when shapes which were intersecting are no longer intersecting, then? My brick has to check every frame until it no longer finds the character in its contact point list? Once it's done that and refiltered its collision it shouldn't need to check collisions against any other specific thing during its lifetime, so bricks should still be relatively cheap to process.

Another question: My game is going to potentially have many bricks, and many characters in the same area. I only want the specific brick that's being thrown to filter collision with the specific character who is throwing it - I want the brick to collide with other characters and bricks, and I want the character to collide with other bricks. I'm not sure how to set this up.


i don't think you can use the contact point list to check for collision without having impulses applied. Are your characters and bricks both one shape bodies? There is some "polygon to polygon" collision check that returns a bool that you should use. I think.
You mean that if the brick starts out filtered so that it doesn't collide with the character, the character will never appear in the brick's contact points list, so it won't know whether there's an overlap or not? There's no way to tell two bodies "don't seperate on collision, but do report that you've intersected"? Hmm... That's unfortunate, if that's the case.

I'll have to look into what you've said about the poly to poly collision checks then, I suppose. The brick is one body. The character is three (2 circles joined by a square to make a capsule shape), but for these purposes just using an AABB for the character would be sufficient. It seems a bit crazy to do the collision test myself, "by hand" though, I would have thought there should be an inbuilt way to report collisions without enforcing collision responses 😐 Maybe that's something that could be a feature request for a future version? Have some additional way of setting up bodies such that they'll do the collision detection against other bodies, and generate contact points, but just skip the collision response part.

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