three.js中的第三人称控制

时间:2019-02-13 16:09:59

标签: javascript three.js

如何将对象旋转到看摄像机的一侧?

class ThirdPersonCamera extends Camera {
    boot(domElement) {
        this.cameraManager = new CameraManager(domElement);
    }

    tick(target = null) {
        if (target !== null) {
            this.cameraManager.target.applyMatrix4(target.matrixWorld);
            this.cameraManager.target.set(target.position.x, target.position.y + 2, target.position.z);
        }

        let targetX = this.cameraManager.target.x;
        let targetY = this.cameraManager.target.y;
        let targetZ = this.cameraManager.target.z;

        let radius = this.cameraManager.radius;
        let theta = this.cameraManager.theta;
        let phi = this.cameraManager.phi;

        let posX = targetX + radius * Math.sin(theta * Math.PI / 360) * Math.cos(phi * Math.PI / 360);
        let posY = targetY + radius * Math.sin(phi * Math.PI / 360);
        let posZ = targetZ + radius * Math.cos(theta * Math.PI / 360) * Math.cos(phi * Math.PI / 360);


        this.position.set(posX, posY, posZ);
        this.lookAt(this.cameraManager.target);
    }
}

函数 tick 中的-目标是我的对象http://joxi.net/bmoYbaBIxVNM8A

这是我的CameraManager:

class CameraManager extends Manager {
    implements() {
        this.sensitivity = new Vector2(1, 1);
        this.target = new Vector3();
        this.character = {};

        this.radius = 15;
        this.theta = 0;
        this.phi = 0;
    }

    realised(deltaX, deltaY) {
        this.theta -= deltaX * this.sensitivity.x;
        this.theta %= 720;

        this.phi += deltaY * this.sensitivity.y;
        this.phi = Math.min(170, Math.max(-170, this.phi));
    }

    event() {
        this.boundLockedPointer = () => this.domElement.requestPointerLock();
        this.boundMouseMove = event => this.realised(event.movementX, event.movementY);

        this.domElement.addEventListener('mousedown', this.boundLockedPointer, false);
        this.domElement.addEventListener('mousemove', this.boundMouseMove, false);
    }
}

当我移动鼠标时,我的相机只是绕着我的物体旋转,但是我只想用相机沿Y轴旋转物体

我如何得到它?谢谢

0 个答案:

没有答案