To set a maximum rotation for looking up and down (which corresponds to rotation around the x-axis in three.js), you can clamp the pitch (vertical) rotation of the camera within desired limits. Here's how you can achieve this:
-
Access the Pitch Object:
In PointerLockControls, the camera is nested inside two objects: a yawObject (for horizontal rotation) and a pitchObject (for vertical rotation). You can access the pitchObject by navigating through the controls object's hierarchy.
-
Define Minimum and Maximum Pitch Angles:
Decide the maximum angles you want for looking up and down. These should be in radians. For example, to limit the rotation to 45 degrees up and down:
const maxPitch = THREE.MathUtils.degToRad(45); // Convert degrees to radians
const minPitch = THREE.MathUtils.degToRad(-45);
-
Clamp the Pitch Rotation:
In your animation loop or an update function that's called on every frame, use THREE.MathUtils.clamp() to restrict the rotation.x of the pitchObject within the defined limits.
Here's how you can modify your Camera class:
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;
this.setInstance();
this.setControls();
// Access the pitchObject to control vertical rotation
this.pitchObject = this.controls.getObject().children[0];
// Define min and max pitch angles (in radians)
this.minPitch = THREE.MathUtils.degToRad(-45); // Looking down limit
this.maxPitch = THREE.MathUtils.degToRad(45); // Looking up limit
// Lock pointer and request fullscreen 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');
this.scene.add(this.instance);
}
setControls() {
this.controls = new PointerLockControls(this.instance, this.canvas);
}
resize() {
this.instance.aspect = this.sizes.width / this.sizes.height;
this.instance.updateProjectionMatrix();
}
// Add an update function to be called every frame
update() {
// Clamp the pitch (vertical) rotation
this.pitchObject.rotation.x = THREE.MathUtils.clamp(
this.pitchObject.rotation.x,
this.minPitch,
this.maxPitch
);
}
}
Explanation:
-
Accessing pitchObject:
this.pitchObject = this.controls.getObject().children[0];
This line gets the first child of the controls' object, which is the pitchObject responsible for vertical rotation.
-
Defining Pitch Limits:
this.minPitch = THREE.MathUtils.degToRad(-45);
this.maxPitch = THREE.MathUtils.degToRad(45);
Here, we're setting the minimum and maximum angles for looking down and up, respectively.
-
Clamping Rotation in update():
this.pitchObject.rotation.x = THREE.MathUtils.clamp(
this.pitchObject.rotation.x,
this.minPitch,
this.maxPitch
);
The clamp function ensures that rotation.x (the pitch) stays within the specified limits.
Remember to Call update():
In your main animation loop or wherever you're updating your scene, make sure to call the update() method of your Camera class on every frame:
function animate() {
requestAnimationFrame(animate);
// Update camera rotation limits
camera.update();
// Other updates and rendering
renderer.render(scene, camera.instance);
}
animate();
Summary:
By clamping the rotation.x of the pitchObject, you effectively limit how far the camera can look up and down. Adjust the minPitch and maxPitch values to suit your needs.
Note:
- Ensure that the angles are in radians when using trigonometric functions in JavaScript.
- The
update() method must be called every frame to continually enforce the rotation limits.