2026-04-11 11:15:52 +02:00
|
|
|
import { Vector3 } from "three";
|
|
|
|
|
|
|
|
|
|
import type { Vec3 } from "../core/vector";
|
|
|
|
|
|
2026-04-11 12:30:37 +02:00
|
|
|
import {
|
2026-04-11 18:37:49 +02:00
|
|
|
FIRST_PERSON_PLAYER_SHAPE,
|
|
|
|
|
cloneFirstPersonPlayerShape,
|
|
|
|
|
getFirstPersonPlayerEyeHeight
|
|
|
|
|
} from "./player-collision";
|
|
|
|
|
import {
|
|
|
|
|
resolvePlayerStartActionInputs,
|
2026-04-11 12:30:37 +02:00
|
|
|
resolvePlayerStartLookInput,
|
|
|
|
|
} from "./player-input-bindings";
|
2026-04-11 18:37:49 +02:00
|
|
|
import {
|
|
|
|
|
createIdleRuntimeLocomotionState,
|
|
|
|
|
stepPlayerLocomotion
|
|
|
|
|
} from "./player-locomotion";
|
2026-04-11 19:05:45 +02:00
|
|
|
import { createPlayerControllerTelemetry } from "./player-controller-telemetry";
|
2026-04-11 19:05:14 +02:00
|
|
|
import type { PlayerControllerTelemetry } from "./navigation-controller";
|
2026-04-11 11:15:52 +02:00
|
|
|
import type {
|
|
|
|
|
NavigationController,
|
|
|
|
|
NavigationControllerDeactivateOptions,
|
|
|
|
|
RuntimeControllerContext,
|
|
|
|
|
RuntimeLocomotionState
|
|
|
|
|
} from "./navigation-controller";
|
2026-04-11 18:00:48 +02:00
|
|
|
import type { RuntimePlayerMovement } from "./runtime-scene-build";
|
2026-04-11 11:15:52 +02:00
|
|
|
|
|
|
|
|
const LOOK_SENSITIVITY = 0.008;
|
2026-04-11 12:30:37 +02:00
|
|
|
const GAMEPAD_LOOK_SPEED = 2.8;
|
2026-04-11 11:15:52 +02:00
|
|
|
const DEFAULT_CAMERA_DISTANCE = 4.5;
|
|
|
|
|
const MIN_CAMERA_DISTANCE = 1.5;
|
|
|
|
|
const MAX_CAMERA_DISTANCE = 7;
|
|
|
|
|
const DEFAULT_PITCH_RADIANS = 0.35;
|
|
|
|
|
const MIN_PITCH_RADIANS = -0.2;
|
|
|
|
|
const MAX_PITCH_RADIANS = Math.PI * 0.45;
|
|
|
|
|
const CAMERA_COLLISION_RADIUS = 0.2;
|
|
|
|
|
const CAMERA_PIVOT_EYE_HEIGHT_FACTOR = 0.85;
|
|
|
|
|
|
|
|
|
|
function clampPitch(pitchRadians: number): number {
|
|
|
|
|
return Math.max(
|
|
|
|
|
MIN_PITCH_RADIANS,
|
|
|
|
|
Math.min(MAX_PITCH_RADIANS, pitchRadians)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function clampCameraDistance(distance: number): number {
|
|
|
|
|
return Math.max(
|
|
|
|
|
MIN_CAMERA_DISTANCE,
|
|
|
|
|
Math.min(MAX_CAMERA_DISTANCE, distance)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function toEyePosition(feetPosition: Vec3, eyeHeight: number): Vec3 {
|
|
|
|
|
return {
|
|
|
|
|
x: feetPosition.x,
|
|
|
|
|
y: feetPosition.y + eyeHeight,
|
|
|
|
|
z: feetPosition.z
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 18:00:48 +02:00
|
|
|
function cloneRuntimePlayerMovement(
|
|
|
|
|
movement: RuntimePlayerMovement
|
|
|
|
|
): RuntimePlayerMovement {
|
|
|
|
|
return {
|
|
|
|
|
templateKind: movement.templateKind,
|
|
|
|
|
moveSpeed: movement.moveSpeed,
|
2026-04-11 20:30:39 +02:00
|
|
|
maxSpeed: movement.maxSpeed,
|
2026-04-11 20:59:15 +02:00
|
|
|
maxStepHeight: movement.maxStepHeight,
|
2026-04-11 18:00:48 +02:00
|
|
|
capabilities: {
|
|
|
|
|
jump: movement.capabilities.jump,
|
|
|
|
|
sprint: movement.capabilities.sprint,
|
|
|
|
|
crouch: movement.capabilities.crouch
|
2026-04-11 20:10:01 +02:00
|
|
|
},
|
|
|
|
|
jump: {
|
|
|
|
|
speed: movement.jump.speed,
|
|
|
|
|
bufferMs: movement.jump.bufferMs,
|
|
|
|
|
coyoteTimeMs: movement.jump.coyoteTimeMs,
|
|
|
|
|
variableHeight: movement.jump.variableHeight,
|
2026-04-11 20:30:39 +02:00
|
|
|
maxHoldMs: movement.jump.maxHoldMs,
|
|
|
|
|
bunnyHop: movement.jump.bunnyHop,
|
|
|
|
|
bunnyHopBoost: movement.jump.bunnyHopBoost
|
2026-04-11 20:10:01 +02:00
|
|
|
},
|
|
|
|
|
sprint: {
|
|
|
|
|
speedMultiplier: movement.sprint.speedMultiplier
|
|
|
|
|
},
|
|
|
|
|
crouch: {
|
|
|
|
|
speedMultiplier: movement.crouch.speedMultiplier
|
2026-04-11 18:00:48 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 11:15:52 +02:00
|
|
|
export class ThirdPersonNavigationController implements NavigationController {
|
|
|
|
|
readonly id = "thirdPerson" as const;
|
|
|
|
|
|
|
|
|
|
private context: RuntimeControllerContext | null = null;
|
|
|
|
|
private readonly pressedKeys = new Set<string>();
|
|
|
|
|
private readonly lookAtVector = new Vector3();
|
|
|
|
|
private feetPosition = {
|
|
|
|
|
x: 0,
|
|
|
|
|
y: 0,
|
|
|
|
|
z: 0
|
|
|
|
|
};
|
2026-04-11 18:37:49 +02:00
|
|
|
private standingPlayerShape = cloneFirstPersonPlayerShape(
|
|
|
|
|
FIRST_PERSON_PLAYER_SHAPE
|
|
|
|
|
);
|
|
|
|
|
private activePlayerShape = cloneFirstPersonPlayerShape(
|
|
|
|
|
FIRST_PERSON_PLAYER_SHAPE
|
|
|
|
|
);
|
2026-04-11 11:15:52 +02:00
|
|
|
private yawRadians = 0;
|
|
|
|
|
private cameraYawRadians = 0;
|
|
|
|
|
private pitchRadians = DEFAULT_PITCH_RADIANS;
|
|
|
|
|
private cameraDistance = DEFAULT_CAMERA_DISTANCE;
|
|
|
|
|
private verticalVelocity = 0;
|
|
|
|
|
private grounded = false;
|
2026-04-11 18:37:49 +02:00
|
|
|
private jumpPressed = false;
|
|
|
|
|
private locomotionState: RuntimeLocomotionState =
|
|
|
|
|
createIdleRuntimeLocomotionState("flying");
|
2026-04-11 11:15:52 +02:00
|
|
|
private inWaterVolume = false;
|
|
|
|
|
private inFogVolume = false;
|
|
|
|
|
private dragging = false;
|
|
|
|
|
private lastPointerClientX = 0;
|
|
|
|
|
private lastPointerClientY = 0;
|
|
|
|
|
private initializedFromSpawn = false;
|
2026-04-11 19:05:14 +02:00
|
|
|
private previousTelemetry: PlayerControllerTelemetry | null = null;
|
|
|
|
|
private latestJumpStarted = false;
|
|
|
|
|
private latestHeadBump = false;
|
2026-04-11 20:10:01 +02:00
|
|
|
private jumpBufferRemainingMs = 0;
|
|
|
|
|
private coyoteTimeRemainingMs = 0;
|
|
|
|
|
private jumpHoldRemainingMs = 0;
|
2026-04-11 11:15:52 +02:00
|
|
|
|
|
|
|
|
activate(ctx: RuntimeControllerContext): void {
|
|
|
|
|
this.context = ctx;
|
|
|
|
|
|
|
|
|
|
if (!this.initializedFromSpawn) {
|
2026-04-11 18:38:24 +02:00
|
|
|
const runtimeScene = ctx.getRuntimeScene();
|
|
|
|
|
const spawn = runtimeScene.spawn;
|
2026-04-11 11:15:52 +02:00
|
|
|
this.feetPosition = {
|
|
|
|
|
...spawn.position
|
|
|
|
|
};
|
2026-04-11 18:38:24 +02:00
|
|
|
this.standingPlayerShape = cloneFirstPersonPlayerShape(
|
|
|
|
|
runtimeScene.playerCollider
|
|
|
|
|
);
|
|
|
|
|
this.activePlayerShape = cloneFirstPersonPlayerShape(
|
|
|
|
|
runtimeScene.playerCollider
|
|
|
|
|
);
|
2026-04-11 11:15:52 +02:00
|
|
|
this.yawRadians = (spawn.yawDegrees * Math.PI) / 180;
|
|
|
|
|
this.cameraYawRadians = this.yawRadians;
|
|
|
|
|
this.pitchRadians = DEFAULT_PITCH_RADIANS;
|
|
|
|
|
this.cameraDistance = DEFAULT_CAMERA_DISTANCE;
|
|
|
|
|
this.verticalVelocity = 0;
|
|
|
|
|
this.grounded = false;
|
2026-04-11 18:38:24 +02:00
|
|
|
this.jumpPressed = false;
|
|
|
|
|
this.locomotionState = createIdleRuntimeLocomotionState(
|
|
|
|
|
runtimeScene.playerCollider.mode === "none" ? "flying" : "airborne"
|
|
|
|
|
);
|
2026-04-11 11:15:52 +02:00
|
|
|
this.inWaterVolume = false;
|
|
|
|
|
this.inFogVolume = false;
|
|
|
|
|
this.initializedFromSpawn = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.addEventListener("keydown", this.handleKeyDown);
|
|
|
|
|
window.addEventListener("keyup", this.handleKeyUp);
|
|
|
|
|
window.addEventListener("blur", this.handleBlur);
|
|
|
|
|
ctx.domElement.addEventListener("pointerdown", this.handlePointerDown);
|
|
|
|
|
ctx.domElement.addEventListener("wheel", this.handleWheel, {
|
|
|
|
|
passive: false
|
|
|
|
|
});
|
|
|
|
|
ctx.domElement.addEventListener("contextmenu", this.handleContextMenu);
|
|
|
|
|
window.addEventListener("pointermove", this.handlePointerMove);
|
|
|
|
|
window.addEventListener("pointerup", this.handlePointerUp);
|
|
|
|
|
|
|
|
|
|
ctx.setRuntimeMessage(
|
2026-04-11 12:30:37 +02:00
|
|
|
"Third Person active. Drag to orbit the camera, use the right stick for gamepad camera look, move with your authored bindings, and scroll to zoom."
|
2026-04-11 11:15:52 +02:00
|
|
|
);
|
|
|
|
|
this.updateCameraTransform();
|
|
|
|
|
this.publishTelemetry();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deactivate(
|
|
|
|
|
ctx: RuntimeControllerContext,
|
|
|
|
|
_options: NavigationControllerDeactivateOptions = {}
|
|
|
|
|
): void {
|
|
|
|
|
void _options;
|
|
|
|
|
window.removeEventListener("keydown", this.handleKeyDown);
|
|
|
|
|
window.removeEventListener("keyup", this.handleKeyUp);
|
|
|
|
|
window.removeEventListener("blur", this.handleBlur);
|
|
|
|
|
ctx.domElement.removeEventListener("pointerdown", this.handlePointerDown);
|
|
|
|
|
ctx.domElement.removeEventListener("wheel", this.handleWheel);
|
|
|
|
|
ctx.domElement.removeEventListener("contextmenu", this.handleContextMenu);
|
|
|
|
|
window.removeEventListener("pointermove", this.handlePointerMove);
|
|
|
|
|
window.removeEventListener("pointerup", this.handlePointerUp);
|
|
|
|
|
this.pressedKeys.clear();
|
|
|
|
|
this.dragging = false;
|
2026-04-11 18:38:24 +02:00
|
|
|
this.jumpPressed = false;
|
2026-04-11 19:05:45 +02:00
|
|
|
this.latestJumpStarted = false;
|
|
|
|
|
this.latestHeadBump = false;
|
2026-04-11 20:10:01 +02:00
|
|
|
this.jumpBufferRemainingMs = 0;
|
|
|
|
|
this.coyoteTimeRemainingMs = 0;
|
|
|
|
|
this.jumpHoldRemainingMs = 0;
|
2026-04-11 19:05:45 +02:00
|
|
|
this.previousTelemetry = null;
|
2026-04-11 11:15:52 +02:00
|
|
|
ctx.setRuntimeMessage(null);
|
2026-04-11 19:05:45 +02:00
|
|
|
ctx.setPlayerControllerTelemetry(null);
|
2026-04-11 11:15:52 +02:00
|
|
|
this.context = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resetSceneState(): void {
|
|
|
|
|
this.pressedKeys.clear();
|
|
|
|
|
this.feetPosition = {
|
|
|
|
|
x: 0,
|
|
|
|
|
y: 0,
|
|
|
|
|
z: 0
|
|
|
|
|
};
|
|
|
|
|
this.yawRadians = 0;
|
|
|
|
|
this.cameraYawRadians = 0;
|
|
|
|
|
this.pitchRadians = DEFAULT_PITCH_RADIANS;
|
|
|
|
|
this.cameraDistance = DEFAULT_CAMERA_DISTANCE;
|
|
|
|
|
this.verticalVelocity = 0;
|
|
|
|
|
this.grounded = false;
|
2026-04-11 18:38:24 +02:00
|
|
|
this.jumpPressed = false;
|
|
|
|
|
this.standingPlayerShape = cloneFirstPersonPlayerShape(
|
|
|
|
|
FIRST_PERSON_PLAYER_SHAPE
|
|
|
|
|
);
|
|
|
|
|
this.activePlayerShape = cloneFirstPersonPlayerShape(
|
|
|
|
|
FIRST_PERSON_PLAYER_SHAPE
|
|
|
|
|
);
|
|
|
|
|
this.locomotionState = createIdleRuntimeLocomotionState("flying");
|
2026-04-11 11:15:52 +02:00
|
|
|
this.inWaterVolume = false;
|
|
|
|
|
this.inFogVolume = false;
|
|
|
|
|
this.dragging = false;
|
|
|
|
|
this.lastPointerClientX = 0;
|
|
|
|
|
this.lastPointerClientY = 0;
|
|
|
|
|
this.initializedFromSpawn = false;
|
2026-04-11 19:05:45 +02:00
|
|
|
this.previousTelemetry = null;
|
|
|
|
|
this.latestJumpStarted = false;
|
|
|
|
|
this.latestHeadBump = false;
|
2026-04-11 20:10:01 +02:00
|
|
|
this.jumpBufferRemainingMs = 0;
|
|
|
|
|
this.coyoteTimeRemainingMs = 0;
|
|
|
|
|
this.jumpHoldRemainingMs = 0;
|
2026-04-11 11:15:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
update(dt: number): void {
|
|
|
|
|
if (this.context === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 18:00:48 +02:00
|
|
|
const runtimeScene = this.context.getRuntimeScene();
|
2026-04-11 18:38:24 +02:00
|
|
|
this.standingPlayerShape = cloneFirstPersonPlayerShape(
|
|
|
|
|
runtimeScene.playerCollider
|
|
|
|
|
);
|
2026-04-11 18:00:48 +02:00
|
|
|
const playerMovement = runtimeScene.playerMovement;
|
2026-04-11 12:30:37 +02:00
|
|
|
const lookInput = resolvePlayerStartLookInput(
|
2026-04-11 18:00:48 +02:00
|
|
|
runtimeScene.playerInputBindings
|
2026-04-11 12:30:37 +02:00
|
|
|
);
|
2026-04-11 18:38:24 +02:00
|
|
|
const inputState = resolvePlayerStartActionInputs(
|
2026-04-11 12:13:19 +02:00
|
|
|
this.pressedKeys,
|
2026-04-11 18:00:48 +02:00
|
|
|
runtimeScene.playerInputBindings
|
2026-04-11 12:13:19 +02:00
|
|
|
);
|
2026-04-11 12:30:37 +02:00
|
|
|
|
|
|
|
|
if (lookInput.horizontal !== 0 || lookInput.vertical !== 0) {
|
|
|
|
|
this.cameraYawRadians -= lookInput.horizontal * GAMEPAD_LOOK_SPEED * dt;
|
|
|
|
|
this.pitchRadians = clampPitch(
|
|
|
|
|
this.pitchRadians - lookInput.vertical * GAMEPAD_LOOK_SPEED * dt
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 18:38:24 +02:00
|
|
|
const locomotionStep = stepPlayerLocomotion(
|
2026-04-11 11:15:52 +02:00
|
|
|
{
|
2026-04-11 18:38:24 +02:00
|
|
|
dt,
|
|
|
|
|
feetPosition: this.feetPosition,
|
|
|
|
|
movementYawRadians: this.cameraYawRadians,
|
|
|
|
|
standingShape: this.standingPlayerShape,
|
|
|
|
|
verticalVelocity: this.verticalVelocity,
|
2026-04-11 19:34:22 +02:00
|
|
|
previousLocomotionState: this.locomotionState,
|
2026-04-11 20:10:01 +02:00
|
|
|
jumpBufferRemainingMs: this.jumpBufferRemainingMs,
|
|
|
|
|
coyoteTimeRemainingMs: this.coyoteTimeRemainingMs,
|
|
|
|
|
jumpHoldRemainingMs: this.jumpHoldRemainingMs,
|
2026-04-11 18:38:24 +02:00
|
|
|
crouched: this.locomotionState.crouched,
|
|
|
|
|
wasJumpPressed: this.jumpPressed,
|
|
|
|
|
input: inputState,
|
|
|
|
|
movement: playerMovement,
|
|
|
|
|
resolveMotion: (feetPosition, motion, shape) =>
|
|
|
|
|
this.context?.resolveFirstPersonMotion(feetPosition, motion, shape) ??
|
|
|
|
|
null,
|
|
|
|
|
resolveVolumeState: (feetPosition) =>
|
|
|
|
|
this.context?.resolvePlayerVolumeState(feetPosition) ?? {
|
|
|
|
|
inWater: false,
|
|
|
|
|
inFog: false
|
|
|
|
|
},
|
|
|
|
|
probeGround: (feetPosition, shape, maxDistance) =>
|
2026-04-11 18:42:37 +02:00
|
|
|
this.context?.probePlayerGround?.(feetPosition, shape, maxDistance) ?? {
|
2026-04-11 18:38:24 +02:00
|
|
|
grounded: false,
|
|
|
|
|
distance: null,
|
|
|
|
|
normal: null,
|
|
|
|
|
slopeDegrees: null
|
|
|
|
|
},
|
|
|
|
|
canOccupyShape: (feetPosition, shape) =>
|
2026-04-11 18:42:37 +02:00
|
|
|
this.context?.canOccupyPlayerShape?.(feetPosition, shape) ?? true
|
2026-04-11 18:38:24 +02:00
|
|
|
}
|
2026-04-11 11:15:52 +02:00
|
|
|
);
|
|
|
|
|
|
2026-04-11 18:38:24 +02:00
|
|
|
if (locomotionStep === null) {
|
2026-04-11 11:15:52 +02:00
|
|
|
this.updateCameraTransform();
|
|
|
|
|
this.publishTelemetry();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 18:38:24 +02:00
|
|
|
this.feetPosition = locomotionStep.feetPosition;
|
|
|
|
|
this.activePlayerShape = locomotionStep.activeShape;
|
|
|
|
|
this.verticalVelocity = locomotionStep.verticalVelocity;
|
2026-04-11 20:10:01 +02:00
|
|
|
this.jumpBufferRemainingMs = locomotionStep.jumpBufferRemainingMs;
|
|
|
|
|
this.coyoteTimeRemainingMs = locomotionStep.coyoteTimeRemainingMs;
|
|
|
|
|
this.jumpHoldRemainingMs = locomotionStep.jumpHoldRemainingMs;
|
2026-04-11 18:38:24 +02:00
|
|
|
this.jumpPressed = locomotionStep.jumpPressed;
|
2026-04-11 19:05:45 +02:00
|
|
|
this.latestJumpStarted = locomotionStep.jumpStarted;
|
|
|
|
|
this.latestHeadBump = locomotionStep.headBump;
|
2026-04-11 18:38:24 +02:00
|
|
|
this.locomotionState = locomotionStep.locomotionState;
|
|
|
|
|
this.grounded = locomotionStep.locomotionState.grounded;
|
|
|
|
|
this.inWaterVolume = locomotionStep.inWaterVolume;
|
|
|
|
|
this.inFogVolume = locomotionStep.inFogVolume;
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
Math.hypot(
|
|
|
|
|
locomotionStep.planarDisplacement.x,
|
|
|
|
|
locomotionStep.planarDisplacement.z
|
|
|
|
|
) > 1e-5
|
|
|
|
|
) {
|
|
|
|
|
this.yawRadians = Math.atan2(
|
|
|
|
|
locomotionStep.planarDisplacement.x,
|
|
|
|
|
locomotionStep.planarDisplacement.z
|
|
|
|
|
);
|
2026-04-11 11:15:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.updateCameraTransform();
|
|
|
|
|
this.publishTelemetry();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
teleportTo(feetPosition: Vec3, yawDegrees: number) {
|
|
|
|
|
this.feetPosition = {
|
|
|
|
|
...feetPosition
|
|
|
|
|
};
|
|
|
|
|
this.yawRadians = (yawDegrees * Math.PI) / 180;
|
|
|
|
|
this.cameraYawRadians = this.yawRadians;
|
|
|
|
|
this.pitchRadians = DEFAULT_PITCH_RADIANS;
|
|
|
|
|
this.verticalVelocity = 0;
|
|
|
|
|
this.grounded = false;
|
2026-04-11 18:38:24 +02:00
|
|
|
this.jumpPressed = false;
|
|
|
|
|
this.activePlayerShape = cloneFirstPersonPlayerShape(
|
|
|
|
|
this.context?.getRuntimeScene().playerCollider ?? FIRST_PERSON_PLAYER_SHAPE
|
|
|
|
|
);
|
|
|
|
|
this.standingPlayerShape = cloneFirstPersonPlayerShape(
|
|
|
|
|
this.context?.getRuntimeScene().playerCollider ?? FIRST_PERSON_PLAYER_SHAPE
|
|
|
|
|
);
|
|
|
|
|
this.locomotionState = createIdleRuntimeLocomotionState(
|
|
|
|
|
this.activePlayerShape.mode === "none" ? "flying" : "airborne"
|
|
|
|
|
);
|
2026-04-11 19:05:45 +02:00
|
|
|
this.previousTelemetry = null;
|
|
|
|
|
this.latestJumpStarted = false;
|
|
|
|
|
this.latestHeadBump = false;
|
2026-04-11 20:10:01 +02:00
|
|
|
this.jumpBufferRemainingMs = 0;
|
|
|
|
|
this.coyoteTimeRemainingMs = 0;
|
|
|
|
|
this.jumpHoldRemainingMs = 0;
|
2026-04-11 11:15:52 +02:00
|
|
|
this.inWaterVolume = false;
|
|
|
|
|
this.inFogVolume = false;
|
|
|
|
|
this.updateCameraTransform();
|
|
|
|
|
this.publishTelemetry();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private updateCameraTransform() {
|
|
|
|
|
if (this.context === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 18:38:24 +02:00
|
|
|
const eyeHeight = getFirstPersonPlayerEyeHeight(this.activePlayerShape);
|
2026-04-11 11:15:52 +02:00
|
|
|
const pivot = {
|
|
|
|
|
x: this.feetPosition.x,
|
|
|
|
|
y: this.feetPosition.y + eyeHeight * CAMERA_PIVOT_EYE_HEIGHT_FACTOR,
|
|
|
|
|
z: this.feetPosition.z
|
|
|
|
|
};
|
|
|
|
|
const horizontalDistance =
|
|
|
|
|
Math.cos(this.pitchRadians) * this.cameraDistance;
|
|
|
|
|
const desiredCameraPosition = {
|
|
|
|
|
x: pivot.x - Math.sin(this.cameraYawRadians) * horizontalDistance,
|
|
|
|
|
y: pivot.y + Math.sin(this.pitchRadians) * this.cameraDistance,
|
|
|
|
|
z: pivot.z - Math.cos(this.cameraYawRadians) * horizontalDistance
|
|
|
|
|
};
|
|
|
|
|
const resolvedCameraPosition =
|
|
|
|
|
this.context.resolveThirdPersonCameraCollision(
|
|
|
|
|
pivot,
|
|
|
|
|
desiredCameraPosition,
|
|
|
|
|
CAMERA_COLLISION_RADIUS
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this.context.camera.position.set(
|
|
|
|
|
resolvedCameraPosition.x,
|
|
|
|
|
resolvedCameraPosition.y,
|
|
|
|
|
resolvedCameraPosition.z
|
|
|
|
|
);
|
|
|
|
|
this.lookAtVector.set(pivot.x, pivot.y, pivot.z);
|
|
|
|
|
this.context.camera.lookAt(this.lookAtVector);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private publishTelemetry() {
|
|
|
|
|
if (this.context === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const eyePosition = toEyePosition(
|
|
|
|
|
this.feetPosition,
|
2026-04-11 18:38:24 +02:00
|
|
|
getFirstPersonPlayerEyeHeight(this.activePlayerShape)
|
2026-04-11 11:15:52 +02:00
|
|
|
);
|
|
|
|
|
const cameraVolumeState = this.context.resolvePlayerVolumeState({
|
|
|
|
|
x: this.context.camera.position.x,
|
|
|
|
|
y: this.context.camera.position.y,
|
|
|
|
|
z: this.context.camera.position.z
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-11 19:05:45 +02:00
|
|
|
const telemetry = createPlayerControllerTelemetry({
|
2026-04-11 11:15:52 +02:00
|
|
|
feetPosition: {
|
|
|
|
|
...this.feetPosition
|
|
|
|
|
},
|
|
|
|
|
eyePosition,
|
|
|
|
|
grounded: this.grounded,
|
|
|
|
|
locomotionState: this.locomotionState,
|
2026-04-11 18:00:48 +02:00
|
|
|
movement: cloneRuntimePlayerMovement(
|
|
|
|
|
this.context.getRuntimeScene().playerMovement
|
|
|
|
|
),
|
2026-04-11 11:15:52 +02:00
|
|
|
inWaterVolume: this.inWaterVolume,
|
|
|
|
|
cameraSubmerged: cameraVolumeState.inWater,
|
|
|
|
|
inFogVolume: this.inFogVolume,
|
|
|
|
|
pointerLocked: false,
|
2026-04-11 19:05:45 +02:00
|
|
|
spawn: this.context.getRuntimeScene().spawn,
|
|
|
|
|
previousLocomotionState: this.previousTelemetry?.locomotionState ?? null,
|
|
|
|
|
previousInWaterVolume: this.previousTelemetry?.inWaterVolume ?? false,
|
|
|
|
|
jumpStarted: this.latestJumpStarted,
|
|
|
|
|
headBump: this.latestHeadBump
|
2026-04-11 11:15:52 +02:00
|
|
|
});
|
2026-04-11 19:05:45 +02:00
|
|
|
|
|
|
|
|
this.context.setPlayerControllerTelemetry(telemetry);
|
|
|
|
|
this.previousTelemetry = telemetry;
|
|
|
|
|
this.latestJumpStarted = false;
|
|
|
|
|
this.latestHeadBump = false;
|
2026-04-11 11:15:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private handleKeyDown = (event: KeyboardEvent) => {
|
|
|
|
|
this.pressedKeys.add(event.code);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private handleKeyUp = (event: KeyboardEvent) => {
|
|
|
|
|
this.pressedKeys.delete(event.code);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private handleBlur = () => {
|
|
|
|
|
this.pressedKeys.clear();
|
|
|
|
|
this.dragging = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private handlePointerDown = (event: PointerEvent) => {
|
|
|
|
|
if (event.button !== 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.dragging = true;
|
|
|
|
|
this.lastPointerClientX = event.clientX;
|
|
|
|
|
this.lastPointerClientY = event.clientY;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private handlePointerMove = (event: PointerEvent) => {
|
|
|
|
|
if (!this.dragging) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const deltaX = event.clientX - this.lastPointerClientX;
|
|
|
|
|
const deltaY = event.clientY - this.lastPointerClientY;
|
|
|
|
|
this.lastPointerClientX = event.clientX;
|
|
|
|
|
this.lastPointerClientY = event.clientY;
|
|
|
|
|
|
|
|
|
|
this.cameraYawRadians -= deltaX * LOOK_SENSITIVITY;
|
|
|
|
|
this.pitchRadians = clampPitch(
|
|
|
|
|
this.pitchRadians + deltaY * LOOK_SENSITIVITY
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private handlePointerUp = () => {
|
|
|
|
|
this.dragging = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private handleWheel = (event: WheelEvent) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
this.cameraDistance = clampCameraDistance(
|
|
|
|
|
this.cameraDistance + event.deltaY * 0.01
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private handleContextMenu = (event: MouseEvent) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
};
|
|
|
|
|
}
|