【Cocos入门】物理系统(物理碰撞)

在这里插入图片描述

物理碰撞

物理引擎默认是关闭状态以节省资源开销。开启方法和之前的普通碰撞类似:cc.director.getPhysicsManager().enabled = true但有一个区别,物理引擎的开启必须放在onLoad函数内运行,否则不生效。
物理碰撞组件也同样具有碰撞回调函数。但如果要使用回调函数,则必须开启刚体的 EnableContact Listener 或者 在代码中写rb.enableContactlistener = true在这里插入图片描述

物理碰撞组件有4个回调函数:

  • onBeginContact(contact,self other)–当碰撞体开始接触时被调用一次
  • onEndContact(contact,self other)–当碰撞体结束接触时被调用一次
  • onPreSolve(contact,self other)–每次将要处理碰撞体接触逻辑时被调用
  • onPostSolve(contact,self other)–每次处理完成碰撞体接触逻辑时被调用

执行顺序:onBeginContact > onPreSolve > onPostSolve > onEndContact
上述回调函数的参数解释如下:
contact: cc.PhysicsContact 碰撞信息
self: cc.PhysicsCollider 自己
other:cc,PhysicsCollider 碰撞的另一个物体
关于物理碰撞更多详情

示例:

    manager:cc.PhysicsManager;

    onLoad(){
   
        this.manager=cc.director.getPhysicsManager();
        this.manager.enabled = true;
    }

    onBeginContact(){
   
        console.log("触发");
    }

勾选Sensor,物体之间不会发生碰撞,依旧触发碰撞回调函数

在这里插入图片描述

相关推荐

  1. Cocos Creator 3D物理引擎的碰撞检测与触发器详解

    2024-02-16 15:20:02       30 阅读
  2. Cocos Creator 3D物理引擎的物体编辑详解

    2024-02-16 15:20:02       41 阅读
  3. 【名词解释】Unity中的3D物理系统碰撞

    2024-02-16 15:20:02       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-16 15:20:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-16 15:20:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-16 15:20:02       82 阅读
  4. Python语言-面向对象

    2024-02-16 15:20:02       91 阅读

热门阅读

  1. redis中key到了过期时间怎么删除

    2024-02-16 15:20:02       61 阅读
  2. Node.js开发-包管理工具

    2024-02-16 15:20:02       58 阅读
  3. LeetCode 31天

    2024-02-16 15:20:02       57 阅读
  4. Mysql学习笔记

    2024-02-16 15:20:02       47 阅读
  5. Redis 的 SETNX

    2024-02-16 15:20:02       47 阅读
  6. PointMixer论文阅读笔记

    2024-02-16 15:20:02       60 阅读
  7. Rust基础拾遗--并发和异步编程

    2024-02-16 15:20:02       47 阅读
  8. Vue路由的传参

    2024-02-16 15:20:02       51 阅读
  9. 正则表达式

    2024-02-16 15:20:02       58 阅读