Craters v0.2.0

v0.2.0 is out — the first real feature release after the TypeScript rebase. It adds two things the framework was clearly missing: proper collision detection and spatial partitioning.


Collision Detection — SAT

The main addition is a full Separating Axis Theorem collision library. It covers the three shape pairs you’ll actually run into in a 2D game:

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

The Response object — which carries penetration depth, overlap vector, and both objects involved — was promoted from an interface to a class. That change sounds minor, but it matters: interfaces can’t be instantiated, so code calling new Response() would silently break or require a workaround. 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 tells you how to detect a collision, but not when to bother checking. Naively testing every entity against every other entity is O(n²) — fine at 10 objects, painful at 500.

The new QuadTree handles broad-phase partitioning: divide the world into quadrants, only test objects in the same region against each other.

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 entities near the player — not all 500

Canvas2DRenderer — Primitive Drawing

The renderer gets four new methods for drawing shapes directly to 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');

Useful for debug overlays — drawing hitboxes, QuadTree cell boundaries — without pulling in a separate drawing library.


Input — Mouse & Touch

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

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

const input = new Input(canvas);

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

Basic mobile-compatible input without extra event wiring.


ECS — Query Coverage

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


Install

npm install @swashvirus/craters@0.2.0

Full diff: v0.1.2…v0.2.0

comments powered by Disqus