-
Notifications
You must be signed in to change notification settings - Fork 42
/
shape.go
479 lines (348 loc) · 12.3 KB
/
shape.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
package resolv
import (
"math"
"sort"
)
// IShape represents an interface that all Shapes fulfill.
type IShape interface {
ID() uint32 // The unique ID of the Shape
Clone() IShape
Tags() *Tags
Position() Vector
SetPosition(x, y float64)
SetPositionVec(vec Vector)
Move(x, y float64)
MoveVec(vec Vector)
SetX(float64)
SetY(float64)
SelectTouchingCells(margin int) CellSelection
update()
SetData(data any)
Data() any
setSpace(space *Space)
Space() *Space
addToTouchingCells()
removeFromTouchingCells()
Bounds() Bounds
IsLeftOf(other IShape) bool
IsRightOf(other IShape) bool
IsAbove(other IShape) bool
IsBelow(other IShape) bool
IntersectionTest(settings IntersectionTestSettings) bool
IsIntersecting(other IShape) bool
Intersection(other IShape) IntersectionSet
VecTo(other IShape) Vector
DistanceTo(other IShape) float64
DistanceSquaredTo(other IShape) float64
}
// ShapeBase implements many of the common methods that Shapes need to implement to fulfill IShape
// (but not the Shape-specific ones, like rotating for ConvexPolygons or setting the radius for Circles).
type ShapeBase struct {
position Vector
space *Space
touchingCells []*Cell
tags *Tags
data any // Data represents some helper data present on the shape.
owner IShape // The owning shape; this allows ShapeBase to call overridden functions (i.e. owner.Bounds()).
id uint32
}
var globalShapeID = uint32(0)
func newShapeBase(x, y float64) ShapeBase {
t := Tags(0)
id := globalShapeID
globalShapeID++
return ShapeBase{
position: NewVector(x, y),
tags: &t,
id: id,
}
}
// ID returns the unique ID of the Shape.
func (s *ShapeBase) ID() uint32 {
return s.id
}
// Data returns any auxiliary data set on the Circle shape.
func (s *ShapeBase) Data() any {
return s.data
}
// SetData sets any auxiliary data on the Circle shape.
func (s *ShapeBase) SetData(data any) {
s.data = data
}
// Tags returns the tags applied to the shape.
func (s *ShapeBase) Tags() *Tags {
return s.tags
}
// Move translates the Circle by the designated X and Y values.
func (s *ShapeBase) Move(x, y float64) {
s.position.X += x
s.position.Y += y
s.update()
}
// MoveVec translates the ShapeBase by the designated Vector.
func (s *ShapeBase) MoveVec(vec Vector) {
s.Move(vec.X, vec.Y)
}
// Position() returns the X and Y position of the ShapeBase.
func (s *ShapeBase) Position() Vector {
return s.position
}
// SetPosition sets the center position of the ShapeBase using the X and Y values given.
func (s *ShapeBase) SetPosition(x, y float64) {
s.position.X = x
s.position.Y = y
s.update()
}
// SetPosition sets the center position of the ShapeBase using the Vector given.
func (c *ShapeBase) SetPositionVec(vec Vector) {
c.SetPosition(vec.X, vec.Y)
}
// SetX sets the X position of the Shape.
func (c *ShapeBase) SetX(x float64) {
pos := c.position
pos.X = x
c.SetPosition(pos.X, pos.Y)
}
// SetY sets the Y position of the Shape.
func (c *ShapeBase) SetY(y float64) {
pos := c.position
pos.Y = y
c.SetPosition(pos.X, pos.Y)
}
func (s *ShapeBase) Space() *Space {
return s.space
}
func (s *ShapeBase) setSpace(space *Space) {
s.space = space
}
// IsLeftOf returns true if the Shape is to the left of the other shape.
func (s *ShapeBase) IsLeftOf(other IShape) bool {
return s.owner.Bounds().Min.X < other.Bounds().Min.X
}
// IsRightOf returns true if the Shape is to the right of the other shape.
func (s *ShapeBase) IsRightOf(other IShape) bool {
return s.owner.Bounds().Max.X > other.Bounds().Max.X
}
// IsAbove returns true if the Shape is above the other shape.
func (s *ShapeBase) IsAbove(other IShape) bool {
return s.owner.Bounds().Min.Y < other.Bounds().Min.Y
}
// IsBelow returns true if the Shape is below the other shape.
func (s *ShapeBase) IsBelow(other IShape) bool {
return s.owner.Bounds().Max.Y > other.Bounds().Max.Y
}
// VecTo returns a vector from the given shape to the other Shape.
func (s *ShapeBase) VecTo(other IShape) Vector {
return s.position.Sub(other.Position())
}
// DistanceSquaredTo returns the distance from the given shape's center to the other Shape.
func (s *ShapeBase) DistanceTo(other IShape) float64 {
return s.owner.Position().Distance(other.Position())
}
// DistanceSquaredTo returns the squared distance from the given shape's center to the other Shape.
func (s *ShapeBase) DistanceSquaredTo(other IShape) float64 {
return s.owner.Position().DistanceSquared(other.Position())
}
func (s *ShapeBase) removeFromTouchingCells() {
for _, cell := range s.touchingCells {
cell.unregister(s.owner)
}
s.touchingCells = s.touchingCells[:0]
}
func (s *ShapeBase) addToTouchingCells() {
if s.space != nil {
cx, cy, ex, ey := s.owner.Bounds().toCellSpace()
for y := cy; y <= ey; y++ {
for x := cx; x <= ex; x++ {
cell := s.space.Cell(x, y)
if cell != nil {
cell.register(s.owner)
s.touchingCells = append(s.touchingCells, cell)
}
}
}
}
}
// SelectTouchingCells returns a CellSelection of the cells in the Space that the Shape is touching.
// margin sets the cellular margin - the higher the margin, the further away candidate Shapes can be to be considered for
// collision. A margin of 1 is a good default. To help visualize which cells contain Shapes, it would be good to implement some kind of debug
// drawing in your game, like can be seen in resolv's examples.
func (s *ShapeBase) SelectTouchingCells(margin int) CellSelection {
cx, cy, ex, ey := s.owner.Bounds().toCellSpace()
cx -= margin
cy -= margin
ex += margin
ey += margin
return CellSelection{
StartX: cx,
StartY: cy,
EndX: ex,
EndY: ey,
space: s.space,
excludeSelf: s.owner,
}
}
func (s *ShapeBase) update() {
s.removeFromTouchingCells()
s.addToTouchingCells()
}
// IsIntersecting returns if the shape is intersecting with the other given Shape.
func (s *ShapeBase) IsIntersecting(other IShape) bool {
return !s.owner.Intersection(other).IsEmpty()
}
// IntersectionTestSettings is a struct that contains settings to control intersection tests.
type IntersectionTestSettings struct {
TestAgainst ShapeIterator // The collection of shapes to test against
// OnIntersect is a callback to be called for each intersection found between the calling Shape and any of the other shapes given in TestAgainst.
// The callback should be called in order of distance to the testing object.
// Moving the object can influence whether it intersects with future surrounding objects.
// set is the intersection set that contains information about the intersection.
// The boolean the callback returns indicates whether the LineTest function should continue testing or stop at the currently found intersection.
OnIntersect func(set IntersectionSet) bool
}
type possibleIntersection struct {
Shape IShape
Distance float64
}
var possibleIntersections []possibleIntersection
// IntersectionTest tests to see if the calling shape intersects with shapes specified in
// the given settings struct, checked in order of distance to the calling shape's center point.
// Internally, the function checks to see what Shapes are nearby, and tests against them in order
// of distance. If the testing Shape moves, then that will influence the result of testing future
// Shapes in the current game frame.
// If the test succeeds in finding at least one intersection, it returns true.
func (s *ShapeBase) IntersectionTest(settings IntersectionTestSettings) bool {
possibleIntersections = possibleIntersections[:0]
settings.TestAgainst.ForEach(func(other IShape) bool {
if other == s.owner {
return true
}
possibleIntersections = append(possibleIntersections, possibleIntersection{
Shape: other,
Distance: other.DistanceSquaredTo(s.owner),
})
return true
})
sort.Slice(possibleIntersections, func(i, j int) bool {
return possibleIntersections[i].Distance < possibleIntersections[j].Distance
})
collided := false
for _, p := range possibleIntersections {
result := s.owner.Intersection(p.Shape)
if !result.IsEmpty() {
collided = true
if settings.OnIntersect != nil {
if !settings.OnIntersect(result) {
break
}
} else {
break
}
}
}
return collided
}
func circleConvexTest(circle *Circle, convex *ConvexPolygon) IntersectionSet {
intersectionSet := IntersectionSet{}
if !convex.owner.Bounds().IsIntersecting(circle.Bounds()) {
return intersectionSet
}
for _, line := range convex.Lines() {
if res := line.IntersectionPointsCircle(circle); len(res) > 0 {
for _, point := range res {
intersectionSet.Intersections = append(intersectionSet.Intersections, Intersection{
Point: point,
Normal: line.Normal(),
})
}
}
}
if !intersectionSet.IsEmpty() {
intersectionSet.OtherShape = convex
// No point in sorting circle -> convex intersection tests because the circle's center is necessarily equidistant from any and all points of intersection
if mtv, ok := convex.calculateMTV(circle); ok {
intersectionSet.MTV = mtv.Invert()
}
}
return intersectionSet
}
func convexCircleTest(convex *ConvexPolygon, circle *Circle) IntersectionSet {
intersectionSet := IntersectionSet{}
if !convex.owner.Bounds().IsIntersecting(circle.Bounds()) {
return intersectionSet
}
for _, line := range convex.Lines() {
res := line.IntersectionPointsCircle(circle)
if len(res) > 0 {
for _, point := range res {
intersectionSet.Intersections = append(intersectionSet.Intersections, Intersection{
Point: point,
Normal: point.Sub(circle.position).Unit(),
})
}
}
}
if !intersectionSet.IsEmpty() {
intersectionSet.OtherShape = circle
sort.Slice(intersectionSet.Intersections, func(i, j int) bool {
return intersectionSet.Intersections[i].Point.DistanceSquared(circle.position) < intersectionSet.Intersections[j].Point.DistanceSquared(circle.position)
})
if mtv, ok := convex.calculateMTV(circle); ok {
intersectionSet.MTV = mtv
}
}
return intersectionSet
}
func circleCircleTest(circleA, circleB *Circle) IntersectionSet {
intersectionSet := IntersectionSet{}
if !circleA.owner.Bounds().IsIntersecting(circleB.Bounds()) {
return intersectionSet
}
d := math.Sqrt(math.Pow(circleB.position.X-circleA.position.X, 2) + math.Pow(circleB.position.Y-circleA.position.Y, 2))
if d > circleA.radius+circleB.radius || d < math.Abs(circleA.radius-circleB.radius) || d == 0 {
return intersectionSet
}
a := (math.Pow(circleA.radius, 2) - math.Pow(circleB.radius, 2) + math.Pow(d, 2)) / (2 * d)
h := math.Sqrt(math.Pow(circleA.radius, 2) - math.Pow(a, 2))
x2 := circleA.position.X + a*(circleB.position.X-circleA.position.X)/d
y2 := circleA.position.Y + a*(circleB.position.Y-circleA.position.Y)/d
intersectionSet.Intersections = []Intersection{
{Point: Vector{x2 + h*(circleB.position.Y-circleA.position.Y)/d, y2 - h*(circleB.position.X-circleA.position.X)/d}},
{Point: Vector{x2 - h*(circleB.position.Y-circleA.position.Y)/d, y2 + h*(circleB.position.X-circleA.position.X)/d}},
}
for i := range intersectionSet.Intersections {
intersectionSet.Intersections[i].Normal = intersectionSet.Intersections[i].Point.Sub(circleA.position).Unit()
}
intersectionSet.MTV = Vector{circleA.position.X - circleB.position.X, circleA.position.Y - circleB.position.Y}
dist := intersectionSet.MTV.Magnitude()
intersectionSet.MTV = intersectionSet.MTV.Unit().Scale(circleA.radius + circleB.radius - dist)
intersectionSet.OtherShape = circleA
return intersectionSet
}
func convexConvexTest(convexA, convexB *ConvexPolygon) IntersectionSet {
intersectionSet := IntersectionSet{}
if !convexA.owner.Bounds().IsIntersecting(convexB.Bounds()) {
return intersectionSet
}
for _, otherLine := range convexB.Lines() {
for _, line := range convexA.Lines() {
if point, ok := line.IntersectionPointsLine(otherLine); ok {
intersectionSet.Intersections = append(intersectionSet.Intersections, Intersection{
Point: point,
Normal: otherLine.Normal(),
})
}
}
}
if !intersectionSet.IsEmpty() {
intersectionSet.OtherShape = convexB
center := convexA.Center()
sort.Slice(intersectionSet.Intersections, func(i, j int) bool {
return intersectionSet.Intersections[i].Point.DistanceSquared(center) < intersectionSet.Intersections[j].Point.DistanceSquared(center)
})
if mtv, ok := convexA.calculateMTV(convexB); ok {
intersectionSet.MTV = mtv
}
}
return intersectionSet
}