SpectatorCamera
new SpectatorCamera(physics : Object, options : Object)SpectatorCamera - Camera for spectating any physics body
Attaches to kinematic or dynamic bodies and provides multiple view modes:
- native: Use the body's camera if available, else first-person
- firstPerson: First-person view from body's position/rotation
- rts: Overhead RTS-style view centered on the body
- free: Detached camera (no automatic positioning)
Designed for spectating AI agents, multiplayer scenarios, and debugging.
Constructor Parameters
physicsObjectPhysics instance
optionsoptionalObjectCamera options
Default is
Default is
{}.eyeHeightoptionalnumberEye height offset for first-person mode
Default is1.7.fovoptionalnumberField of view
Default is70.nearoptionalnumberNear clipping plane
Default is0.1.faroptionalnumberFar clipping plane
Default is1000.
Example
import { SpectatorCamera } from '@three-blocks/pro';
const spectator = new SpectatorCamera(physics, {
eyeHeight: 1.7,
});
// Attach to a bot
spectator.attach(bot.body);
// Override with RTS style
spectator.setMode('rts', { distance: 30, tilt: Math.PI / 3 });
// Return to body's native view
spectator.setMode('native');
// Detach (returns to free camera)
spectator.detach();
// In animation loop
spectator.update(delta);
renderer.render(scene, spectator);Properties
# .eyeHeight : number
Eye height offset for first-person mode.
# .attachedBody :
Get currently attached body.
# .mode :
Get current view mode.
# .isAttached :
Check if camera is attached to a body.
Methods
attach#
attach(body : Object) : SpectatorCameraAttach to a physics body.
Parameters
bodyObjectKinematicBody or DynamicBody to spectate
Returns
SpectatorCamera — This instance for chainingdetach#
detach() : SpectatorCameraDetach from current body.
Returns
SpectatorCamera — This instance for chainingsetMode#
setMode(mode : 'native'|'firstPerson'|'rts'|'free', config : Object) : SpectatorCameraSet spectator view mode.
Parameters
mode'native' | 'firstPerson' | 'rts' | 'free'View mode
configoptionalObjectMode-specific configuration
Default is
Default is
{}.distanceoptionalnumberDistance from body (RTS mode)
Default is30.tiltoptionalnumberCamera tilt angle (RTS mode)
Default isMath.PI/3.
Returns
SpectatorCamera — This instance for chainingupdate#
update(delta : number)Update camera state. Call each frame.
Parameters
deltanumberTime delta in seconds
dispose#
dispose()Clean up resources.