matter.js 创建一个旋转平台

matter.js create a rotating platform

我需要创建一个可以用鼠标旋转的平台。我试过创建一个矩形,然后用Matter.Constraint.create()连接到那个点上,但是这样的平台是不能用鼠标旋转的。我没有在演示或示例中找到替代方案

这是我使用的代码:

(function(){
    // Matter.js module aliases
    var Engine = Matter.Engine;
        World = Matter.World;
        Render = Matter.Render;
        Bodies = Matter.Bodies;
        Composites = Matter.Composites;
        Constraint = Matter.Constraint;
        MouseConstraint = Matter.MouseConstraint;

    // create a Matter.js engine
    var _engine = Engine.create(document.body);

    var rotatingPlatform = Bodies.rectangle(100, 200, 200, 30);

    World.add(_engine.world, [
    MouseConstraint.create(_engine),
    rotatingPlatform,
    Constraint.create({ pointA: {x: 100, y: 200}, bodyB: rotatingPlatform}),
    ]);

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

和标记:

<!doctype html>
<html>
<head></head>
<body>
    <script src="matter-0.8.0.js"></script>
    <script src="myJsFile.js"></script>
</body>
</html>

如何让这个平台随鼠标旋转?

这个问题有点老,但是没有人回答。

将此添加到脚本底部对我有用:

Matter.Events.on(_engine, "mousemove",  function(event) {
    if (Matter.Bounds.contains(rotatingPlatform.bounds, event.mouse.position) && event.mouse.button == 0) {
        targetAngle = Matter.Vector.angle(rotatingPlatform.position, event.mouse.position);
        Matter.Body.rotate(rotatingPlatform, targetAngle - rotatingPlatform.angle);
    }
});
_engine.world.gravity.y = 0;

我喜欢这个想法,@oxdeadbeef,对我来说奇怪的是,mousemove 事件从未触发。我最终这样做了,效果很好:

document.body.addEventListener("mousemove", function(event) {
  var mousePosition = {x: event.offsetX, y: event.offsetY};
  if (Matter.Bounds.contains(rotatingPlatform.bounds, mousePosition) && event.button == 0) {
    targetAngle = Matter.Vector.angle(rotatingPlatform.position, mousePosition);
    Matter.Body.rotate(rotatingPlatform, targetAngle - rotatingPlatform.angle);
  }
});

再次感谢您的想法。