Detect collision but not interact based on object type.

Hi!

So I have this issue I am trying to resolve, but I’ve failed to find a good solution:

I have a world and two objects.

Each of the objects need to have normal collision behavior with the respect to the world.

The two objects need to know when they are colliding, but they must not interact. That is, they must not disturb each others movement.

Does anyone have a solution to this? Thanks

Hi there,

You’ll need to implement a pre-collision listener on the objects to stop the physics engine from allowing them disturb each other when they collide.  To do that, you set event.contact.isEnabled to false inside the pre-collision listener.

A simple listener would look something like this…

local function preCollision(self, event)   --Ignore non-contact events   if (event.contact==nil) then return end      --Get the other object involved in the collision...   local object = event.other      --Tell Box 2D to ignore objects with the 'ghost' property.   if (object.ghost) then      event.contact.isEnabled=false   end end

Now add the ghost property to your two objects.

object1.ghost=true object2.ghost=true

And finally add the pre-collision listener to your objects.

Add pre-collision listener to object 1 object1.preCollision = onPreCollision object1:addEventListener("preCollision", object1) --Add pre-collision listener to object 2 object2.preCollision = onPreCollision object2:addEventListener("preCollision", object2)

Note that Corona will still fire collision events for the two objects when they hit each other, so you can take appropriate action when they do.  Setting event.contact.isEnabled to false just makes sure they don’t disturb each other when they do hit.

There’s a full explanation of pre-collision events here: https://docs.coronalabs.com/api/event/preCollision/index.html

And a general tutorial on collisions here: https://docs.coronalabs.com/guide/physics/collisionDetection/

And this explanation is useful too: https://coronalabs.com/blog/2012/11/27/introducing-physics-event-contact/

1 Like

Nice, thank you!

Hi there,

You’ll need to implement a pre-collision listener on the objects to stop the physics engine from allowing them disturb each other when they collide.  To do that, you set event.contact.isEnabled to false inside the pre-collision listener.

A simple listener would look something like this…

local function preCollision(self, event)   --Ignore non-contact events   if (event.contact==nil) then return end      --Get the other object involved in the collision...   local object = event.other      --Tell Box 2D to ignore objects with the 'ghost' property.   if (object.ghost) then      event.contact.isEnabled=false   end end

Now add the ghost property to your two objects.

object1.ghost=true object2.ghost=true

And finally add the pre-collision listener to your objects.

Add pre-collision listener to object 1 object1.preCollision = onPreCollision object1:addEventListener("preCollision", object1) --Add pre-collision listener to object 2 object2.preCollision = onPreCollision object2:addEventListener("preCollision", object2)

Note that Corona will still fire collision events for the two objects when they hit each other, so you can take appropriate action when they do.  Setting event.contact.isEnabled to false just makes sure they don’t disturb each other when they do hit.

There’s a full explanation of pre-collision events here: https://docs.coronalabs.com/api/event/preCollision/index.html

And a general tutorial on collisions here: https://docs.coronalabs.com/guide/physics/collisionDetection/

And this explanation is useful too: https://coronalabs.com/blog/2012/11/27/introducing-physics-event-contact/

Nice, thank you!