Changing size doesn't change collision area.

Hello community,

I’d like to increase the size of my object upon certain events (I’d like for Reggie the bacteria to grow when he eats). However, when I do, the collision-detection area seems to remain as the original size of the object. After expanding the object, it will overlap with other objects until its very center collides. 

I tried different methods of expanding. Adjusting the height/width. Adjusting the xScale/yScale. Transitioning the height/width. etc. I’ve also tried adding the body again as a physics object after its transformation. 

I’ve considered deleting the object and creating it again, but I’d like to avoid this if possible. My hunch is this would cause lots of problems in other areas.

Is there any way to keep the collision-detection area so that it matches the actual boundaries of an object after changing the size?

Have you tried using the non-physics collision logic to detect overlapping objects? It might serve you better in this instance:

http://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

Hi @zivkovic.jeff,

Physics-enabled objects can’t be scaled or resized after you create them. Well, they “can” be, but only the display object (image) will scale, while the body definition remains the same.

For your scenario, you may consider these two workarounds:

  1. Destroy and re-create the body when “Reggie” grows or shrinks. This is the most simple approach, and as long as you’re not doing this with a loop of like 100 objects at once, you shouldn’t notice any performance impact at all.

  2. Create a multi-element physics body as illustrated in the following tutorial, and then selectively tell Corona which “element” should be honored (and which shouldn’t) during a collision, depending on Reggie’s size. This is probably a more robust solution, but I think it’s overkill for your needs. Still, you should read the tutorial just in case…

http://www.coronalabs.com/blog/2013/01/08/working-with-multi-element-physics-bodies/

Take care,

Brent

I was trying that approach in the earliest phases of development. But the physics engine works so much nicer for all the walls that Reggie needs to bounce off. And the other Reggies.

The first solution has proven problematic. I haven’t been able to create a new object during a collision and still set its linear velocity.

As for the second solution, I read the tutorial. To implement this, would suggest that I create a multi-element body composed of concentric circles? Then “turn on” the outer circles as Reggie grows?

Sounds like you should revisit the non-physics option. I’m telling you, it works wonders!

for the first option - you say during a collision - i seem to recall reading something in a tut or forum about using a small delay.

T.

Hi @zivkovic.jeff,

As @ToeKnee says, you can’t perform certain physics actions during a collision event, so you need to perform them following a minuscule delay (like 10 milliseconds). I updated this guide to indicate which actions can and can’t be done during a collision time step (look under “Collision Handling”):

http://docs.coronalabs.com/guide/physics/collisionDetection/index.html

Option #2 is still perfectly valid too. And yes, you would basically create a series of concentric circles for Reggie, in a known order… i.e., the smallest one is declared first, the next larger one is declared second, etc. This order of declaration is crucial because it sets the “index” of the body elements, which you’ll need later during the collision handler. Essentially, Reggie would have a property set to which “size” he is, based on the index number of the body element you want to detect. Then, in the preCollision, you’d conditionally check if any body element is “active” and, if not, use the PhysicsContact to bypass the collision itself.

Brent

Thanks guys for the advice.

I’ve tried to do a few different things with timer delays. But it seems that every time I try to include a timer.performWithDelay in a function that is called by a collision event, the system ignores the delay period. It simply calls the function immediately.

I haven’t had any problems with timer.performWithDelay anywhere else.

Have you tried using the non-physics collision logic to detect overlapping objects? It might serve you better in this instance:

http://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

Hi @zivkovic.jeff,

Physics-enabled objects can’t be scaled or resized after you create them. Well, they “can” be, but only the display object (image) will scale, while the body definition remains the same.

For your scenario, you may consider these two workarounds:

  1. Destroy and re-create the body when “Reggie” grows or shrinks. This is the most simple approach, and as long as you’re not doing this with a loop of like 100 objects at once, you shouldn’t notice any performance impact at all.

  2. Create a multi-element physics body as illustrated in the following tutorial, and then selectively tell Corona which “element” should be honored (and which shouldn’t) during a collision, depending on Reggie’s size. This is probably a more robust solution, but I think it’s overkill for your needs. Still, you should read the tutorial just in case…

http://www.coronalabs.com/blog/2013/01/08/working-with-multi-element-physics-bodies/

Take care,

Brent

I was trying that approach in the earliest phases of development. But the physics engine works so much nicer for all the walls that Reggie needs to bounce off. And the other Reggies.

The first solution has proven problematic. I haven’t been able to create a new object during a collision and still set its linear velocity.

As for the second solution, I read the tutorial. To implement this, would suggest that I create a multi-element body composed of concentric circles? Then “turn on” the outer circles as Reggie grows?

Sounds like you should revisit the non-physics option. I’m telling you, it works wonders!

for the first option - you say during a collision - i seem to recall reading something in a tut or forum about using a small delay.

T.

Hi @zivkovic.jeff,

As @ToeKnee says, you can’t perform certain physics actions during a collision event, so you need to perform them following a minuscule delay (like 10 milliseconds). I updated this guide to indicate which actions can and can’t be done during a collision time step (look under “Collision Handling”):

http://docs.coronalabs.com/guide/physics/collisionDetection/index.html

Option #2 is still perfectly valid too. And yes, you would basically create a series of concentric circles for Reggie, in a known order… i.e., the smallest one is declared first, the next larger one is declared second, etc. This order of declaration is crucial because it sets the “index” of the body elements, which you’ll need later during the collision handler. Essentially, Reggie would have a property set to which “size” he is, based on the index number of the body element you want to detect. Then, in the preCollision, you’d conditionally check if any body element is “active” and, if not, use the PhysicsContact to bypass the collision itself.

Brent

Thanks guys for the advice.

I’ve tried to do a few different things with timer delays. But it seems that every time I try to include a timer.performWithDelay in a function that is called by a collision event, the system ignores the delay period. It simply calls the function immediately.

I haven’t had any problems with timer.performWithDelay anywhere else.