PhysicsJS - 如何旋转元素以查看鼠标位置

时间:2014-06-16 21:22:06

标签: physicsjs

我正在尝试旋转矩形以始终指向鼠标位置。

我试过这个:

document.getElementById('viewport').onmousemove = function(e){

     var scratch = Physics.scratchpad();
     mousePos = scratch.vector().set(e.x, e.y);

    var newAngle = box.state.pos.angle(mousePos);
    box.state.angular.pos = newAngle;

    scratch.done();
};

感谢。

1 个答案:

答案 0 :(得分:1)

也许尝试这样的事情:

document.getElementById('viewport').onmousemove = function(e){

    var scratch = Physics.scratchpad();
    // assuming your viewport is the whole screen
    mousePos = scratch.vector().set(e.pageX, e.pageY); 
    mousePos.vsub( box.state.pos ); // get vector pointing towards mouse pos

    var newAngle = mousePos.angle(); // get angle with respect to x axis
    box.state.angular.pos = newAngle;

    scratch.done();
};