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
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