Konz
Konz

Reputation: 13

PhysicsJS - How to rotate an element to look at the mouse position

I'm trying to rotate a rectangle to always point at the mouse position.

I've tried this:

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();
};

Thanks.

Upvotes: 1

Views: 290

Answers (1)

Jasper
Jasper

Reputation: 1193

Maybe try something like this:

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();
};

Upvotes: 1

Related Questions