This translation is community contributed and may not be up to date. We only maintain the English version of the documentation. Read this manual in English
2つのオブジェクトが衝突すると、エンジンはイベントコールバックにイベントを送信するか、両方のオブジェクトにメッセージをブロードキャストします。
生成するイベントの種類は、各オブジェクトのフラグで制御できます。
これらはすべて既定で true です。
2つのコリジョンオブジェクト(collision object)が相互作用するとき、これらのチェックボックスに基づいて、ユーザーにメッセージを送信するかどうかを判定します。
たとえば、”Generate Contact Events” チェックボックスの場合は次のようになります。
physics.set_event_listener() を使用する場合:
| コンポーネント(component) A | コンポーネント B | メッセージの送信 |
|---|---|---|
| ✅︎ | ✅︎ | はい |
| ❌ | ✅︎ | はい |
| ✅︎ | ❌ | はい |
| ❌ | ❌ | いいえ |
既定のメッセージハンドラーを使用する場合:
| コンポーネント A | コンポーネント B | メッセージの送信(複数の場合あり) |
|---|---|---|
| ✅︎ | ✅︎ | はい (A,B) + (B,A) |
| ❌ | ✅︎ | はい (B,A) |
| ✅︎ | ❌ | はい (A,B) |
| ❌ | ❌ | いいえ |
"collision_response" メッセージは、衝突するオブジェクトの一方の種類が “dynamic”、”kinematic”、または “static” のときに送信されます。次のフィールドが設定されます。
other_idhash)other_positionvector3)other_grouphash)own_grouphash)collision_response メッセージは、オブジェクトが実際に交差している部分の詳細を必要としない衝突の処理にのみ適しています。たとえば、弾丸が敵に当たったかどうかを検出する場合です。衝突するオブジェクトの各ペアについて、このメッセージは1フレームにつき1つだけ送信されます。
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
"contact_point_response" メッセージは、衝突するオブジェクトの一方の種類が “dynamic” または “kinematic” で、もう一方の種類が “dynamic”、”kinematic”、または “static” のときに送信されます。次のフィールドが設定されます。
positionvector3)。normalvector3)。relative_velocityvector3)。distancenumber)。applied_impulsenumber)。life_timenumber)。massnumber)。other_massnumber)。other_idhash)。other_positionvector3)。other_grouphash)。own_grouphash)。オブジェクトを完全に分離する必要があるゲームやアプリケーションでは、"contact_point_response" メッセージから必要な情報をすべて取得できます。ただし、衝突の状況によっては、衝突する1組のオブジェクトについて、1フレームで複数の "contact_point_response" メッセージを受信することがあります。詳しくは衝突の解決を参照してください。
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
"trigger_response" メッセージは、衝突するオブジェクトの一方の種類が “trigger” のときに送信されます。このメッセージは、衝突が最初に検出されたときに1回送信され、オブジェクトが衝突しなくなったときにもう1回送信されます。次のフィールドがあります。
other_idhash)。entertrue、退出であれば false です(boolean)。other_grouphash)。own_grouphash)。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