Как непрерывно вращать тело с помощью 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)

// 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>


person rich    schedule 06.08.2017    source источник


Ответы (1)


Вы должны использовать угол увеличения цикла шестиугольника и установить вращение, используя 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);

The full code is here:

// 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>

person Greyson R.    schedule 05.05.2019