You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用matter-js的fromVertices创建Body时顶点位置异常及穿透问题

Matter.js 自定义顶点物理体异常问题

我需要实现带有顶点标记的大炮形状物理体,通过PhysicsEditor生成了包含顶点数据的JSON配置文件:

{
    "cannon_transparent_sm": {
        "label": "cannon",
        "density": 0.1,
        "restitution": 0,
        "friction": 0.1,
        "frictionAir": 0.01,
        "frictionStatic": 0.5,
        "fixtures": [
            {
                "label": "cannon_fixture",
                "vertices": [
                    [ { "x":150, "y":45 }, { "x":134, "y":0 }, { "x":18, "y":30 }, { "x":1, "y":56 }, { "x":21, "y":94 }, { "x":86, "y":76 } ],
                    [ { "x":43, "y":17 }, { "x":44, "y":23 }, { "x":59, "y":19 }, { "x":58, "y":14 } ],
                    [ { "x":6, "y":41 }, { "x":1, "y":56 }, { "x":18, "y":30 } ],
                    [ { "x":6, "y":83 }, { "x":21, "y":94 }, { "x":1, "y":56 }, { "x":2, "y":72 } ],
                    [ { "x":86, "y":76 }, { "x":21, "y":94 }, { "x":25, "y":108 }, { "x":48, "y":124 }, { "x":61, "y":124 }, { "x":73, "y":119 }, { "x":83, "y":109 }, { "x":88, "y":95 } ],
                    [ { "x":34, "y":118 }, { "x":48, "y":124 }, { "x":25, "y":108 } ]
                ]
            }
        ]
    }
}

随后使用matter-js的Bodies.fromVertices()方法创建物理体并添加到2D物理世界,代码如下:

var cannonBodyDef = {
  position: Vector.create(300, 50),
  density: this._cannon.density,
  restitution: this._cannon.restitution,
  friction: this._cannon.friction,
  frictionStatic: this._cannon.frictionStatic,
  frictionAir: 0.9, //this._cannon.frictionAir
};

var cannon = Bodies.fromVertices(300, 200, this._cannon.fixtures[0].vertices, cannonBodyDef);

World.add(engine.world, cannon);

现在遇到两个问题:

  • 实际渲染的物理体各顶点组相对位置异常,和预期的大炮形状不符
  • 该物理体落到设置为static: true的地面后,会先轻微晃动,然后直接穿透地面,不确定是否和顶点位置异常相关

内容的提问来源于stack exchange,提问作者PeteGO

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 15:05:24