FirstPersonCamera
new FirstPersonCamera(physics : Object, options : Object)FirstPersonCamera - FPS-style camera with pointer lock
Extends PhysicsCamera to provide first-person shooter camera controls. Automatically manages pointer lock, accumulates yaw/pitch rotation, and syncs body rotation for WASD movement in the look direction.
Constructor Parameters
physicsObjectoptionsoptionalObjectDefault is
{}.canvasHTMLCanvasElementCanvas element for pointer locktargetoptionalTHREE.Object3DPlayer mesh to follow (auto-detects body index from mesh.userData)playerBodyIndexoptionalnumberExplicit body index to follow (overrides auto-detection)eyeHeightoptionalnumberEye height offset from target position
Default is1.7.sensitivityoptionalObjectMouse sensitivity
Default is{ x: 2, y: 2 }.pitchLimitsoptionalArray<number>Vertical look limits in degrees
Default is[-85, 85].fovoptionalnumberField of view
Default is70.
Example
import { FirstPersonCamera } from '@three-blocks/pro';
const camera = new FirstPersonCamera(physics, {
canvas: renderer.domElement,
target: playerMesh,
eyeHeight: 1.7,
sensitivity: { x: 2, y: 2 },
});
// In animate loop
camera.update(delta);
renderer.render(scene, camera);Properties
# .canvas : HTMLCanvasElement
Canvas element for pointer lock.
# .eyeHeight : number
Eye height offset from target position.
# .sensitivity : Object
Mouse sensitivity for rotation.
# .pitchLimits : Array.
Vertical look limits in degrees [min, max].
# .locked : boolean
Whether pointer lock is currently active.
# .yaw : number
Current yaw angle in radians.
# .pitch : number
Current pitch angle in radians.
Methods
on#
on(event : 'lock'|'unlock', callback : function) : thisRegister an event callback.
Parameters
event'lock' | 'unlock'callbackfunctionReturns
this — For chainingoff#
off(event : 'lock'|'unlock', callback : function) : thisRemove an event callback.
Parameters
event'lock' | 'unlock'callbackfunctionReturns
this — For chainingsetYaw#
setYaw(radians : number) : thisSet the yaw angle (horizontal rotation).
Parameters
radiansnumberReturns
this — For chainingsetPitch#
setPitch(radians : number) : thisSet the pitch angle (vertical rotation).
Parameters
radiansnumberReturns
this — For chainingupdate#
update(delta : number)Update camera state. Call each frame.
Parameters
deltanumberlock#
lock()Manually request pointer lock.
unlock#
unlock()Exit pointer lock.
dispose#
dispose()Clean up resources and event listeners.