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 | 26x 13x 2x 11x 8x 4x 4x 4x 6x 2x 4x 4x 10x 4x 6x 6x 6x 6x | /**
* Physics-based type definitions for Black Trigram (흑괘)
*
* This module defines physics-first types where all positions, distances,
* and measurements are in **meters** or **centimeters**, never pixels.
* Pixel conversions happen ONLY at render time.
*
* @module types/PhysicsTypes
* @category Type Definitions
* @korean 물리타입
*/
import * as THREE from "three";
import type { Position3D } from "./physics";
/**
* Arena bounds with both pixel dimensions (for rendering) and
* world dimensions (for physics calculations).
*
* This allows proper conversion between screen space (pixels) and
* physics space (meters) without fixed constants.
*
* **Korean**: 경기장 경계 (Arena Bounds)
*
* @public
* @category Physics Types
*/
export interface PhysicsArenaBounds {
/** X coordinate of arena top-left corner (pixels, for rendering) */
readonly x: number;
/** Y coordinate of arena top-left corner (pixels, for rendering) */
readonly y: number;
/** Arena width in pixels (for rendering) */
readonly width: number;
/** Arena height in pixels (for rendering) */
readonly height: number;
/** Arena scale factor (1.0 = desktop, <1.0 = mobile, for 3D scene scaling) */
readonly scale: number;
/**
* Physical arena width in meters.
*
* This is the actual size of the combat arena in the game world.
* Combined with width (pixels), this gives pixels-per-meter ratio:
* `pixelsPerMeter = width / worldWidthMeters`
*
* Example: Desktop 960px / 10m = 96 px/m, Mobile 300px / 6m = 50 px/m
*/
readonly worldWidthMeters: number;
/**
* Physical arena depth in meters.
*
* Typically same as worldWidthMeters for square arenas.
* For rectangular arenas, may differ.
*
* Example: 10m × 10m square arena
*/
readonly worldDepthMeters: number;
}
/**
* Validates if a value is a valid Position3D or THREE.Vector3.
*
* @param pos - Value to check
* @returns True if valid position with x, y, z properties
* @internal
*/
function isValidPosition(pos: unknown): pos is Position3D | THREE.Vector3 {
return (
pos !== null &&
pos !== undefined &&
typeof pos === "object" &&
"x" in pos &&
"y" in pos &&
"z" in pos &&
typeof (pos as Position3D).x === "number" &&
typeof (pos as Position3D).y === "number" &&
typeof (pos as Position3D).z === "number" &&
Number.isFinite((pos as Position3D).x) &&
Number.isFinite((pos as Position3D).y) &&
Number.isFinite((pos as Position3D).z)
);
}
/**
* Calculate pixels-per-meter ratio from arena bounds.
*
* This is the fundamental conversion factor between screen space (pixels)
* and physics space (meters). It varies by device resolution and arena size.
*
* **Korean**: 픽셀당미터비율 (Pixels Per Meter Ratio)
*
* @param bounds - Arena bounds with pixel and meter dimensions
* @returns Conversion ratio (pixels per meter)
* @throws Error if worldWidthMeters is not positive
*
* @example
* ```typescript
* // Desktop: 960px arena, 10m world
* const desktopPxPerM = getPixelsPerMeter({ width: 960, worldWidthMeters: 10, ... });
* // Result: 96 px/m
*
* // Mobile: 300px arena, 6m world
* const mobilePxPerM = getPixelsPerMeter({ width: 300, worldWidthMeters: 6, ... });
* // Result: 50 px/m
* ```
*
* @public
* @category Physics Types
*/
export function getPixelsPerMeter(bounds: PhysicsArenaBounds): number {
if (bounds.worldWidthMeters <= 0) {
throw new Error(
`worldWidthMeters must be positive, got: ${bounds.worldWidthMeters}`
);
}
return bounds.width / bounds.worldWidthMeters;
}
/**
* Convert physics position (meters) to screen position (pixels).
*
* **IMPORTANT**: This function only handles scaling conversion between
* meters and pixels. It does NOT account for arena position offsets
* (bounds.x and bounds.y). Callers must add these offsets separately
* when rendering to screen.
*
* Example:
* ```typescript
* const pixelPos = metersToPixels(physicsPos, bounds);
* const screenX = bounds.x + pixelPos.x; // Add arena offset
* const screenY = bounds.y + pixelPos.y; // Add arena offset
* ```
*
* This conversion should ONLY be used at render time.
* Internal game logic should always work in meters.
*
* **Korean**: 미터를픽셀로 (Meters To Pixels)
*
* @param positionMeters - Position in meters
* @param bounds - Arena bounds for conversion
* @returns Position in pixels RELATIVE to arena origin (add bounds.x/y for screen position)
* @throws Error if position is invalid
*
* @public
* @category Physics Types
*/
export function metersToPixels(
positionMeters: Position3D | THREE.Vector3,
bounds: PhysicsArenaBounds
): { x: number; y: number } {
if (!isValidPosition(positionMeters)) {
throw new Error(
"Invalid position: must be Vector3 or Position3D with numeric x, y, z properties"
);
}
const pixelsPerMeter = getPixelsPerMeter(bounds);
return {
x: positionMeters.x * pixelsPerMeter,
y: positionMeters.z * pixelsPerMeter, // Z axis maps to screen Y
};
}
/**
* Convert screen position (pixels) to physics position (meters).
*
* **IMPORTANT**: This function only handles scaling conversion between
* pixels and meters. If you're converting from screen coordinates, you
* must first subtract arena position offsets (bounds.x and bounds.y).
*
* Example:
* ```typescript
* const arenaRelativeX = screenX - bounds.x; // Remove arena offset
* const arenaRelativeY = screenY - bounds.y; // Remove arena offset
* const physicsPos = pixelsToMeters({ x: arenaRelativeX, y: arenaRelativeY }, bounds);
* ```
*
* Use this when converting user input or initial positions.
* After conversion, work entirely in meters.
*
* **Korean**: 픽셀을미터로 (Pixels To Meters)
*
* @param pixelPosition - Position in pixels RELATIVE to arena origin (subtract bounds.x/y first)
* @param bounds - Arena bounds for conversion
* @returns Position in meters
* @throws Error if pixel coordinates are not finite numbers
*
* @public
* @category Physics Types
*/
export function pixelsToMeters(
pixelPosition: { x: number; y: number },
bounds: PhysicsArenaBounds
): Position3D {
if (!Number.isFinite(pixelPosition.x) || !Number.isFinite(pixelPosition.y)) {
throw new Error(
`Invalid pixel coordinates: x=${pixelPosition.x}, y=${pixelPosition.y}`
);
}
const pixelsPerMeter = getPixelsPerMeter(bounds);
return {
x: pixelPosition.x / pixelsPerMeter,
y: 0, // Ground plane
z: pixelPosition.y / pixelsPerMeter, // Screen Y maps to Z axis
};
}
/**
* Calculate distance between two positions in meters.
*
* This is the correct way to calculate distance - always in meters,
* never in pixels.
*
* **Korean**: 거리계산 (Distance Calculation)
*
* @param pos1 - First position in meters
* @param pos2 - Second position in meters
* @returns Distance in meters
* @throws Error if either position is invalid
*
* @public
* @category Physics Types
*/
export function calculateDistanceMeters(
pos1: Position3D | THREE.Vector3,
pos2: Position3D | THREE.Vector3
): number {
if (!isValidPosition(pos1) || !isValidPosition(pos2)) {
throw new Error(
"Invalid positions for distance calculation: both positions must be valid Vector3 or Position3D objects"
);
}
const dx = pos2.x - pos1.x;
const dy = pos2.y - pos1.y;
const dz = pos2.z - pos1.z;
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
|