preCollision

Type Event
Revision Release 2024.3703
Keywords preCollision, physics
See also Collision Detection (guide)
collision
postCollision
physics.setAverageCollisionPositions()
physics.setReportCollisionsInContentCoordinates()

Overview

Used in conjunction with the physics library. Physics engine pre-collision events are exposed through the Corona event listener model with the event name of "preCollision". In addition to these events and standard collision events, Corona offers postCollision events.

Object References

Depending on whether pre-collisions are detected locally or globally, references to the objects involved will vary. For more information about these two collision handling methods, see the Collision Detection guide.

Local Collision Event   Global Collision Event
event.target or self   event.object1
event.other   event.object2
event.selfElement   event.element1
event.otherElement   event.element2

Please compare these two example functions for reference variations:

-- Local preCollision handling
local function onLocalPreCollision( self, event )
    print( event.target )        --the first object in the collision
    print( event.other )         --the second object in the collision
    print( event.selfElement )   --the element (number) of the first object which was hit in the collision
    print( event.otherElement )  --the element (number) of the second object which was hit in the collision
end
object.preCollision = onLocalPreCollision
object:addEventListener( "preCollision" )

-- Global preCollision handling
local function onGlobalPreCollision( event )
    print( event.object1 )       --the first object in the collision
    print( event.object2 )       --the second object in the collision
    print( event.element1 )      --the element (number) of the first object which was hit in the collision
    print( event.element2 )      --the element (number) of the second object which was hit in the collision
end
Runtime:addEventListener( "preCollision", onGlobalPreCollision )

Gotchas

Physics engine pre-collision events are quite “noisy” and may report many times per contact, potentially affecting performance. Therefore, you should only listen for these events if you truly need pre-collision detection. We also recommend that you use local listeners within the objects of interest rather than listening globally to all "preCollision" events in the physics world.

Properties

event.contact

event.element1

event.element2

event.name

event.object1

event.object2

event.other

event.otherElement

event.selfElement

event.target

event.x

event.y