目次
以前やっていたのは2.x版で、最近3.6に切り替えて記録
- 2D コリジョン コールバック · Cocos Creator
- バージョン 3.6
- この記事は監視のみを行い、実際の衝突は生成しません
1.衝突監視をオンにする
プロジェクト - プロジェクト設定 - フィーチャ クリッピング
Box2D 物理モジュールは、対応するコールバックが生成されるように、 最初に Rigidbody で 衝突監視を有効にする必要があります。この記事は監視のみを行い、実際の衝突を生成せず、組み込みの 2D を選択し、Rigidbody2Dコンポーネントを追加しません。
2.衝突コールバックをオンにします
BoxCollider2Dコンポーネントをノードに追加します
- 組み込みの物理モジュールには、衝突コールバックを生成するためのコライダー コンポーネントのみが必要です。(この記事)
- Sensor は、衝突コンポーネントがセンサー タイプであるかどうかを示します。センサー タイプの衝突コンポーネントは、衝突コールバックを生成しますが、物理的な衝突効果は発生しません。PS: スクリプトは、この項目がチェックされているノードでハングします。
- Box2D を開く方法は、 Rigidbody2Dプロパティ インスペクターでEnabledContactListenerプロパティを 確認する ことです ( Rigidbody2D コンポーネント を追加する必要があります)。
3番目に、コールバック関数を登録します
登録方法:
- 指定されたコライダーを介して登録する
- グローバル コールバック関数を 2D 物理システムに登録する
Builtin 2D physics モジュールは、BEGIN_CONTACT および END_CONTACT コールバック メッセージのみを送信します。
スクリプト Player.ts を作成します。
import { _decorator, Component, Node, BoxCollider2D, Contact2DType, PhysicsSystem2D, Collider2D, IPhysics2DContact } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('Player')
export class Player extends Component {
start() {
// 注册单个碰撞体的回调函数
let collider = this.getComponent(BoxCollider2D);
if (collider) {
// Builtin 2D 物理模块只会发送 BEGIN_CONTACT 和 END_CONTACT 回调消息。
collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
// collider.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
// collider.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
}
// 注册全局碰撞回调函数
if (PhysicsSystem2D.instance) {
// Builtin 2D 物理模块只会发送 BEGIN_CONTACT 和 END_CONTACT 回调消息。
PhysicsSystem2D.instance.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
PhysicsSystem2D.instance.on(Contact2DType.END_CONTACT, this.onEndContact, this);
// PhysicsSystem2D.instance.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
// PhysicsSystem2D.instance.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
}
}
/**
* 只在两个碰撞体开始接触时被调用一次
* @param selfCollider 指的是回调脚本的节点上的碰撞体
* @param otherCollider 指的是发生碰撞的另一个碰撞体
* @param contact 碰撞主要的信息, 位置和法向量, 带有刚体的本地坐标来,
*/
onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
console.log('onBeginContact');
// contact.getWorldManifold在 Builtin 物理模块这个参数为空
// const worldManifold = contact.getWorldManifold();
// // 碰撞点数组
// const points = worldManifold.points;
// // 碰撞点上的法向量
// const normal = worldManifold.normal;
}
onEndContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// 只在两个碰撞体结束接触时被调用一次
console.log('onEndContact');
}
onPreSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// 每次将要处理碰撞体接触逻辑时被调用
console.log('onPreSolve');
}
onPostSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// 每次处理完碰撞体接触逻辑时被调用
console.log('onPostSolve');
}
}
テスト
キャラクターが金貨と衝突したときにトリガーされます
他の
// 节点名字和标签
console.log(otherCollider.node.name);
console.log(otherCollider.tag);
衝突後に消える2つの方法
if (otherCollider.tag == 3) {
// 不会从内存中释放, 默认传入false, 会清空节点上绑定的事件和 action
// 文档:https://docs.cocos.com/creator/manual/zh/scripting/basic-node-api.html?h=removefromparent
// otherCollider.node.removeFromParent();
// 节点不再使用
otherCollider.node.destroy();
}