Log inGet started
World

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 at speed toward to (or a moving target).
  • shell: ballistic arc under gravity that lands exactly on to, peaking arc metres above the higher end, so it lobs over walls.
  • missile: accelerates to speed and turns toward its target at turnRate degrees 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 on to.
  • lead(from: Vec, pos: Vec, vel: Vec?, speed: number): (Vec, number): aim point and time to impact for a projectile of speed meeting a target at pos moving with vel (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. targetPos is where its target is now (nil when it has none). Returns nil while 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 after t seconds if nothing changes (works on a copy; for previews and tests).