space-browser / commits / eb9677f
fix: you can now orbit far orbiting planets
@@ -8,14 +8,16 @@ import (
88 "github.com/gorilla/websocket"
99 )
1010
11-// The rig flies as one body: steer toward the target, never past it.
12-func (p *player) steer(dt float64) {
11+// The rig flies as one body: steer toward the target, never past it. A target that
12+// moves carries its own velocity, and the ship flies that plus its closing speed, so
13+// the slowdown ramp measures the gap that is left and not a point that runs away.
14+func (p *player) steer(dt, tvx, tvy float64) {
1315 dx, dy := p.TX-p.X, p.TY-p.Y
1416 dist := math.Hypot(dx, dy)
15- var wantX, wantY float64
17+ wantX, wantY := tvx, tvy
1618 if dist >= settle {
1719 s := math.Min(maxSpeed, dist*arrive)
18- wantX, wantY = dx/dist*s, dy/dist*s
20+ wantX, wantY = tvx+dx/dist*s, tvy+dy/dist*s
1921 }
2022 dvx, dvy := wantX-p.vx, wantY-p.vy
2123 if dl, max := math.Hypot(dvx, dvy), accel*dt; dl > max {
@@ -50,22 +52,27 @@ func (h *hub) step(dt float64) {
5052 if p.AI {
5153 h.think(p, dt) // patrol a yard, or run down whoever it is fighting
5254 }
55+ var tvx, tvy float64
5356 if b := h.body(p.orbit); b != nil {
5457 // The ring point turns with the body around its star, so the ship is
55- // carried with it even in another system.
58+ // carried with it even in another system. Keeping up with it is the
59+ // point's own velocity, which grows with the ring: chasing only where it
60+ // was leaves the ship parked short of a fast body and never docked.
5661 d := b.speed * (h.time - p.anchored)
5762 cs, sn := math.Cos(d), math.Sin(d)
5863 p.TX = b.cx + p.ox*cs - p.oy*sn
5964 p.TY = b.cy + p.ox*sn + p.oy*cs
65+ tvx, tvy = b.speed*-(p.TY-b.cy), b.speed*(p.TX-b.cx)
6066 }
6167 if o := h.player(p.chase); o != nil {
68+ tvx, tvy = 0, 0 // the hull being closed on is aimed at, not led
6269 if math.Hypot(o.X-p.X, o.Y-p.Y) > gunRange*approach {
6370 p.TX, p.TY = o.X, o.Y // close in until the guns can reach
6471 } else {
6572 p.TX, p.TY = p.X, p.Y // in range: settle and keep shooting
6673 }
6774 }
68- p.steer(dt)
75+ p.steer(dt, tvx, tvy)
6976 // An order is spent once the ship reaches it, and a spent order is nothing to
7077 // mark: a point when the ship settles on it, an orbit when it is docked on the
7178 // ring. A flown point stops being a target at all; an orbit has to keep its