小编ric*_*ich的帖子

如何使用 Matter.js 连续旋转物体?

我是物理引擎的新手,但要开始我设想的项目,我需要使六边形在页面中心的固定位置连续旋转。

我想我从根本上误解了物理引擎的工作原理,但是当我调用 Matter.Body.rotate(hexagon, 1); 时 当由提供的参数 (1) 渲染时,它只是立即旋转六边形,并且不会旋转得更远。怎样才能让它连续旋转呢?

这是我的代码:

请注意,设置 setStatic 是为了使六边形不会掉出框架。

// module aliases
var Engine = Matter.Engine,
    Render = Matter.Render,
    World = Matter.World,
    Bodies = Matter.Bodies;
    Composites = Matter.Composites;

// create an engine
var engine = Engine.create();

// create a renderer
var render = Render.create({
    element: document.body,
    engine: engine
});


var hexagon = Bodies.polygon(375, 300, 6, 200, {inertia: Infinity}); // setting inertia to inifinty will prevent rotation upon collision
Matter.Body.setStatic(hexagon, true);
Matter.Body.rotate(hexagon, 1);
console.log(hexagon);
// Matter.Body.rotate(hexagon, 1)

// …
Run Code Online (Sandbox Code Playgroud)

javascript rotation physics-engine matter.js

6
推荐指数
1
解决办法
3924
查看次数

标签 统计

javascript ×1

matter.js ×1

physics-engine ×1

rotation ×1