egret p2物理引擎 遇到的坑(1)

  • 直接将pythsic包丢到libs目录下并且修改egretPropertis.json文件

TypeError [ERR_INVALID_ARG_TYPE]: The "to" argument must be of type string. Received type object
at validateString (internal/validators.js:125:11)
at Object.relative (path.js:493:5)
at C:\Users\mi\AppData\Roaming\EgretLauncher\download\EgretCompiler\@egret\egret-webpack-bundler\lib\egretproject\data.js:149:34
at Array.map (<anonymous>)
at EgretProjectData.getModulesConfig (C:\Users\mi\AppData\Roaming\EgretLauncher\download\EgretCompiler\@egret\egret-webpack-bundler\lib\egretproject\data.js:145:51)
at Object.getLibsFileList (C:\Users\mi\AppData\Roaming\EgretLauncher\download\EgretCompiler\@egret\egret-webpack-bundler\lib\egretproject\index.js:8:24)
at EgretWebpackBundler.startDevServer (C:\Users\mi\AppData\Roaming\EgretLauncher\download\EgretCompiler\@egret\egret-webpack-bundler\lib\index.js:47:38)
at C:\Users\mi\Documents\EgretProjects\keli_go\scripts\plugins\webpack-plugin.ts:59:21
at new Promise (<anonymous>)

原因分析

一般都是路径有问题,没有配置正确的p2包的具体文件路径**,to入参接收的是对象参数

解决方案

需要在egretProperties.json文件配置具体的path路径,具体如下:将physics.d.ts/physics.js/physics.min.js 放到 根目录/../physics目录下

然后将egretProperties.json添加配置项

{
"name":"physics",
"path":"../physics"
}
  • 修改配置文件和地址之后虽然编译不报错,但是在浏览器创建p2对象的时候还是会有找不到p2这个对象的问题

    Main.ts:89 Uncaught (in promise) ReferenceError: p2 is not defined
    at HelloWorld../src/Main.ts.HelloWorld.createWorld (Main.ts:89:22)
    at HelloWorld.<anonymous> (Main.ts:24:10)
    at step (main.js:80:19)
    at Object.next (main.js:61:49)
    at fulfilled (main.js:51:54)

    解决方案:

    需要使用`egret`自带的`wing`编辑器找到 插件-Egret项目工具-编译引擎
    点击进行编译,编译之后,再次打开项目即可
    ![截图](9e3c157f63630e22e7e4397976151e41.png)

    或者使用代码 egret build -e尝试进行手动编译

    编译成功的标志是截图
    image

    libs/modules/路径下有编译出来的physics库文件目录


  • wrold,plane(地平线),物体都已经设置好并且添加到world中,但是并没有显示图案

    截图
    image

    可能原因:没有给相关物理引擎中的物体绑定贴图,绑定贴图通过,p2.Body对象的displays属性进行绑定,示例

    this.display = new egret.Shape()
    this.display.x = 100
    this.display.graphics.beginFill(0xff0000,1)
    this.display.graphics.drawCircle(0,0,(<p2.Box>boxShape).width)
    this.display.graphics.endFill()
    // this.display.width = (<p2.Box>bfoxShape).width
    // this.display.height = (<p2.Box>boxShape).height
    boxBody.displays = [this.display]

    通过绑定贴图之后,可以看到物理引擎中的body刚体对象也有了相应的贴图

  • 刚体设置材质为STATIC,物体碰撞仍然能够穿过物体表面

    2.gif
    image


地面刚体初始化的属性是这样的

new p2.Body({
type:p2.Body.STATIC,
position:[0,stageHeight -100]
})

球体刚体初始化的属性是这样的

new p2.Body({ mass: 1, position: [200, 200], angularVelocity: 1})

这里刚体的type一共有三种类型:

Dynamic :动态, Dynamic类型刚体可以与任何类型的刚体交互,可以移动。

STATIC: 静态,Static类型刚体不可以移动,但是可以与 dynamic类型刚体交互。

Kinematic: 动态刚体,Kinematic类型刚体通过设置速度来控制,其他方面则和Static刚体相同。

讲道理这里设置了STATIC应该在和物体碰撞的时候不会发生穿破的现象

  1. 首先怀疑的是球体是不是没有设置刚体的原因,将球体的type同样设置成 STATIC试一下

    。。。。emmm发现自己是个智障,前面刚刚说了STATIC类型不能移动,如下图,完全不动

    截图
    image

  2. 怀疑球体shape形状有问题,目前的设置是这样的

    var boxShape:p2.Shape = new p2.Box({width:20,height:20})
    暂时没解决,今天有点晚,准备休息了
  • world 和 地平线,小球都设置好了,或者说怎么让物理引擎运动起来

    截图
    image

可能的情况有以下几种

  1. 设置刚体的type类型都是static ,静态的刚体是不能有位移发生
  2. 没有调world.step()步进函数,使物理世界按照公式轨迹运行,使用定时器,或者在egret帧刷新事件绑定step函数,并且在函数中刷新egret视图位置
    //帧事件,步函数
    private update() {
    this.world.step(1);
    var l = this.world.bodies.length;
    for (var i:number = 0; i < l; i++) {
    var boxBody:p2.Body = this.world.bodies[i];
    var box:egret.DisplayObject = boxBody.displays[0];
    if (box) {
    //将刚体的坐标和角度赋值给显示对象
    box.x = boxBody.position[0];
    box.y = boxBody.position[1];
    //如果刚体当前状态为睡眠状态,将图片alpha设为0.5,否则为1
    if (boxBody.sleepState == p2.Body.SLEEPING) {
    box.alpha = 0.5;
    }
    else {
    box.alpha = 1;
    }
    }
    }
    }
    主函数中,监听事件
    this.addEventListener(egret.Event.ENTER_FRAME,this.update,this);
  3. 物体没有设置mass重量属性,或者视图绑定有问题
posted @   三十男  阅读(348)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
点击右上角即可分享
微信分享提示