space-browser / commits / 9938f83
feat: better hit detection and visual accuracy
@@ -96,12 +96,12 @@ There is no ship size cap. A bigger ship hits harder and survives more, and is a
9696
9797 Combat is frequent, automated, and controlled purely through movement and positioning.
9898
99-- **Auto-Shooting:** Triangles fire automatically at the nearest hostile within weapons range. There is no targeting, no target list, and no firing button: which enemy you hit is decided by where you are.
99+- **Auto-Shooting:** Triangles fire automatically at the nearest hostile within weapons range. Left click to target, no target list, and no firing button: which enemy piece you hit is decided by where you click.
100100 - **Visible Weapon Ranges:**
101101 - The player's own attack range ring is clearly drawn in space around their ship.
102102 - Enemy weapon range rings are clearly drawn when approaching or engaging hostiles, so the player always sees the exact line between safety and danger.
103103 - **Movement as Control:** Because firing is automatic and projectiles hit physical pieces on the hull, combat skill comes from kiting, maintaining optimal range, using orbital motion, and rotating the ship so Square armor pieces absorb fire before Triangle guns or the root piece are severed. Leaving range is the only way to break off a fight.
104-- **Players Are Not Special:** The same rules apply to other players. Automated fire does not ask permission; anyone in range gets shot, and Galaxy Security hunts whoever shoots first in a core system. Every piece lost or gained in a fight stays lost or gained.
104+- **Players Are Not Special:** The same rules apply to other players. Every piece lost or gained in a fight stays lost or gained.
105105 - **What Combat Pays:** Ship pieces come only from wrecks. Ore comes from barren planets and from the loose ore left in wrecks. You can survive by mining, but everything you fly comes from fights you won.
106106
107107 ---
@@ -1,6 +1,7 @@
11 // Intents: every pointer and key here becomes a message for the server, and the
22 // server decides what happened. The only local decisions are the camera's.
33 import { aim } from './dev';
4+import { pull } from './pull';
45 import { at, cam, pan, wpc, wpp, zoomAt } from './stage';
56 import { send } from './world';
67
@@ -19,6 +20,7 @@ export function bind(): void {
1920 }
2021 const p = at(e);
2122 pulling = true;
23+ pull.to = p; // the piece in hand sits under the pointer, not a tick behind it
2224 send({ t: 'pick', x: p.x, y: p.y });
2325 });
2426
@@ -30,7 +32,7 @@ export function bind(): void {
3032 return;
3133 }
3234 if (!pulling) return;
33- pullTo = at(e);
35+ pull.to = pullTo = at(e);
3436 });
3537
3638 addEventListener('pointerup', (e) => {
@@ -41,6 +43,7 @@ export function bind(): void {
4143 if (!pulling) return;
4244 pulling = false;
4345 pullTo = null;
46+ pull.to = null;
4447 const p = at(e);
4548 send({ t: 'drop', x: p.x, y: p.y });
4649 });
@@ -0,0 +1,6 @@
1+// Where the piece in hand is being dragged to, in world space. Picking up and
2+// dragging is direct manipulation: the client says where the piece is and the
3+// server holds it at exactly that point, so the piece is drawn under the pointer
4+// instead of one tick behind it. Nothing but the place of that one piece comes
5+// from here, and it is never a decision about the world.
6+export const pull: { to: { x: number; y: number } | null } = { to: null };
@@ -38,6 +38,7 @@ export type Ship = {
3838 fuel: number; // what is in the tank
3939 fuelmax: number; // capacity, summed from the hull
4040 foe: number; // the ship this one is hostile with, 0 = neutral
41+ target: number; // piece of that ship its guns are pointed at, 0 = the nearest
4142 ai: boolean; // a server-run hull, so it draws in its own colour
4243 pieces: Piece[];
4344 };
@@ -2,6 +2,7 @@
22 // thing is at tick rate, we only smooth the way it gets there. Nothing here decides
33 // anything, and the camera that follows our ship rides along with it.
44 import { connect, type State } from './net';
5+import { pull } from './pull';
56 import type { Loose, Ore, Ship, Shot } from './sim';
67 import { cam, snap } from './stage';
78
@@ -59,7 +60,11 @@ export function apply(dt: number): void {
5960 looseLive.add(l.id);
6061 const d = loose.get(l.id);
6162 if (!d) loose.set(l.id, { ...l });
62- else {
63+ else if (l.id === state.held && pull.to) {
64+ d.x = pull.to.x; // in hand: exactly where the pointer put it, no easing
65+ d.y = pull.to.y;
66+ d.h = l.h;
67+ } else {
6368 d.x = ease(d.x, l.x, k);
6469 d.y = ease(d.y, l.y, k);
6570 d.h = turn(d.h, l.h, k);
@@ -2,11 +2,14 @@ package main
22
33 import "math"
44
5+// The piece of a hull under a point, if any. Each piece's own outline decides, and
6+// where two outlines overlap the nearer centre wins, so a click in the seam between
7+// two pieces picks the one it is closest to.
58 func hullPieceAt(p *player, x, y float64) int {
6- id, best := 0, grabR
9+ id, best := 0, math.MaxFloat64
710 for _, q := range p.Pieces {
811 wx, wy := hullToWorld(p, q.LX, q.LY)
9- if d := math.Hypot(wx-x, wy-y); d < best {
12+ if d := math.Hypot(wx-x, wy-y); d < best && pieceAt(q.Kind, wx, wy, p.H, x, y) {
1013 best, id = d, q.ID
1114 }
1215 }
@@ -19,22 +22,9 @@ func hullToWorld(p *player, lx, ly float64) (float64, float64) {
1922 }
2023
2124 func hullToLocal(p *player, x, y float64) (float64, float64) {
22- c, s := math.Cos(p.H), math.Sin(p.H)
23- dx, dy := x-p.X, y-p.Y
24- return dx*c + dy*s, -dx*s + dy*c
25+ return inward(p.X, p.Y, p.H, x, y)
2526 }
2627
2728 func wrap(a float64) float64 { return math.Atan2(math.Sin(a), math.Cos(a)) }
2829
29-// How far a point lies from a segment, and how far along that segment the closest
30-// approach is, so the earliest piece on a round's path can be picked.
31-func segDist(x0, y0, x1, y1, px, py float64) (float64, float64) {
32- dx, dy := x1-x0, y1-y0
33- t := 0.0
34- if l := dx*dx + dy*dy; l > 0 {
35- t = math.Max(0, math.Min(1, ((px-x0)*dx+(py-y0)*dy)/l))
36- }
37- return math.Hypot(x0+t*dx-px, y0+t*dy-py), t
38-}
39-
4030 func finite(f float64) bool { return !math.IsNaN(f) && !math.IsInf(f, 0) }
@@ -3,8 +3,8 @@ package main
33 import "math"
44
55 // A neutral ship never opens fire. Once two hulls are hostile, every triangle that
6-// has the enemy in range fires one round a salvo, and the round has to be flown
7-// into: leaving range or dodging sideways is the whole defence.
6+// has the aimed piece in range fires one round a salvo, and the round has to be
7+// flown into: leaving range or dodging sideways is the whole defence.
88 func (h *hub) guns(p *player, dt float64) {
99 p.cool = math.Max(0, p.cool-dt)
1010 if p.cool > 0 {
@@ -14,17 +14,18 @@ func (h *hub) guns(p *player, dt float64) {
1414 if t == nil {
1515 return
1616 }
17+ tx, ty := aim(p, t)
1718 fired := false
1819 for _, q := range p.Pieces {
1920 if q.Kind != "triangle" {
2021 continue // only a gun shoots
2122 }
2223 x, y := hullToWorld(p, q.LX, q.LY)
23- if math.Hypot(t.X-x, t.Y-y) > gunRange {
24+ if math.Hypot(tx-x, ty-y) > gunRange {
2425 continue
2526 }
2627 h.shots = append(h.shots, &shot{ID: h.pieceID(), X: x, Y: y,
27- H: math.Atan2(t.Y-y, t.X-x), owner: p.ID, left: gunRange})
28+ H: math.Atan2(ty-y, tx-x), owner: p.ID, left: gunRange})
2829 fired = true
2930 }
3031 if fired {
@@ -32,6 +33,23 @@ func (h *hub) guns(p *player, dt float64) {
3233 }
3334 }
3435
36+// The point the guns are pointed at: the piece the click picked, or the nearest
37+// piece of the same hull once that one is gone, so a fight never stalls on an aim
38+// that no longer describes anything.
39+func aim(p, o *player) (float64, float64) {
40+ tx, ty, best := 0.0, 0.0, math.MaxFloat64
41+ for _, q := range o.Pieces {
42+ x, y := hullToWorld(o, q.LX, q.LY)
43+ if q.ID == p.Target {
44+ return x, y
45+ }
46+ if d := math.Hypot(x-p.X, y-p.Y); d < best {
47+ tx, ty, best = x, y, d
48+ }
49+ }
50+ return tx, ty
51+}
52+
3553 // The closest ship this hull is hostile with. Rounds hit whatever they touch, so
3654 // this only decides who the guns are pointed at.
3755 func (h *hub) foeNear(p *player) *player {
@@ -67,8 +85,9 @@ func (h *hub) stepShots(dt float64) {
6785 h.shots = live
6886 }
6987
70-// A round is checked as the segment it swept this tick, so a fast one can never
71-// tunnel through a piece between two ticks. The first piece on the path takes it.
88+// A round is checked as the segment it swept this tick against the outlines it
89+// crossed, so a fast one can never tunnel through a piece between two ticks. The
90+// piece nearest the shot's tail takes it, which is the first one on the path.
7291 func (h *hub) shotHit(s *shot, nx, ny float64) (*player, int) {
7392 var got *player
7493 id, best := 0, math.MaxFloat64
@@ -78,8 +97,8 @@ func (h *hub) shotHit(s *shot, nx, ny float64) (*player, int) {
7897 }
7998 for _, q := range p.Pieces {
8099 x, y := hullToWorld(p, q.LX, q.LY)
81- if d, t := segDist(s.X, s.Y, nx, ny, x, y); d < pieceR && t < best {
82- best, got, id = t, p, q.ID
100+ if d := math.Hypot(x-s.X, y-s.Y); d < best && pieceCross(q.Kind, x, y, p.H, s.X, s.Y, nx, ny) {
101+ best, got, id = d, p, q.ID
83102 }
84103 }
85104 }
@@ -0,0 +1,80 @@
1+package main
2+
3+import "math"
4+
5+// A piece's hitbox is the outline it is drawn with, at the size it is drawn and
6+// turned by the angle it is drawn at, so a click or a round lands on the glyphs
7+// that are lit and on nothing else. A radius test would be a blob nobody can see,
8+// and sharing that radius with anything else would silently tie two decisions
9+// together: the size of a hull's click target is not a tunable of its own.
10+//
11+// The numbers are the client's, from view/shapes.ts, because the outline is a fact
12+// about the drawing and not a second opinion about it.
13+var outline = map[string][][2]float64{
14+ "triangle": poly(3, pieceR, 0, 0),
15+ "square": poly(4, pieceR*0.8, 0, math.Pi/4),
16+ "circle": poly(18, pieceR*0.75, 0, 0),
17+ "star": poly(10, pieceR, pieceR*0.45, -math.Pi/2),
18+}
19+
20+// The polygon the client builds: n points on a circle of radius r, every other one
21+// pulled in to inner when inner is set, the whole thing tilted by tilt.
22+func poly(n int, r, inner, tilt float64) [][2]float64 {
23+ vs := make([][2]float64, n)
24+ for i := range vs {
25+ a := tilt + float64(i)/float64(n)*2*math.Pi
26+ if inner > 0 && i%2 == 1 {
27+ vs[i] = [2]float64{math.Cos(a) * inner, math.Sin(a) * inner}
28+ continue
29+ }
30+ vs[i] = [2]float64{math.Cos(a) * r, math.Sin(a) * r}
31+ }
32+ return vs
33+}
34+
35+// A world offset in the frame of something centred at (x, y) and turned by h: the
36+// inverse of hullToWorld, and what every outline test starts with.
37+func inward(x, y, h, px, py float64) (float64, float64) {
38+ c, s := math.Cos(h), math.Sin(h)
39+ dx, dy := px-x, py-y
40+ return dx*c + dy*s, -dx*s + dy*c
41+}
42+
43+// Is the point inside the piece centred at (x, y) and turned by h?
44+func pieceAt(kind string, x, y, h, px, py float64) bool {
45+ vs := outline[kind]
46+ lx, ly := inward(x, y, h, px, py)
47+ hit := false
48+ for i, a := range vs {
49+ b := vs[(i+1)%len(vs)]
50+ if (a[1] > ly) != (b[1] > ly) && lx < (b[0]-a[0])*(ly-a[1])/(b[1]-a[1])+a[0] {
51+ hit = !hit
52+ }
53+ }
54+ return hit
55+}
56+
57+// Does the segment swept from (x0, y0) to (x1, y1) this tick cross the piece? A
58+// round is checked as the line it flew, so a fast one cannot tunnel through a
59+// piece between two ticks, and the outline decides what it hit.
60+func pieceCross(kind string, x, y, h, x0, y0, x1, y1 float64) bool {
61+ if pieceAt(kind, x, y, h, x0, y0) || pieceAt(kind, x, y, h, x1, y1) {
62+ return true
63+ }
64+ vs := outline[kind]
65+ ax, ay := inward(x, y, h, x0, y0)
66+ bx, by := inward(x, y, h, x1, y1)
67+ for i, a := range vs {
68+ if b := vs[(i+1)%len(vs)]; crosses(ax, ay, bx, by, a[0], a[1], b[0], b[1]) {
69+ return true
70+ }
71+ }
72+ return false
73+}
74+
75+// Do two segments cross? Touching counts: a round that only grazes an edge of a
76+// piece has hit the piece.
77+func crosses(ax, ay, bx, by, cx, cy, dx, dy float64) bool {
78+ return ((bx-ax)*(cy-ay)-(by-ay)*(cx-ax))*((bx-ax)*(dy-ay)-(by-ay)*(dx-ax)) <= 0 &&
79+ ((dx-cx)*(ay-cy)-(dy-cy)*(ax-cx))*((dx-cx)*(by-cy)-(dy-cy)*(bx-cx)) <= 0
80+}
@@ -8,9 +8,10 @@ import (
88 // A click is resolved by the server: grab a piece, tear one off, engage a ship,
99 // park on a body, or fly to a point. The client never decides which happened.
1010 func (h *hub) pick(p *player, x, y float64) {
11- // A piece is picked up by hand, so the hull has to be at it: a click on
11+ // A piece is picked up by hand: the click lands on its outline, and the piece is
12+ // within arm's reach of the hull. Nothing else is grabbable, so a click on
1213 // wreckage across the system is only a move order.
13- if l := h.freeLooseAt(x, y); l != nil && math.Hypot(l.X-p.X, l.Y-p.Y) < grabR {
14+ if l := h.looseAt(x, y); l != nil && math.Hypot(l.X-p.X, l.Y-p.Y) < handR {
1415 p.held, l.held = l.ID, p.ID
1516 return
1617 }
@@ -24,8 +25,11 @@ func (h *hub) pick(p *player, x, y float64) {
2425 }
2526 }
2627 for _, o := range h.players() {
27- if o != p && hullPieceAt(o, x, y) != 0 {
28- h.engage(p, o)
28+ if o == p {
29+ continue
30+ }
31+ if id := hullPieceAt(o, x, y); id != 0 {
32+ h.engage(p, o, id)
2933 return
3034 }
3135 }
@@ -38,15 +42,17 @@ func (h *hub) pick(p *player, x, y float64) {
3842 p.TX, p.TY = x, y
3943 }
4044
41-// Clicking a ship is a declaration, not a shot: both hulls become hostile and the
42-// attacker closes to gun range. Nobody is hostile until someone says so.
43-func (h *hub) engage(p, o *player) {
45+// Clicking a ship's piece is a declaration, not a shot: both hulls become hostile,
46+// the attacker closes to gun range, and its guns are pointed at the piece that was
47+// clicked. Nobody is hostile until someone says so.
48+func (h *hub) engage(p, o *player, piece int) {
4449 for _, q := range h.players() {
4550 if q.Foe == p.ID || q.Foe == o.ID {
46- q.Foe = 0 // an old pairing ends when a new one starts
51+ q.Foe, q.Target = 0, 0 // an old pairing ends when a new one starts
4752 }
4853 }
4954 p.Foe, o.Foe, p.chase, p.orbit = o.ID, p.ID, o.ID, 0
55+ p.Target = piece
5056 }
5157
5258 func (h *hub) drag(p *player, x, y float64) {
@@ -39,11 +39,11 @@ func (h *hub) player(id int) *player {
3939 }
4040
4141 // A dead or departed ship takes its grudges with it: nobody stays hostile with a
42-// ship that is no longer there.
42+// ship that is no longer there, and no guns stay pointed at a piece that is gone.
4343 func (h *hub) forgive(id int) {
4444 for _, p := range h.players() {
4545 if p.Foe == id {
46- p.Foe = 0
46+ p.Foe, p.Target = 0, 0
4747 }
4848 if p.chase == id {
4949 p.chase = 0
@@ -69,18 +69,16 @@ func (h *hub) looseByID(id int) *loose {
6969 return nil
7070 }
7171
72-func (h *hub) freeLooseAt(x, y float64) *loose {
73- var got *loose
74- best := grabR
72+// The loose piece under a point, if nobody already has it in hand. Its own outline
73+// decides, exactly as it is drawn, so a click lands on the piece and never on the
74+// space around it.
75+func (h *hub) looseAt(x, y float64) *loose {
7576 for _, l := range h.loose {
76- if l.held != 0 {
77- continue
78- }
79- if d := math.Hypot(l.X-x, l.Y-y); d < best {
80- best, got = d, l
77+ if l.held == 0 && pieceAt(l.Kind, l.X, l.Y, l.H, x, y) {
78+ return l
8179 }
8280 }
83- return got
81+ return nil
8482 }
8583
8684 func (h *hub) oreByID(id int) *ore {
@@ -15,9 +15,11 @@ const (
1515 standoff = 26.0 // ring radius beyond a planet's surface
1616 starGap = 34.0 // a star kills on contact, so its ring sits further out
1717 hitR = 1.5 // click radius of a body, as a multiple of its radius
18- grabR = 13.0 // world radius to pick a piece up
1918 spacing = 17.0 // world distance between attached piece centers
2019 slots = 6 // attachment directions around a hull piece
20+ // A piece you have just torn off sits one spacing out of the hull, and the outer
21+ // pieces of a big hull sit two, so the hand has to reach that far to reach them.
22+ handR = 2 * spacing
2123 maxSpeed = 90.0
2224 accel = 260.0
2325 arrive = 2.2 // slowdown radius factor
@@ -36,7 +38,7 @@ const (
3638 oreWaitMax = 60.0
3739 oreHolds = 3 // a barren planet holds 3-6 ore, a fixed count per planet
3840
39- pieceR = 11.0 // world radius of a piece, for star heat overlap
41+ pieceR = 11.0 // world radius of a piece: its outline, and its share of a star
4042 burn = 5.0 // hit points a second a piece loses inside a star
4143 dockR = 8.0 // the ship counts as docked inside this of its ring point
4244 towBase = 3 // ore a hull can haul with no circle at all
@@ -46,7 +48,7 @@ const (
4648 yardGap = 1.0 // seconds between services, so a full line drains visibly
4749
4850 fuelPer = 1800.0 // world units of travel one unit of fuel buys
49- refuel = 1.0 // fuel a second a docked shipyard puts back in the tank
51+ refuel = 1.0 // fuel a second a docked shipyard puts back in the tank
5052
5153 aiPerYard = 2 // passive AI ships a shipyard keeps in orbit
5254 aiDelay = 25.0 // seconds after an AI death before that yard births another
@@ -152,6 +154,7 @@ type player struct {
152154 Fuel float64 `json:"fuel"` // what is in the tank, spent by flying
153155 FuelMax float64 `json:"fuelmax"` // capacity, summed from the hull every tick
154156 Foe int `json:"foe"` // ship this one is hostile with, 0 = neutral
157+ Target int `json:"target"` // piece of that ship the guns are pointed at
155158 AI bool `json:"ai"` // a server-run hull, not a connection
156159
157160 home int // shipyard body an AI was born at, and comes back to