How to know which collision object is colliding with other?

I have a enemy with 3 collision objects and i want to do different actions depending on which collision object is detecting the collision. How i do that?

And another question. Is possible to stop detecting the collisions of a specific group? For example, when my main character is dead, then he stops detecting the collision with group “enemies” but not with group “terrain”.

The url to the collisionobject that has detected the collision will be available in the sender parameter of on_message. You can also check the property message.group to see which group has triggered the collision.
To stop detecting collisions on an object, simply send a disable message to it, so:

function on_message(self, message_id, message, sender)
  if sender==msg.url("#collisionobject") then
    --do something
    msg.post(sender, "disable")
  end
end
4 Likes

ok, thank you for your help!

other question, you know if it possible to disable a specific mask? instead of disable the whole collision object

No, you would need to do that manually. Set allt he masks that the object would collide with at any point, and then in code do something like:

function init(self)
  self.mask={
    [hash("ground")]=true,
    [hash("enemy")]=true
  }
end

function on_message(self, message_id, message, sender)
  if message_id==COLLISION_RESPONSE and mask[message.group] then
    --do collision logic
    self.mask[message.group]=false
  end
end
2 Likes

ok, thank you again :smile: