To set a maximum angle for looking up and down with PointerLockControls, you need to limit the pitch rotation of the camera. In PointerLockControls, the vertical rotation (pitch) is managed by an internal object called pitchObject. Unfortunately, pitchObject is not directly exposed, but you can modify the onMouseMove method to clamp the pitch rotation within your desired limits.
Here's how you can achieve this:
-
Define Your Maximum and Minimum Pitch Angles: Decide the maximum and minimum angles (in radians) you want to allow for looking up and down.
-
Override the onMouseMove Method: After creating the PointerLockControls instance, override its onMouseMove method to include the clamping logic.
-
Clamp the Pitch Rotation: In your custom onMouseMove method, clamp the pitchObject.rotation.x value between your defined minimum and maximum pitch angles.
Here's your updated Camera class with the necessary changes:
import * as THREE from 'three';
import { PointerLockControls } from 'three/examples/jsm/controls/PointerLockControls.js';
import Game from './game.js';
export default class Camera {
constructor() {
this.game = new Game();
this.sizes = this.game.sizes;
this.scene = this.game.scene;
this.canvas = this.game.canvas;
// Define min and max pitch angles (in radians)
this.minPitchAngle = THREE.Math.degToRad(-80); // Look down limit
this.maxPitchAngle = THREE.Math.degToRad(80); // Look up limit
this.setInstance();
this.setControls();
// Lock pointer and request full screen on canvas click
this.canvas.onclick = () => {
this.controls.lock();
document.body.requestFullscreen();
};
}
setInstance() {
this.instance = new THREE.PerspectiveCamera(
75,
this.sizes.width / this.sizes.height,
0.1,
1000
);
this.instance.position.y = 10;
this.instance.position.z = 10;
this.instance.rotation.reorder('YXZ'); // Ensure proper rotation order
this.scene.add(this.instance);
}
setControls() {
this.controls = new PointerLockControls(this.instance, this.canvas);
// Save reference to `this` for use inside event listener
const self = this;
// Override the onMouseMove method
const originalOnMouseMove = this.controls.onMouseMove.bind(this.controls);
this.controls.onMouseMove = function (event) {
if (this.isLocked === false) return;
const movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
const movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
// Update yaw rotation (left/right)
this.yawObject.rotation.y -= movementX * 0.002;
// Update pitch rotation (up/down)
this.pitchObject.rotation.x -= movementY * 0.002;
// Clamp the pitch rotation
this.pitchObject.rotation.x = Math.max(
self.minPitchAngle,
Math.min(self.maxPitchAngle, this.pitchObject.rotation.x)
);
};
// Add event listeners to lock and unlock controls
this.controls.addEventListener('lock', () => {
document.addEventListener('mousemove', this.controls.onMouseMove, false);
});
this.controls.addEventListener('unlock', () => {
document.removeEventListener('mousemove', this.controls.onMouseMove, false);
});
}
resize() {
this.instance.aspect = this.sizes.width / this.sizes.height;
this.instance.updateProjectionMatrix();
}
}
Explanation:
-
Defining Pitch Limits: We set this.minPitchAngle and this.maxPitchAngle to limit how far the camera can look up and down. The angles are converted from degrees to radians using THREE.Math.degToRad().
-
Overriding onMouseMove: We store the original onMouseMove method in case we need to reference it, then override it with our own function. Inside our custom onMouseMove, we perform the usual rotation updates and then clamp pitchObject.rotation.x.
-
Clamping the Pitch: The clamping is done using Math.max() and Math.min() to ensure that pitchObject.rotation.x stays within the specified limits.
-
Event Listeners: Since we overrode the onMouseMove method, we need to ensure that our custom method is used when the pointer is locked and removed when unlocked. We add and remove the mousemove event listener accordingly.
Note: This solution relies on internal properties of PointerLockControls (yawObject and pitchObject), which are not officially part of its public API. This approach works with the current implementation of PointerLockControls in three.js, but future updates to the library may change these internal properties. Always check the latest version of the library and consider implementing a custom control if necessary.