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

Matter.js调用setVertices后碰撞未更新问题求助

Matter.js 更新刚体顶点后碰撞检测未同步问题

在游戏开发中使用matter.js物理引擎时,调用Body.setVertices方法更新静态刚体(ground)的顶点后,碰撞检测并未同步更新,导致圆形物体无法正常下落。以下是简化代码示例,如需完整可运行示例可提供:

// 初始化引擎
const engine = Engine.create();
const runner = Runner.create();
Runner.run(runner, engine);

// 添加用于检测碰撞的圆形物体
const circle = Bodies.polygon(300, 100, 5, 12, {
    frictionAir: 0, friction: 0.0001, restitution: 0.6
});
World.add(engine.world, circle);

// 从顶点创建刚体(顶点由SVG路径解析而来)
// 原始顶点
const mapVertexes = [[..]];
const ground = Bodies.fromVertices(400, 400, mapVertexes, { isStatic: true }, true);
World.add(engine.world, ground);

// 点击时更新多边形顶点
const newVertices = [...]
Body.setVertices(ground, newVertices);

更新前效果:
更新前效果

更新后效果:
更新后效果

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 18:48:24