Project projects the shape onto the given vector.
(p engo.Point, sc SpaceComponent)
| 18 | |
| 19 | // Project projects the shape onto the given vector. |
| 20 | func (s Shape) Project(p engo.Point, sc SpaceComponent) (min, max float32) { |
| 21 | s.PolygonEllipse() |
| 22 | sin, cos := math.Sincos(sc.Rotation * math.Pi / 180) |
| 23 | l := engo.Line{ |
| 24 | P1: engo.Point{ |
| 25 | X: sc.Position.X + s.Lines[0].P1.X*cos - s.Lines[0].P1.Y*sin, |
| 26 | Y: sc.Position.Y + s.Lines[0].P1.Y*cos + s.Lines[0].P1.X*sin, |
| 27 | }, |
| 28 | P2: engo.Point{ |
| 29 | X: sc.Position.X + s.Lines[0].P2.X*cos - s.Lines[0].P2.Y*sin, |
| 30 | Y: sc.Position.Y + s.Lines[0].P2.Y*cos + s.Lines[0].P2.X*sin, |
| 31 | }, |
| 32 | } |
| 33 | min = l.P1.X*p.X + l.P1.Y*p.Y |
| 34 | max = min |
| 35 | for _, line := range s.Lines { |
| 36 | l = engo.Line{ |
| 37 | P1: engo.Point{ |
| 38 | X: sc.Position.X + line.P1.X*cos - line.P1.Y*sin, |
| 39 | Y: sc.Position.Y + line.P1.Y*cos + line.P1.X*sin, |
| 40 | }, |
| 41 | P2: engo.Point{ |
| 42 | X: sc.Position.X + line.P2.X*cos - line.P2.Y*sin, |
| 43 | Y: sc.Position.Y + line.P2.Y*cos + line.P2.X*sin, |
| 44 | }, |
| 45 | } |
| 46 | dot := l.P2.X*p.X + l.P2.Y*p.Y |
| 47 | if dot < min { |
| 48 | min = dot |
| 49 | } |
| 50 | if dot > max { |
| 51 | max = dot |
| 52 | } |
| 53 | } |
| 54 | return |
| 55 | } |
| 56 | |
| 57 | // PolygonEllipse approximates the Ellipse as an N-sided polygon, determined by |
| 58 | // the shape's N value. If N is 0, it defaults to 25. |