使用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
相关产品推荐
相关产品推荐

