Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 | 23x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 1072x 1072x 1072x 1072x 1072x 1072x 1072x 1022x 1022x 1022x 617x 595x 595x 595x 595x 595x 595x 595x 595x 595x 595x 22x 22x 22x 22x 22x 405x 405x 50x 50x 37x 37x 37x 37x 13x 50x 1072x 1072x 20x 20x 1072x 1097x 3x 3x 9x 9x 9x 9x 1x 1x 1x 1x | /**
* Physics-based movement system for realistic combat movement.
*
* **Korean**: 이동 물리 시스템 (Movement Physics System)
*
* This module implements realistic physics-based player movement with proper
* acceleration/deceleration, foot-wide step precision, and stance-based speed
* modifiers for authentic Korean martial arts combat feel.
*
* ## Features
*
* - **Realistic Acceleration**: 0 to 2m/s in 0.5 seconds (4.0 m/s²)
* - **Realistic Deceleration**: 2m/s to 0 in 0.3 seconds (6.67 m/s²)
* - **Foot-wide Steps**: Discrete 30cm movement increments for tactical positioning
* - **Stance Modifiers**: 8 trigram stances with different speed characteristics
* - **Injury Integration**: Movement speed reduced by leg damage (10-50%)
*
* @module systems/physics/MovementPhysics
* @category Physics System
* @korean 이동물리
*/
import * as THREE from 'three';
import { TrigramStance } from '@/types/common';
/**
* Movement input from keyboard/gamepad controls.
*
* **Korean**: 이동 입력 (Movement Input)
*
* @public
* @category Physics System
* @korean 이동입력
*/
export interface MovementInput {
/** Forward/backward input (-1 to 1, where 1 is forward) */
readonly forward: number;
/** Lateral left/right input (-1 to 1, where 1 is right) */
readonly lateral: number;
/** Whether sprint/run key is held */
readonly isRunning: boolean;
/** Whether any movement input is active */
readonly isMoving: boolean;
/** Whether to use tactical step mode (30cm grid) */
readonly useTacticalSteps: boolean;
}
/**
* Complete movement state for physics calculations.
*
* **Korean**: 이동 상태 (Movement State)
*
* Contains position, velocity, and current movement parameters.
* All vectors are mutable for performance (updated in-place during physics loop).
*
* @public
* @category Physics System
* @korean 이동상태
*/
export interface MovementState {
/**
* Current position in 3D space (meters).
*
* NOTE: This is a readonly reference to a mutable THREE.Vector3.
* The physics engine intentionally mutates the vector in-place
* (e.g. via position.add(...)) for performance. The readonly
* modifier prevents reassignment of the Vector3 instance, not
* mutation of its components.
*/
readonly position: THREE.Vector3;
/**
* Current velocity vector (m/s).
*
* NOTE: This is a readonly reference to a mutable THREE.Vector3.
* The physics engine updates this vector in-place each frame.
* Callers must not reassign the velocity reference, but may pass
* it to APIs that read or modify its components.
*/
readonly velocity: THREE.Vector3;
/** Current acceleration magnitude (m/s²) */
acceleration: number;
/** Maximum speed for current state (m/s) */
maxSpeed: number;
/** Current Eight Trigram stance */
readonly currentStance: TrigramStance;
/** Leg injury percentage (0-1, where 1 is fully injured) */
legInjuryFactor: number;
}
/**
* Stance-based speed modifiers for Eight Trigram system.
*
* **Korean**: 팔괘 속도 배수 (Eight Trigram Speed Multipliers)
*
* Each trigram stance has unique movement characteristics based on
* traditional Korean martial arts philosophy:
*
* - ☰ 건 (Geon/Heaven): 100% - Balanced, standard speed
* - ☱ 태 (Tae/Lake): 110% - Fluid movement, flowing techniques
* - ☲ 리 (Li/Fire): 120% - Aggressive, fast attacks
* - ☳ 진 (Jin/Thunder): 115% - Explosive power
* - ☴ 손 (Son/Wind): 125% - Continuous motion, fastest stance
* - ☵ 감 (Gam/Water): 105% - Adaptive, slightly faster than neutral
* - ☶ 간 (Gan/Mountain): 80% - Solid defense, slower movement
* - ☷ 곤 (Gon/Earth): 85% - Grounded, stable but slower
*
* @korean 자세속도배수
*/
export const STANCE_SPEED_MODIFIERS: Record<TrigramStance, number> = {
[TrigramStance.GEON]: 1.00, // Heaven: balanced
[TrigramStance.TAE]: 1.10, // Lake: fluid
[TrigramStance.LI]: 1.20, // Fire: aggressive
[TrigramStance.JIN]: 1.15, // Thunder: explosive
[TrigramStance.SON]: 1.25, // Wind: fastest
[TrigramStance.GAM]: 1.05, // Water: adaptive
[TrigramStance.GAN]: 0.80, // Mountain: defensive
[TrigramStance.GON]: 0.85, // Earth: grounded
};
/**
* Physics-based movement engine for combat.
*
* **Korean**: 이동 물리 엔진 (Movement Physics Engine)
*
* Implements realistic acceleration, deceleration, and stance-based movement
* for authentic Korean martial arts combat. Supports both continuous movement
* and tactical foot-wide steps for precise positioning.
*
* @example
* ```typescript
* const physics = new MovementPhysics();
*
* const state: MovementState = {
* position: new THREE.Vector3(0, 0, 0),
* velocity: new THREE.Vector3(0, 0, 0),
* acceleration: 0,
* maxSpeed: 2.0,
* currentStance: TrigramStance.GEON,
* legInjuryFactor: 0,
* };
*
* const input: MovementInput = {
* forward: 1.0,
* lateral: 0,
* isRunning: false,
* isMoving: true,
* useTacticalSteps: false,
* };
*
* // In game loop at 60fps
* physics.updateMovement(state, input, deltaTime);
* ```
*
* @public
* @category Physics System
* @korean 이동물리엔진
*/
export class MovementPhysics {
/**
* Base acceleration rate (m/s²)
* Achieves 0 to 2m/s in 0.5 seconds
*
* **Korean**: 기본 가속도 (Base Acceleration)
*/
private readonly BASE_ACCELERATION = 4.0;
/**
* Base deceleration rate (m/s²)
* Achieves 2m/s to 0 in 0.3 seconds
*
* **Korean**: 기본 감속도 (Base Deceleration)
*/
private readonly BASE_DECELERATION = 6.67;
/**
* Foot-wide step size (meters)
* Standard Korean martial arts step is approximately 30cm
*
* **Korean**: 보법 거리 (Step Distance)
*/
private readonly STEP_SIZE = 0.3;
/**
* Base walking speed (m/s)
*
* **Korean**: 기본 걷기 속도 (Base Walking Speed)
*/
private readonly BASE_WALK_SPEED = 2.0;
/**
* Base running speed (m/s)
*
* **Korean**: 기본 달리기 속도 (Base Running Speed)
*/
private readonly BASE_RUN_SPEED = 4.0;
/**
* Lateral movement speed (m/s) - side-stepping
*
* **Korean**: 측면 이동 속도 (Lateral Movement Speed)
*/
private readonly LATERAL_SPEED = 1.8;
/**
* Backward movement speed multiplier (25% slower than forward)
*
* **Korean**: 후퇴 속도 배수 (Backward Speed Multiplier)
*/
private readonly BACKWARD_SPEED_MULTIPLIER = 0.75;
/**
* Override for max speed from external speed modifier systems.
*
* **Korean**: 최대속도 재정의 (Max Speed Override)
*/
private _overrideMaxSpeed: number | null = null;
/**
* Override for acceleration from external speed modifier systems.
*
* **Korean**: 가속도 재정의 (Acceleration Override)
*/
private _overrideAcceleration: number | null = null;
// Temporary vectors to avoid allocations in update loop
private readonly tempTargetVelocity = new THREE.Vector3();
private readonly tempMovement = new THREE.Vector3();
private readonly tempDirection = new THREE.Vector3();
private readonly tempTargetDirection = new THREE.Vector3();
/**
* Update player movement based on input and physics.
*
* **Korean**: 이동 업데이트 (Update Movement)
*
* Called every frame (60fps) to update player position based on
* current velocity, acceleration, and input. Applies stance modifiers
* and injury penalties automatically.
*
* @param state - Current movement state (modified in-place)
* @param input - Current movement input from controls
* @param deltaTime - Time since last update (seconds)
*
* @korean 이동업데이트
*/
public updateMovement(
state: MovementState,
input: MovementInput,
deltaTime: number
): void {
// Calculate stance speed modifier
const stanceModifier = this.getStanceSpeedModifier(state.currentStance);
// Calculate injury penalty (0-50% speed reduction)
const injuryPenalty = 1.0 - (state.legInjuryFactor * 0.5);
// Calculate base target speed (walking or running)
const baseSpeed = input.isRunning ? this.BASE_RUN_SPEED : this.BASE_WALK_SPEED;
// Apply all modifiers to get final max speed (or use override)
state.maxSpeed = this._overrideMaxSpeed !== null
? this._overrideMaxSpeed
: baseSpeed * stanceModifier * injuryPenalty;
// Use override acceleration if set, otherwise use base
const currentAcceleration = this._overrideAcceleration !== null
? this._overrideAcceleration
: this.BASE_ACCELERATION;
// Calculate target velocity based on input direction
this.tempTargetVelocity.set(
input.lateral * this.LATERAL_SPEED * stanceModifier * injuryPenalty,
0,
input.forward * state.maxSpeed * (input.forward < 0 ? this.BACKWARD_SPEED_MULTIPLIER : 1.0)
);
// Apply acceleration or deceleration
if (input.isMoving) {
// Accelerate toward target velocity with realistic direction changes
const currentSpeed = state.velocity.length();
const targetSpeed = this.tempTargetVelocity.length();
if (currentSpeed < targetSpeed) {
// Check if direction change is needed
if (currentSpeed > 0.001 && targetSpeed > 0.001) {
// Current movement direction
this.tempDirection.copy(state.velocity).normalize();
// Desired movement direction (reuse temp vector to avoid allocation)
this.tempTargetDirection.copy(this.tempTargetVelocity).normalize();
const directionDot = this.tempDirection.dot(this.tempTargetDirection);
Iif (directionDot < 0) {
// Moving in opposite direction: decelerate first before reversing
const velocityDelta = this.BASE_DECELERATION * deltaTime;
const newSpeed = Math.max(currentSpeed - velocityDelta, 0);
if (newSpeed > 0.001) {
state.velocity.copy(this.tempDirection.multiplyScalar(newSpeed));
} else {
// Fully stopped; can now start accelerating in new direction
state.velocity.set(0, 0, 0);
}
state.acceleration = -this.BASE_DECELERATION;
I} else if (directionDot < 0.7) {
// Perpendicular direction change (e.g., forward to strafe): moderate deceleration
const blendedAccel = currentAcceleration * 0.6; // Reduced acceleration for sharp turns
const velocityDelta = blendedAccel * deltaTime;
const newSpeed = Math.min(currentSpeed + velocityDelta, targetSpeed);
this.tempDirection.copy(this.tempTargetVelocity).normalize();
state.velocity.copy(this.tempDirection.multiplyScalar(newSpeed));
state.acceleration = blendedAccel;
} else {
// Same or similar direction: full acceleration
this.tempDirection.copy(this.tempTargetVelocity).normalize();
const velocityDelta = currentAcceleration * deltaTime;
const newSpeed = Math.min(currentSpeed + velocityDelta, targetSpeed);
state.velocity.copy(this.tempDirection.multiplyScalar(newSpeed));
state.acceleration = currentAcceleration;
}
} else {
// Very low speed: safe to accelerate directly toward target
this.tempDirection.copy(this.tempTargetVelocity).normalize();
const velocityDelta = currentAcceleration * deltaTime;
const newSpeed = Math.min(currentSpeed + velocityDelta, targetSpeed);
state.velocity.copy(this.tempDirection.multiplyScalar(newSpeed));
state.acceleration = currentAcceleration;
}
} else {
// Already at or above target speed: snap to target velocity
state.velocity.copy(this.tempTargetVelocity);
state.acceleration = 0;
}
} else {
// Decelerate to stop
const currentSpeed = state.velocity.length();
if (currentSpeed > 0.01) {
this.tempDirection.copy(state.velocity).normalize();
const velocityDelta = this.BASE_DECELERATION * deltaTime;
const newSpeed = Math.max(currentSpeed - velocityDelta, 0);
state.velocity.copy(this.tempDirection.multiplyScalar(newSpeed));
} else {
state.velocity.set(0, 0, 0);
}
state.acceleration = -this.BASE_DECELERATION;
}
// Calculate movement delta for this frame
this.tempMovement.copy(state.velocity).multiplyScalar(deltaTime);
// Apply tactical step quantization if enabled
if (input.useTacticalSteps) {
// Quantize to 30cm grid steps
this.tempMovement.x = Math.round(this.tempMovement.x / this.STEP_SIZE) * this.STEP_SIZE;
this.tempMovement.z = Math.round(this.tempMovement.z / this.STEP_SIZE) * this.STEP_SIZE;
}
// Update position
state.position.add(this.tempMovement);
}
/**
* Get speed modifier for a specific trigram stance.
*
* **Korean**: 자세 속도 배수 가져오기 (Get Stance Speed Modifier)
*
* @param stance - Eight Trigram stance
* @returns Speed multiplier (0.8 to 1.25)
*
* @korean 자세속도배수
*/
public getStanceSpeedModifier(stance: TrigramStance): number {
return STANCE_SPEED_MODIFIERS[stance];
}
/**
* Calculate movement penalty from leg injury.
*
* **Korean**: 부상 이동 페널티 (Injury Movement Penalty)
*
* Leg damage reduces movement speed by 10-50% based on injury severity.
*
* @param legHealthPercentage - Remaining leg health (0-1)
* @returns Injury factor (0 = no injury, 1 = maximum injury)
*
* @korean 부상페널티
*/
public calculateInjuryPenalty(legHealthPercentage: number): number {
// Injury penalty scales from 0% (healthy) to 50% (critical)
// Clamp between 0 and 1
const healthFactor = Math.max(0, Math.min(1, legHealthPercentage));
return 1.0 - healthFactor;
}
/**
* Get maximum speed for current state configuration.
*
* **Korean**: 최대 속도 계산 (Calculate Maximum Speed)
*
* @param isRunning - Whether running (vs walking)
* @param stance - Current trigram stance
* @param legInjuryFactor - Leg injury severity (0-1)
* @returns Maximum speed in m/s
*
* @korean 최대속도
*/
public getMaxSpeed(
isRunning: boolean,
stance: TrigramStance,
legInjuryFactor: number
): number {
const baseSpeed = isRunning ? this.BASE_RUN_SPEED : this.BASE_WALK_SPEED;
const stanceModifier = this.getStanceSpeedModifier(stance);
const injuryPenalty = 1.0 - (legInjuryFactor * 0.5);
return baseSpeed * stanceModifier * injuryPenalty;
}
/**
* Calculate time required to reach target speed from current velocity.
*
* **Korean**: 가속 시간 (Acceleration Time)
*
* @param currentSpeed - Current speed magnitude (m/s)
* @param targetSpeed - Desired speed magnitude (m/s)
* @returns Time in seconds to reach target speed
*
* @korean 가속시간
*/
public getAccelerationTime(currentSpeed: number, targetSpeed: number): number {
const speedDiff = Math.abs(targetSpeed - currentSpeed);
return speedDiff / this.BASE_ACCELERATION;
}
/**
* Calculate stopping distance from current velocity.
*
* **Korean**: 제동 거리 (Braking Distance)
*
* @param currentSpeed - Current speed magnitude (m/s)
* @returns Distance in meters required to stop
*
* @korean 제동거리
*/
public getStoppingDistance(currentSpeed: number): number {
// Using kinematic equation: d = v² / (2a)
return (currentSpeed * currentSpeed) / (2 * this.BASE_DECELERATION);
}
/**
* Get tactical step size.
*
* **Korean**: 보법 거리 (Step Distance)
*
* @returns Step size in meters (0.3m = 30cm)
*
* @korean 보법거리
*/
public getStepSize(): number {
return this.STEP_SIZE;
}
/**
* Override maximum speed for external speed modifier systems.
*
* **Korean**: 최대 속도 설정 (Set Maximum Speed)
*
* Allows external systems (like SpeedModifierSystem) to override
* the calculated maximum speed. This is applied in the next
* updateMovement call.
*
* @param speed - Maximum speed in m/s
*
* @public
*/
public setMaxSpeed(speed: number): void {
this._overrideMaxSpeed = speed;
}
/**
* Override acceleration for external speed modifier systems.
*
* **Korean**: 가속도 설정 (Set Acceleration)
*
* Allows external systems (like SpeedModifierSystem) to override
* the base acceleration rate. This is applied in the next
* updateMovement call.
*
* @param acceleration - Acceleration in m/s²
*
* @public
*/
public setAcceleration(acceleration: number): void {
this._overrideAcceleration = acceleration;
}
/**
* Clear speed and acceleration overrides.
*
* **Korean**: 속도 재정의 해제 (Clear Speed Overrides)
*
* Resets movement to use default calculations without external
* override values.
*
* @public
*/
public clearOverrides(): void {
this._overrideMaxSpeed = null;
this._overrideAcceleration = null;
}
}
|