miaoda-game-vision2d-core
v0.3.0
Published
Engine-agnostic 2D vision core: line-of-sight, field-of-view cones, and visibility-polygon (shadow-casting) computation against segment/rect/polygon obstacles. The gameplay-level visibility layer Cocos (and most engines) don't ship — stealth detection, AI
Downloads
611
Maintainers
Readme
miaoda-game-vision2d-core
Use this package for continuous 2D visibility: line of sight, guard FOV, flashlight cones, visibility polygons, and shadow-cast reveal. It returns booleans or vertex rings; it does not render, run physics, or use a spatial index.
Use grid-vision-core when visibility is defined per tile. Use an engine raycast when you only need the first real collider hit.
Install and query visibility
pnpm add miaoda-game-vision2d-coreimport { Obstacles, lineOfSight, inFieldOfView, visibilityCone } from 'miaoda-game-vision2d-core';
const obstacles = new Obstacles()
.addBounds({ minX: 0, minY: 0, maxX: 960, maxY: 640 })
.addRect({ minX: 400, minY: 300, maxX: 460, maxY: 360 });
const guard = { x: 200, y: 200 };
const player = { x: 500, y: 220 };
lineOfSight(obstacles, guard, player, 300);
inFieldOfView(obstacles, guard, 0, 70, player, 400);
const cone = visibilityCone(obstacles, guard, 0, 70, 400);
drawPolygon(cone);Angles are degrees, 0° points along +X, and fovDeg is the total cone width. Coordinates are continuous; the skin decides screen orientation.
Obstacles and visibility rings
Register segments, rectangles, polygons, or an outer bounds rectangle once. addBounds closes the world so rays do not escape to infinity. visibilityPolygon computes a full 360-degree ring; visibilityCone returns a cone fan with the viewer as its apex.
For large worlds, feed each query only nearby segments from your own broadphase. The core intentionally scans the supplied obstacle set directly. It does not model attenuation, soft shadows, colors, tile FOV, or physics colliders.
