如何使用matter.js实现连续旋转一个物体?

6

我对物理引擎还不太了解,但为了开始一个我正在构思的项目,我需要让一个六边形在页面中心的固定位置不断旋转。

我想我可能基本上误解了物理引擎的工作原理,但当我调用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)

// add all of the bodies to the world
World.add(engine.world, [hexagon]);

// run the engine
Engine.run(engine);


// run the renderer
Render.run(render);
<!DOCTYPE html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/p5.js"></script>
    <!-- <script src="matter.js" type="text/javascript"></script> -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.12.0/matter.js"></script>
    <!-- <script async src="hexagondwana.js"></script> -->
    <script async src="hex_no_p5.js"></script>
  </head>
  <body>
  </body>
</html>

1个回答

3
你应该使用循环来增加六边形的角度,并使用 Matter.Body.setAngle 设置旋转。代码如下所示:

你应该使用循环来增加六边形的角度,并使用 Matter.Body.setAngle 设置旋转。代码如下所示:

var hexagon = Bodies.polygon(375, 300, 6, 200, {
    isStatic: true,
    inertia: Infinity,// setting inertia to infinty will prevent rotation upon collision
    rotationSpeed: 1 // Optional - you could substitute hexagon.rotationSpeed in updateRotation() with this number
});
World.add(world, [hexagon]);  

function updateRotation() {
    Matter.Body.setAngle(hexagon, hexagon.angle + hexagon.rotationSpeed);
    requestAnimationFrame(updateRotation);
}
window.requestAnimationFrame(updateRotation);

// module aliases
var Engine = Matter.Engine,
    Render = Matter.Render,
    World = Matter.World,
    Body = Matter.Body,
    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, {
    isStatic: true,
    inertia: Infinity,// setting inertia to infinty will prevent rotation upon collision
    rotationSpeed: 1 // Optional
}); 
// add all of the bodies to the world
World.add(engine.world, [hexagon]);

// run the engine
Engine.run(engine);

function updateRotation() {
    Body.setAngle(hexagon, hexagon.angle + hexagon.rotationSpeed);
    requestAnimationFrame(updateRotation);
}
window.requestAnimationFrame(updateRotation);



// run the renderer
Render.run(render);
<!DOCTYPE html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/p5.js"></script>
    <!-- <script src="matter.js" type="text/javascript"></script> -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.12.0/matter.js"></script>
    <!-- <script async src="hexagondwana.js"></script> -->
    <script async src="hex_no_p5.js"></script>
  </head>
  <body>
  </body>
</html>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接