Download src/collision.ts from gpue/flyyt: direct link, hf CLI and curl.
- Browser
- Download file 8.66 kB
-
https://huggingface.co/spaces/gpue/flyyt/resolve/main/src/collision.ts
- Command line
-
hf download hf://spaces/gpue/flyyt/src/collision.ts
-
curl -L -o collision.ts https://huggingface.co/spaces/gpue/flyyt/resolve/main/src/collision.ts
8.66 kB
| import { ALL_OBSTACLES, FLOOR_SURFACES, FRUIT_FLOOR_SURFACES, ROOM_CONFIG } from "./roomLayout"; | |
| import { FLY_EXTENT_MM, FLY_GROUND_OFFSET_MM } from "./useFlyLayout"; | |
| import type { LiveFlyPositions } from "./useLiveFlyState"; | |
| // Half the fly's own footprint -- the fly's CENTER is kept this far from an | |
| // obstacle's edge, treating it as a point offset by its own radius rather | |
| // than a full second AABB-vs-AABB test. Simple, and consistent with the | |
| // previous code's point-based treatment of the fly against the outer | |
| // rectangle. | |
| const FLY_RADIUS_X_MM = FLY_EXTENT_MM[0] / 2; | |
| const FLY_RADIUS_Z_MM = FLY_EXTENT_MM[2] / 2; | |
| const HALF_WIDTH = ROOM_CONFIG.widthMm / 2; | |
| const HALF_DEPTH = ROOM_CONFIG.depthMm / 2; | |
| function clamp(value: number, min: number, max: number): number { | |
| return Math.min(max, Math.max(min, value)); | |
| } | |
| /** | |
| * Per-axis reject-and-revert against every obstacle AABB (inflated by the | |
| * fly's own half-extent) plus the outer room bounds -- the same | |
| * "clamp each axis independently" idiom the code already used for the | |
| * outer rectangle, generalized to N boxes and applied with axis-independent | |
| * revert-to-previous rather than a single min/max (an interior obstacle | |
| * isn't centered at the origin, so a plain clamp can't express "stay | |
| * outside this box"). | |
| * | |
| * `y` is the fly's height from the PREVIOUS frame (this frame's target, | |
| * still being eased toward) -- an obstacle with a `topY` (a landable | |
| * surface: table/chair/sofa/shelf) only blocks horizontal movement while | |
| * the fly is below that height; once a flying fly has climbed above it, | |
| * it's free to move over that footprint and can then descend to land on | |
| * top. Obstacles with no `topY` (walls, the plant) have no landable top, | |
| * so they block at every height -- consistent with flight altitude never | |
| * exceeding the room's wall height anyway. | |
| * | |
| * Checks the DESTINATION point against each obstacle and, for any axis that | |
| * would end up inside it, clamps JUST that axis to the box's nearest edge | |
| * on the fly's approach side (not swept/continuous collision, so a | |
| * fast-moving fly could in principle tunnel through a thin obstacle within | |
| * one frame, but flies move slowly relative to wall/furniture thickness -- | |
| * an accepted scope tradeoff for "simple" collision). Clamping to the | |
| * nearest edge rather than reverting all the way back to prevX/prevZ is | |
| * what makes a fast approach slide smoothly right up to the surface and | |
| * stop flush against it, instead of visibly snapping back a full frame's | |
| * worth of motion. | |
| * | |
| * Accepted edge case: the topY skip above uses last frame's `y`. In the | |
| * one-frame window where a fly's `y` first drops below a box's `topY` | |
| * while its XZ is already over that box's footprint (the instant of | |
| * landing on it), this obstacle briefly "activates" and could nudge the | |
| * fly to its edge instead of just letting it settle -- a sub-frame, | |
| * likely-imperceptible flicker at 60fps. Fixing it properly would need | |
| * extra per-fly-per-box exemption state; not worth it for "simple" | |
| * collision. | |
| */ | |
| export function resolvePosition(x: number, z: number, prevX: number, prevZ: number, y: number): { x: number; z: number } { | |
| let nx = clamp(x, -HALF_WIDTH + FLY_RADIUS_X_MM, HALF_WIDTH - FLY_RADIUS_X_MM); | |
| let nz = clamp(z, -HALF_DEPTH + FLY_RADIUS_Z_MM, HALF_DEPTH - FLY_RADIUS_Z_MM); | |
| for (const box of ALL_OBSTACLES) { | |
| if (box.topY !== undefined && y >= box.topY) continue; | |
| const minX = box.minX - FLY_RADIUS_X_MM; | |
| const maxX = box.maxX + FLY_RADIUS_X_MM; | |
| const minZ = box.minZ - FLY_RADIUS_Z_MM; | |
| const maxZ = box.maxZ + FLY_RADIUS_Z_MM; | |
| const insideX = nx > minX && nx < maxX; | |
| const insideZ = nz > minZ && nz < maxZ; | |
| if (insideX && insideZ) { | |
| // Clamp whichever axis actually caused the new overlap (the one that | |
| // moved into range this frame) to the box's nearest edge -- keeps | |
| // per-axis independence, so sliding along a wall/table edge still | |
| // works instead of freezing outright the instant any obstacle is | |
| // touched. | |
| const wasInsideX = prevX > minX && prevX < maxX; | |
| if (wasInsideX) nz = prevZ <= (minZ + maxZ) / 2 ? minZ : maxZ; | |
| else nx = prevX <= (minX + maxX) / 2 ? minX : maxX; | |
| } | |
| } | |
| return { x: nx, z: nz }; | |
| } | |
| /** | |
| * "What height counts as ground/support directly under (x,z)" -- flat room | |
| * floor (0) by default, or the top of whichever surface's footprint | |
| * contains this point (e.g. a fly standing over the table's or rug's | |
| * footprint would rest on that surface, not the bare floor). Deliberately | |
| * checks FLOOR_SURFACES, not ALL_OBSTACLES -- the rug has a real (if thin) | |
| * surface height but must NOT horizontally block movement the way the | |
| * table/walls do. Single source of truth for both the landing target and | |
| * the defensive per-frame clamp in FlyInstances.tsx. | |
| */ | |
| export function floorHeightAt(x: number, z: number): number { | |
| let height = 0; | |
| for (const box of FLOOR_SURFACES) { | |
| if (box.topY === undefined) continue; | |
| if (x >= box.minX && x <= box.maxX && z >= box.minZ && z <= box.maxZ) { | |
| height = Math.max(height, box.topY); | |
| } | |
| } | |
| return height; | |
| } | |
| // Roughly one body length (the longer of the fly's own X/Z extents) -- | |
| // close enough that two visibly-overlapping flies nudge apart, not a large | |
| // "personal space" bubble. | |
| const MIN_SEPARATION_MM = Math.max(FLY_EXTENT_MM[0], FLY_EXTENT_MM[2]); | |
| // Deliberately smaller than one altitudeLevels.ts step (~122mm) so a fly | |
| // hovering one level above another is never affected, but two flies | |
| // resting on the same floor/tabletop (small y jitter only) are. | |
| const ALTITUDE_GATE_MM = 50; | |
| // Spring-like: closes a FRACTION of the overlap each frame (exponential | |
| // approach, same idiom as wingController.ts's smoothedDrive), never an | |
| // instant snap-to-distance. | |
| const SEPARATION_TAU_S = 0.2; | |
| /** | |
| * Gently pushes any two flies apart once their horizontal distance drops | |
| * below MIN_SEPARATION_MM AND they're at a similar altitude (ALTITUDE_GATE_MM) | |
| * -- a flying fly high above a walking one is unaffected. Purely physical | |
| * (keeps bodies from visibly overlapping); complementary to, not a | |
| * replacement for, loomingPerception.ts's sensory/behavioral wing reaction. | |
| * Re-clamps each nudged fly against ALL_OBSTACLES afterward (reusing | |
| * resolvePosition) so a push near a wall/table edge can't shove a fly | |
| * through it. | |
| */ | |
| export function applyFlySeparation(livePositions: LiveFlyPositions, dt: number): void { | |
| const pushAlpha = 1 - Math.exp(-dt / SEPARATION_TAU_S); | |
| const states = Array.from(livePositions.values()); | |
| for (let i = 0; i < states.length; i++) { | |
| const a = states[i]; | |
| for (let j = i + 1; j < states.length; j++) { | |
| const b = states[j]; | |
| if (Math.abs(a.y - b.y) > ALTITUDE_GATE_MM) continue; | |
| let dx = b.x - a.x; | |
| let dz = b.z - a.z; | |
| let dist = Math.hypot(dx, dz); | |
| if (dist >= MIN_SEPARATION_MM) continue; | |
| if (dist < 1e-6) { | |
| // Degenerate/identical-position case -- push along a deterministic | |
| // arbitrary axis rather than dividing by zero. | |
| dx = 1; | |
| dz = 0; | |
| dist = 1; | |
| } | |
| const overlap = MIN_SEPARATION_MM - dist; | |
| const ux = dx / dist; | |
| const uz = dz / dist; | |
| const push = overlap * pushAlpha * 0.5; // half the correction to each fly | |
| const prevAX = a.x; | |
| const prevAZ = a.z; | |
| const prevBX = b.x; | |
| const prevBZ = b.z; | |
| a.x -= ux * push; | |
| a.z -= uz * push; | |
| b.x += ux * push; | |
| b.z += uz * push; | |
| const resolvedA = resolvePosition(a.x, a.z, prevAX, prevAZ, a.y); | |
| a.x = resolvedA.x; | |
| a.z = resolvedA.z; | |
| const resolvedB = resolvePosition(b.x, b.z, prevBX, prevBZ, b.y); | |
| b.x = resolvedB.x; | |
| b.z = resolvedB.z; | |
| } | |
| } | |
| } | |
| // How close live.y must be to a fruit's actual resting height (topY + | |
| // FLY_GROUND_OFFSET_MM, matching floorHeightAt's landing-target convention) | |
| // to count as "resting on it" -- distinguishes actually landed from flying | |
| // past overhead at any altitude. | |
| const FRUIT_LANDED_TOLERANCE_MM = 5; | |
| /** Is this fly currently grounded AND resting specifically on one of the | |
| * fruit props (not just somewhere else on the table)? Used to gate the | |
| * Enter-key "bite" feature. */ | |
| export function isRestingOnFruit(x: number, y: number, z: number, flying: boolean): boolean { | |
| if (flying) return false; | |
| for (const box of FRUIT_FLOOR_SURFACES) { | |
| if (x < box.minX || x > box.maxX || z < box.minZ || z > box.maxZ) continue; | |
| if (box.topY !== undefined && Math.abs(y - (box.topY + FLY_GROUND_OFFSET_MM)) <= FRUIT_LANDED_TOLERANCE_MM) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |