why do I get a spurious collision event in frequently with this code snippet?

I have a game/physics prototype that seems to work fine 99 out of 100 times.  But every so often on level “reset” (when use initiates this) there is an immediate collection with the player standing at the starting point which I don’t expect.  The physics is paused and player body is also “inactive” at this point in time so why would a collision occur during the reset phase.

Question:  Is there a case where a collision would occur in my “reset level” code phase below?  Wondering if because everything occurs in sequence whether this could be an issue?  Does some parts of this section really need to have “performAfterDelay” perhaps.    

-- PLAYER IN MOTION  2013-09-10 11:24:56.035 Corona Simulator[6677:707] Player.isBodyActive = true -- COLLISION OCCURS 2013-09-10 11:24:57.453 Corona Simulator[6677:707] onPlayerCollision:began:      2013-09-10 11:24:57.453 Corona Simulator[6677:707] physics.removeBody(Player) -- RESET LEVEL HIT (Get collision every so often on restart during this phase, but why?) 2013-09-10 11:25:01.644 Corona Simulator[6677:707] physics.pause() 2013-09-10 11:25:01.645 Corona Simulator[6677:707]   physics.addBody( Player, "dynamic")    -- add body back 2013-09-10 11:25:01.645 Corona Simulator[6677:707]   Player.isBodyActive = false            -- make inactive until after motion is started 2013-09-10 11:25:01.645 Corona Simulator[6677:707] physics.start()  

I’ve worked the code in with the logging to given an indication of times…

Update: Think I have it (more testing needed) but it appear I do need to delay the physics.start to allow the 'Player.isBodyActive = false " to take effect.  So last line now:

    timer.performWithDelay(100,  function(event)             physics.start()         end     )

Hi Greg,

I’m not sure if this is the cause, but making a body active actually causes a collision event (if it’s in contact with another body, like an object over a sensor). Even making it inactive may trigger an “ended” collision phase for that object (because it is, in effect, “ending” collision on the object it’s in contact with when it becomes inactive).

Brent

thanks Brent - didn’t think of that - in this case (looking back at my code above) that should be ok no, as I do this straight after doing a “physics.pause”?   Or could this be a case where you need to do a “performWithDelay” for the “addBody” to make sure the “physics.pause” really does finished first?   

Hi Greg,

Can you show the bit of code where you’re doing this? As isolated as you can make it, please (i.e. no Storyboard “full scene code dump” :slight_smile: )

Thanks,

Brent

it’s kind of buried within the app so I can’t easily extract (without spending some time) - that’s why I just posted the pseudo-code.   I have a work around now with having to put the “performWithDelay” in so that’s letting me progress.  I remember reading other places in Corona where you need to do the “performWithDelay” trick, but can’t remember where.  perhaps it’s just an inherent aspect of the Corona/box2D design and how it works we can’t escape from

Brent - for your info, I went through and put more debug code to double check - there was actually a place in the game where I put a isBodyActive = false, and then put this back to true based on timer/delay.  The end result is that there was a design flaw whereby I could be issuing an isBodyActive = true, after I’d done a physics.removeBody on the player.  Not exactly sure why this caused on the flow on effect or what happened under the bonnet due to this however it wasn’t good.   That is I still end up getting the collision occurring after the isBodyActive is false, and before it gets set to true again.   So I won’t try to understand exactly what is happening but fix the design flaw…

&nbsp;physics.removeBody &nbsp;parachutist.isBodyActive = true &nbsp; &nbsp; &nbsp;-- \<== CALLED AFTER DELAY, BUT IN THIS CASE ENDED &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- UP BEING AFTER THE REMOVE BODDY &nbsp;resetLevel ---------------------------------------------------------- &nbsp;physics.pause &nbsp;physics.addBody &nbsp;parachutist.isBodyActive = false &nbsp;physics.start &nbsp;onParachutistCollision: &nbsp; &nbsp; nil &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- COLLISION BEFORE&nbsp;parachutist.isBodyActive = true IS SET LATER &nbsp;

Update: Think I have it (more testing needed) but it appear I do need to delay the physics.start to allow the 'Player.isBodyActive = false " to take effect.  So last line now:

&nbsp;&nbsp;&nbsp;&nbsp;timer.performWithDelay(100,&nbsp; function(event) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;physics.start() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;)

Hi Greg,

I’m not sure if this is the cause, but making a body active actually causes a collision event (if it’s in contact with another body, like an object over a sensor). Even making it inactive may trigger an “ended” collision phase for that object (because it is, in effect, “ending” collision on the object it’s in contact with when it becomes inactive).

Brent

thanks Brent - didn’t think of that - in this case (looking back at my code above) that should be ok no, as I do this straight after doing a “physics.pause”?   Or could this be a case where you need to do a “performWithDelay” for the “addBody” to make sure the “physics.pause” really does finished first?   

Hi Greg,

Can you show the bit of code where you’re doing this? As isolated as you can make it, please (i.e. no Storyboard “full scene code dump” :slight_smile: )

Thanks,

Brent

it’s kind of buried within the app so I can’t easily extract (without spending some time) - that’s why I just posted the pseudo-code.   I have a work around now with having to put the “performWithDelay” in so that’s letting me progress.  I remember reading other places in Corona where you need to do the “performWithDelay” trick, but can’t remember where.  perhaps it’s just an inherent aspect of the Corona/box2D design and how it works we can’t escape from

Brent - for your info, I went through and put more debug code to double check - there was actually a place in the game where I put a isBodyActive = false, and then put this back to true based on timer/delay.  The end result is that there was a design flaw whereby I could be issuing an isBodyActive = true, after I’d done a physics.removeBody on the player.  Not exactly sure why this caused on the flow on effect or what happened under the bonnet due to this however it wasn’t good.   That is I still end up getting the collision occurring after the isBodyActive is false, and before it gets set to true again.   So I won’t try to understand exactly what is happening but fix the design flaw…

&nbsp;physics.removeBody &nbsp;parachutist.isBodyActive = true &nbsp; &nbsp; &nbsp;-- \<== CALLED AFTER DELAY, BUT IN THIS CASE ENDED &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- UP BEING AFTER THE REMOVE BODDY &nbsp;resetLevel ---------------------------------------------------------- &nbsp;physics.pause &nbsp;physics.addBody &nbsp;parachutist.isBodyActive = false &nbsp;physics.start &nbsp;onParachutistCollision: &nbsp; &nbsp; nil &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- COLLISION BEFORE&nbsp;parachutist.isBodyActive = true IS SET LATER &nbsp;