Bullets, arrows, missiles, lasers, grenades and spells. Spawning from pools, aiming with lead (intercept moving targets), spread and ring patterns, ballistic arcs with gravity (angle to hit a point), homing with turn limits, bouncing, piercing, lifetime and range, sweep tests so fast bullets never pass through targets, hitscan lasers with raycasts, and bullet patterns for shooters. Includes tes...
Download skill (.zip) Files on GitHubProjectile system
Drop-in: assets/projectiles.js (tests: lead aim intercepts a moving target, fan and ring spreads, ballistic arcs hit their
target in simulation, homing reaches the target at constant speed, sweep finds the nearest thin target at 6000 px/s, bounce, pierce).
import { leadAngle, spread, ballisticAngles, home, sweepHit, stepProjectile, onHit } from "./core/projectiles.js";
// fire a 5-bullet fan toward where the player will be
for (const a of spread(leadAngle(e.x, e.y, player, 350), 5, 0.6)) bullets.spawn((b) => Object.assign(b, { x: e.x, y: e.y, vx: Math.cos(a) * 350, vy: Math.sin(a) * 350, life: 3, pierce: 0 }));
// each step
bullets.update((b) => {
const hit = sweepHit(b, dt, enemiesNear(b), b.r); // before moving: catches fast bullets
if (hit && !onHit(b, hit.target)) { damage(hit.target, b); return false; }
if (b.homing) home(b, target.x, target.y, 3.5, dt);
return stepProjectile(b, dt, { gravity: b.gravity ?? 0, bounds: arena });
});
Types
| Type | Recipe |
|---|---|
| Bullet | Pool + stepProjectile + sweepHit |
| Shotgun / fan / ring | spread(angle, count, arc) (arc = 2π for rings) |
| Grenade / arrow | Gravity in stepProjectile; aim with ballisticAngles(dx, dy, speed, g) → [flat, lob] or null (out of range) |
| Missile | home() with a turn rate (2–5 rad/s) so it can be dodged |
| Bouncer | bounces: n with bounds, or reflect off walls (game-physics bounce) |
| Piercing | pierce: n + onHit (each target once) |
| Laser / hitscan | No projectile: raycastGrid / rayRect (collision-detection), draw a beam for 1–3 frames |
Rules
- Pool everything (game-object-pooling). Cap the count.
- Enemy bullets: readable (bright, outlined, slower than the player can dodge), with hitboxes smaller than the art.
- Player bullets: fast and generous. Muzzle flash, small recoil and a sound on every shot.
- Spawn at the gun’s tip, not the center. Inherit some of the shooter’s velocity for feel if it helps.
- Bullet-hell patterns: combine rings and spreads with rotating base angles over time (bullet-hell-development).