• cocos creator基础-(二十)物理引擎碰撞检测


    1: 理解物体类型和分类,配置碰撞矩阵;
    2: 编写碰撞响应函数,监听碰撞事件;
    3: 学会了解Sensor来做触发器,只触发碰撞不改变运动;

    物体类型与碰撞矩阵

    1: 添加物体类型: Add Layer, 每个类型对应一个名字group与groupIndex
    2: 创建物体的时候要选择一个类型;
    3: 配置碰撞矩阵,决定哪些物体类型碰撞;

    碰撞事件监听

    1: 刚体组件开启碰撞监听;
    2: 当有碰撞发生的时候,遍历刚体所在的节点所挂的所有的组件,看组件是否实现了碰撞检测函数,如果是,那么调用;
    3: 在需要检测碰撞的组件代码里面编写碰撞响应函数:
      onBeginContact ( contact, selfCollider, otherCollider): 碰撞开始
      onEndContact (contact, selfCollider, otherCollider): 碰撞结束
      onPreSolve(contact, selfCollider, otherCollider); 碰撞持续,接触时被调用;
      onPostSolve (contact, selfCollider, otherCollider); 碰撞接触更新完后调用,可以获得冲量信息

    4: 如果把碰撞器设置成了sensor,那么只会做碰撞检测,而不会改变物体碰撞后的运动状态;
      sensor: 用于触发器: 道具, 关卡的出口,入口等;

    cc.Class({
        extends: cc.Component,
    
        properties: {
            // foo: {
            //    default: null,      // The default value will be used only when the component attaching
            //                           to a node for the first time
            //    url: cc.Texture2D,  // optional, default is typeof default
            //    serializable: true, // optional, default is true
            //    visible: true,      // optional, default is true
            //    displayName: 'Foo', // optional
            //    readonly: false,    // optional, default is false
            // },
            // ...
        },
    
        // use this for initialization
        onLoad: function () {
    
        },
    
        // contact 是碰撞对象,本次碰撞的信息
        // selfCollider: 是自己的碰撞器组件
        // otherCollider: 碰撞到的碰撞器组件;
        // 我们可以有碰撞器组件,来获取我们的碰撞的节点对象
        // 碰撞开始
        onBeginContact: function ( contact, selfCollider, otherCollider) {
            console.log("onBeginContact:", otherCollider.node.name, " ", selfCollider.node.name);
            console.log("onBeginContact", selfCollider.node.group, otherCollider.node.group);
            console.log("onBeginContact", selfCollider.node.groupIndex, otherCollider.node.groupIndex);
        },
    
        // 碰撞结束
        onEndContact: function(contact, selfCollider, otherCollider) {
            console.log("onEndContact");
        },
    
        // 碰撞持续
        onPreSolve: function(contact, selfCollider, otherCollider) {
            console.log("onPreSolve function");
        },
    
        // 计算完本次碰撞持续后,调用这个
        onPostSolve: function (contact, selfCollider, otherCollider) {
            console.log("onPostSolve");
        }
        // called every frame, uncomment this function to activate update callback
        // update: function (dt) {
    
        // },
    });
  • 相关阅读:
    麻省理工公开课:线性代数 第4课 A的LU分解
    麻省理工公开课:线性代数 第3课 乘法和逆矩阵
    麻省理工公开课:线性代数 第2课 矩阵消元
    麻省理工公开课:线性代数 第1课 方程组的几何解释
    线性代数导论(一)向量介绍
    鸟哥的linux私房菜——第6章 Linux的文件权限与目录配置
    Python学习(七)数组读写和保存
    Python学习(六)向量化
    Python学习(五)Numpy通用函数汇总
    Python学习(四)数组和矩阵
  • 原文地址:https://www.cnblogs.com/orxx/p/10528530.html
Copyright © 2020-2023  润新知