Bastion Works: Towers, Turrets and Targeting Kit
README.md
projectileKit.motion
How a projectile moves, as pure maths. A projectile is a plain table and step
advances it by dt; there are no entities and no clock, so the same shot fired on two
peers with the same numbers flies the same way, and the tests drive it frame by frame.
Called by projectileKit.api (which launches and steps every live shot) and by
turretsKit for lead aiming. Require it as ~.motion inside the package.
Motion models
M.KINDS = { "bullet", "shell", "missile" }
bullet: straight line atspeedtowardto(or a moving target).shell: ballistic arc undergravitythat lands exactly onto, peakingarcmetres above the higher end, so it lobs over walls.missile: accelerates tospeedand turns toward its target atturnRatedegrees per second; the turn tightens the longer it flies, so it closes instead of orbiting.
Any other kind is treated as bullet.
Projectile fields read by launch / step
kind, from, to, speed (default 40), arc (default 4), gravity (default 18),
radius (hit radius, default 0.6), maxLife, and for missiles launchDir, startSpeed
(default speed * 0.35), accel (default speed * 1.5), turnRate (default 180).
launch writes pos, prev, vel, t, flight and maxLife.
Functions
solveBallistic(from: Vec, to: Vec, arc: number, gravity: number): (Vec, number)- launch velocity and flight time for a shell that lands exactly onto.lead(from: Vec, pos: Vec, vel: Vec?, speed: number): (Vec, number): aim point and time to impact for a projectile ofspeedmeeting a target atposmoving withvel(first-order lead with two refinements).launch(p: any): fills in a projectile's starting state from its spec.step(p: any, dt: number, targetPos: Vec?): string?: advances a launched projectile.targetPosis where its target is now (nilwhen it has none). Returnsnilwhile flying, or"hit"(touched the target),"arrive"(reached its aim point) or"expire"(ran out of life).predict(p: any, t: number): Vec: where the projectile would be aftertseconds if nothing changes (works on a copy; for previews and tests).