javascriptbabylonjspointerlock

Moving a player towards the mouse direction?


I'm looking for a simple line of code or an API, if it is as simple as that.

I am using BabylonJS to make an online game, and I have run into a problem. I can't find anything on the documentation to support my problem. I am using

canvas.requestPointerLock = canvas.requestPointerLock || canvas.mozRequestPointerLock;
    canvas.requestPointerLock();

To lock the mouse, when normally you have to click and drag. I am also using this movement script for basic movement:

function KEY_DOWN(event)
{
    if (event.keyCode == 87)
    {
        player.position.z -= 0.5;
    }
    else if (event.keyCode == 65)
    {
        player.position.x += 0.5;
    }
    else if (event.keyCode == 83)
    {
        player.position.z += 0.5;
    }
    else if (event.keyCode == 68)
    {
        player.position.x -= 0.5;
    }
}

This only transforms on an axis unfortunately. Is there any way to determine where my mouse is looking and move towards that point, and not just move on a fixed axis? I can't seem to find anything that has the exact methods I need. Thanks!


Solution

  • I don't know of any API, but it shouldn't be so bad, if I understand your problem correctly. You'll need to hook into the mousemove event after the point has been locked, and instead of the simple handling like shown here: http://www.html5rocks.com/en/tutorials/pointerlock/intro/ you would need to do your vector calculations against each vector of movement possible with the keys in that handler. Effectively, you would compute the x, y, z deltas that each key press corresponds to within your pointerlockchange handler based on the mouse deltas. Probably not as bad as it sounds, since your mouse deltas are only going to affect up to 2 axes at a time.