Craters v0.2.0

v0.2.0 is out. This is the first significant feature release since the TypeScript rebase, and it focuses on two things the framework was missing: physics and spatial awareness.


Collision Detection — SAT

The big addition is a full Separating Axis Theorem collision library. It handles the three pairs you actually run into in 2D games:

Pair Supported
Circle ↔ Circle Yes
Circle ↔ Polygon Yes
Polygon ↔ Polygon Yes

The Response object — which carries penetration depth, overlap vector, and the two objects involved — was also promoted from an interface to a class. This was a subtle but necessary change: interfaces can’t be instantiated, so collision code that called new Response() would silently fail or require workarounds. Now it just works.

import { SAT, Response } from '@swashvirus/craters';

const a = new SAT.Circle(new SAT.Vector(0, 0), 20);
const b = new SAT.Circle(new SAT.Vector(25, 0), 20);
const response = new Response();

if (SAT.testCircleCircle(a, b, response)) {
  console.log('overlap:', response.overlap); // 15
  console.log('vector:', response.overlapV);  // { x: 15, y: 0 }
}

QuadTree — Broad-Phase Partitioning

SAT alone solves how to detect a collision, but not when to bother checking. Naively testing every object against every other object is O(n²) — fine for 10 entities, painful at 500.

The new QuadTree handles broad-phase: partition the world into quadrants, only test objects in the same region.

import { QuadTree } from '@swashvirus/craters';

const tree = new QuadTree(0, { x: 0, y: 0, width: 800, height: 600 });

for (const entity of entities) {
  tree.insert(entity.bounds);
}

const candidates = tree.retrieve(player.bounds);
// candidates: only the entities near the player — not all 500

Canvas2DRenderer — Primitive Drawing

The renderer gains four new drawing methods for shapes directly on the canvas:

renderer.drawRect(10, 10, 100, 50, '#ff0000');
renderer.drawCircle(200, 200, 30, '#00ff00');
renderer.drawPolygon([[0,0],[50,0],[25,50]], '#0000ff');
renderer.drawLine(0, 0, 400, 300, '#ffffff');

This is useful for debug overlays (drawing hitboxes, QuadTree boundaries) without needing an external drawing library.


Input — Mouse & Touch

The Input class now tracks pointer position and the first touch binding:

import { Input } from '@swashvirus/craters';

const input = new Input(canvas);

// Works for both mouse and touch
console.log(input.pointerPosition); // { x: 320, y: 240 }
console.log(input.Touch0.pressed);  // true / false

This makes basic mobile-compatible input possible without additional event wiring.


ECS — Query Coverage

Test coverage for ECS query registration was improved. No API changes — this is stability work to make sure component queries are correctly registered and deregistered as entities are added and removed.


Installing

npm install @swashvirus/craters@0.2.0

Full diff: v0.1.2…v0.2.0

comments powered by Disqus