How to use PostCollision and isSensor to make Character jump through Platform?

   Hi Everyone.

 I’ve just read Corona’s Collision Detection Document. I want to make an object go through others, so I tested with PreCollison as the Doc:

https://docs.coronalabs.com/daily/guide/physics/collisionDetection/index.html#collisionevents

 I opened Corona’s Collison Detection Sample, and modified the main.lua as:

-- -- Abstract: CollisionDetection sample project -- Demonstrates global and local collision listeners, along with collision forces -- -- Version: 1.2 (revised for Alpha 3, demonstrates "collision" event and new "preCollision" and "postCollision" events) -- -- Sample code is MIT licensed, see http://www.coronalabs.com/links/code/license -- Copyright (C) 2010 Corona Labs Inc. All Rights Reserved. -- -- Supports Graphics 2.0 --------------------------------------------------------------------------------------- local centerX = display.contentCenterX local centerY = display.contentCenterY local \_W = display.contentWidth local \_H = display.contentHeight local physics = require( "physics" ) physics.setDrawMode( "hybrid" ) physics.start() local sky = display.newImage( "bkg\_clouds.png", centerX, 195 ) local ground = display.newImage( "ground.png", centerX, 445 ) ground.myName = "ground" physics.addBody( ground, "static", { friction=0.5, bounce=0.3 } ) local crate1 = display.newImage( "crate.png", 180, -50 ) crate1.myName = "first crate" physics.addBody( crate1, { density=3.0, friction=0.5, bounce=0.3 } ) local function onLocalCollision( self, event ) if ( event.phase == "began" ) then print( self.myName .. ": collision began with " .. event.other.myName ) elseif ( event.phase == "ended" ) then print( self.myName .. ": collision ended with " .. event.other.myName ) end end local function onLocalPreCollision( self, event ) print( "preCollision: " .. self.myName .. " is about to collide with " .. event.other.myName ) self.isSensor = true end local function onLocalPostCollision( self, event ) print( "postCollision force: " .. event.force .. ", friction: " .. event.friction ) end -- Here we assign the above two functions to local listeners within crate1 only, using table listeners: crate1.preCollision = onLocalPreCollision crate1:addEventListener( "preCollision", crate1 ) crate1.postCollision = onLocalPostCollision crate1:addEventListener( "postCollision", crate1 ) crate1.collision = onLocalCollision crate1:addEventListener( "collision" )

   As the doc said, on PreCollision you can use a short timer to set object.isSensor = true, so it will pass through ground. I tried both ways: use timer or not, but the Crate still bounced off the ground :frowning:

 I did more researchs, and found out that I can make it by setting ‘event.contact.isEnabled = false’ on PreCollision instead of using object’s ‘isSensor’ property.

  What’s prolems here? Am I wrong with something :( ?

One more question, I think the ‘PreCollision’ event should fire before the objects start to interact, which means it will occur before the ‘collision’ event. But the order I saw was : 

collision event with 'began' phase -\> preCollsion -\> postCollision -\> (maybe) collsion event with 'ended' phase

Hi @khanh.dq,

Can you state what your exact goal is? Do you simply want the crate to pass through a certain object (but not necessarily all objects)? If so, then a pre-collision listener is the answer, and using it to set the physics contact “.isEnabled = false”. That essentially tells the physics engine to ignore the collision entirely, as if it never occured, and the object should pass through whatever it was “about to collide with”.

Take care,

Brent

  Hi Brent.

 Yes, I do want the Create to pass through a certain object on some conditions. I found out myself to set physics ‘contact.isEnabled = false’ on pre-collision listener and it worked.

 My first question is why Corona’s Doc said that I could set ‘object.isSensor = true’ and I tried but it didn’t work.

 And the second:

 

One more question, I think the ‘PreCollision’ event should fire before the objects start to interact, which means it will occur before the ‘collision’ event. But the order I saw was : 

collision event with ‘began’ phase -> preCollsion -> postCollision -> (maybe) collsion event with ‘ended’ phase

Hi @khanh.dq,

I need to investigate the sample code more closely, as the sensor setting on preCollision does not seem to work (as you found out, and I just tested). Absolutely, though, the recommended way to void a collision entirely is to use the preCollision with contact.isEnabled.

As for the timing on your print() of collision events, I think internally those two events may be occurring at the same time step (frame), however the preCollision is still used to potentially void the normal collision IF the contact.isEnabled is false.

Brent

 Thank Brent.

 Maybe you should edit the Doc to avoid others misunderstand :slight_smile:

Hi @khanh.dq,

Can you state what your exact goal is? Do you simply want the crate to pass through a certain object (but not necessarily all objects)? If so, then a pre-collision listener is the answer, and using it to set the physics contact “.isEnabled = false”. That essentially tells the physics engine to ignore the collision entirely, as if it never occured, and the object should pass through whatever it was “about to collide with”.

Take care,

Brent

  Hi Brent.

 Yes, I do want the Create to pass through a certain object on some conditions. I found out myself to set physics ‘contact.isEnabled = false’ on pre-collision listener and it worked.

 My first question is why Corona’s Doc said that I could set ‘object.isSensor = true’ and I tried but it didn’t work.

 And the second:

 

One more question, I think the ‘PreCollision’ event should fire before the objects start to interact, which means it will occur before the ‘collision’ event. But the order I saw was : 

collision event with ‘began’ phase -> preCollsion -> postCollision -> (maybe) collsion event with ‘ended’ phase

Hi @khanh.dq,

I need to investigate the sample code more closely, as the sensor setting on preCollision does not seem to work (as you found out, and I just tested). Absolutely, though, the recommended way to void a collision entirely is to use the preCollision with contact.isEnabled.

As for the timing on your print() of collision events, I think internally those two events may be occurring at the same time step (frame), however the preCollision is still used to potentially void the normal collision IF the contact.isEnabled is false.

Brent

 Thank Brent.

 Maybe you should edit the Doc to avoid others misunderstand :slight_smile: