1package main 2 3import "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. 13var 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. 22func 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. 37func 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? 44func 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. 60func 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. 77func 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}