Enable / disable physics bodies

I see this has been asked in several different ways, but I haven’t found a good answer yet and I’m running out of ideas.  If you have a ball let’s say of a radius of 100 that is a physics body, and you wish to reduce or scale down that ball to a radius of 10, if you have multiple bodies, the largest body is still the reactionary body when it collides with static bodies. I’ve tried adding and removing bodies but that looks wonky and loses the in flight properties.  I’ve played with Brent Sorrentino’s ideas on using selfElement but that is for collision events, not for collision actions such as bouncing.  

Does anyone have a solutions for this?  Another way to think of it is an object that as it bounces against walls and gets smaller with each bounce.

Thanks

Pep

Hi Pep,

Did you try the usage of the “PhysicsContact” object and pre-collision listeners? The basic idea is that you create the ball with multiple circular body elements of varying diameter (so, it would look like a shooting/archery target if you turned on “hybrid” physics view). Then, when the ball gets smaller, you basically set some type of “index” property on the ball which tells Box2D which of the multiple body parts are still active.

In other words, let’s say you have a ball which might have 10 different sizes as it collides with things and gets smaller. When you create the 10 body parts, they are automatically assigned an index based on the order of their creation (see here). So, create the smallest one first and the largest one last, resulting in the smallest element having an index of 1 and the largest 10. Then, assign the ball its own counting property of 10 as well (to match how many elements should initially be “active”). When the ball collides, compare this number against the internal index of which element collided, and then (using PhysicsContact), ignore the collision completely if that part is “inactive”. Of course, as the ball gets smaller, you should reduce the counter on it to match how many of its elements are still active.

I hope that makes sense. It seems complicated but once you get it going, it should be really effective.

Brent

Thanks Brent!  That’s the path I need to look at going down.  My apologies if this has been answered.

Pep

Hi Pep,

Did you try the usage of the “PhysicsContact” object and pre-collision listeners? The basic idea is that you create the ball with multiple circular body elements of varying diameter (so, it would look like a shooting/archery target if you turned on “hybrid” physics view). Then, when the ball gets smaller, you basically set some type of “index” property on the ball which tells Box2D which of the multiple body parts are still active.

In other words, let’s say you have a ball which might have 10 different sizes as it collides with things and gets smaller. When you create the 10 body parts, they are automatically assigned an index based on the order of their creation (see here). So, create the smallest one first and the largest one last, resulting in the smallest element having an index of 1 and the largest 10. Then, assign the ball its own counting property of 10 as well (to match how many elements should initially be “active”). When the ball collides, compare this number against the internal index of which element collided, and then (using PhysicsContact), ignore the collision completely if that part is “inactive”. Of course, as the ball gets smaller, you should reduce the counter on it to match how many of its elements are still active.

I hope that makes sense. It seems complicated but once you get it going, it should be really effective.

Brent

Thanks Brent!  That’s the path I need to look at going down.  My apologies if this has been answered.

Pep