We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.

How to use PointerLock correctly for a first-person camera

Home Forums Programming How to use PointerLock correctly for a first-person camera

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #13439
    webgl
    Customer

    In RunCode() I write:

    var container = document.getElementById('container');
    container.addEventListener('click', function () {
      container.requestPointerLock();
    }, false);

    The cursor is captured, but the camera is not controlled by the mouse, I can only move from the keyboard. I don’t know what’s missing.

    #13445

    After entering the PointerLock mode, you also need to move the camera according to mouse events:

    
    function onMouseMove(event) {
    
        var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
        var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
    
        ...
    }

    Alternatively, you can assign PointerLockControls to the camera, as shown in this demo: https://cdn.soft8soft.com/demo/examples/index.html?q=pointerlo#misc_controls_pointerlock

    Chief 3D Verger | LinkedIn | Twitter

    #13450
    webgl
    Customer

    Good, thanks:

            container.addEventListener("mousemove", onMouseMove, false);
    
            function onMouseMove(event) {
    
                var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
                var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
    
                app.camera.rotateX(-movementY/1000)
                app.camera.rotateY(-movementX/1000)
    
            }
    #47832

    update: pointerlock is now supported natively in verge3d for first-person cameras. See more details here: https://www.soft8soft.com/topic/implementing-pointer-lock/#post-47831

    Co-founder and lead developer at Soft8Soft.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.