When two objects collide, the engine will broadcast messages to all components in both objects:
The "collision_response"
message is sent for all collision objects. It has the following fields set:
other_id
hash
)other_position
vector3
)other_group
hash
)The collision_response message is only adequate to resolve collisions where you don’t need any details on the actual intersection of the objects, for example if you want to detect if a bullet hits an enemy. There is only one of these messages sent for any colliding pair of objects each frame.
function on_message(self, message_id, message, sender)
-- check for the message
if message_id == hash("collision_response") then
-- take action
print("I collided with", message.other_id)
end
end
The "contact_point_response"
message is sent when one of the colliding objects is dynamic or kinematic. It has the following fields set:
position
vector3
).normal
vector3
).relative_velocity
vector3
).distance
number
).applied_impulse
number
).life_time
number
).mass
number
).other_mass
number
).other_id
hash
).other_position
vector3
).group
hash
).For a game or application where you need to separate objects perfectly, the "contact_point_response"
message gives you all information you need. However, note that for any given collision pair, several "contact_point_response"
messages can be received each frame, depending on the nature of the collision. See Resolving collisions for more information.
function on_message(self, message_id, message, sender)
-- check for the message
if message_id == hash("contact_point_response") then
-- take action
if message.other_mass > 10 then
print("I collided with something weighing more than 10 kilos!")
end
end
end
The "trigger_response"
message is sent when a colliding object is of type “trigger”.
In a trigger collision "collision_response"
messages are sent. In addition, triggers also send a special "trigger_response"
message when the collision begins and ends. The message has the following fields:
other_id
hash
).enter
true
if the interaction was an entry into the trigger, false
if it was an exit. (boolean
).function on_message(self, message_id, message, sender)
-- check for the message
if message_id == hash("trigger_response") then
if message.enter then
-- take action for entry
print("I am now inside", message.other_id)
else
-- take action for exit
print("I am now outside", message.other_id)
end
end
end
Did you spot an error or do you have a suggestion? Please let us know on GitHub!
GITHUB