-
Notifications
You must be signed in to change notification settings - Fork 2
/
collision.go
254 lines (197 loc) · 7.11 KB
/
collision.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
package cirno
import (
"fmt"
"math"
)
// ResolveCollision assumes types of the given shapes
// and detects if they collide.
func ResolveCollision(one, another Shape, useTags bool) (bool, error) {
if one == nil {
return false, fmt.Errorf("the first shape is nil")
}
if another == nil {
return false, fmt.Errorf("the second shape is nil")
}
shouldCollide, err := one.ShouldCollide(another)
if err != nil {
return false, err
}
if useTags && !shouldCollide {
return false, nil
}
id := one.TypeName() + "_" + another.TypeName()
switch id {
case "Rectangle_Rectangle":
return CollisionRectangleToRectangle(one.(*Rectangle), another.(*Rectangle))
case "Rectangle_Circle":
return CollisionRectangleToCircle(one.(*Rectangle), another.(*Circle))
case "Circle_Rectangle":
return CollisionRectangleToCircle(another.(*Rectangle), one.(*Circle))
case "Circle_Circle":
return CollisionCircleToCircle(one.(*Circle), another.(*Circle))
case "Line_Line":
return IntersectionLineToLine(one.(*Line), another.(*Line))
case "Line_Circle":
return IntersectionLineToCircle(one.(*Line), another.(*Circle))
case "Circle_Line":
return IntersectionLineToCircle(another.(*Line), one.(*Circle))
case "Line_Rectangle":
return IntersectionLineToRectangle(one.(*Line), another.(*Rectangle))
case "Rectangle_Line":
return IntersectionLineToRectangle(another.(*Line), one.(*Rectangle))
}
return false, fmt.Errorf(
"unknown shape type combination: '%s' and '%s'",
one.TypeName(), another.TypeName())
}
// CollisionRectangleToRectangle detects if there is an intersection
// between two oriented rectangles.
func CollisionRectangleToRectangle(a, b *Rectangle) (bool, error) {
if a == nil {
return false, fmt.Errorf("the first rectangle is nil")
}
if b == nil {
return false, fmt.Errorf("the second rectangle is nil")
}
// A vector from the center of rectangle A to the center of rectangle B.
t := b.center.Subtract(a.center)
// Check if Ax is parallel to the separating axis and hence the separating axis exists.
sepAx := math.Abs(Dot(t, a.xAxis)) > a.extents.X+
math.Abs(Dot(b.xAxis.MultiplyByScalar(b.extents.X), a.xAxis))+
math.Abs(Dot(b.yAxis.MultiplyByScalar(b.extents.Y), a.xAxis))
// Check if Ay is parallel to the separating axis and hence the separating axis exists.
sepAy := math.Abs(Dot(t, a.yAxis)) > a.extents.Y+
math.Abs(Dot(b.xAxis.MultiplyByScalar(b.extents.X), a.yAxis))+
math.Abs(Dot(b.yAxis.MultiplyByScalar(b.extents.Y), a.yAxis))
// Check if Bx is parallel to the separating axis and hence the separating axis exists.
sepBx := math.Abs(Dot(t, b.xAxis)) > b.extents.X+
math.Abs(Dot(a.xAxis.MultiplyByScalar(a.extents.X), b.xAxis))+
math.Abs(Dot(a.yAxis.MultiplyByScalar(a.extents.Y), b.xAxis))
// Check if By is parallel to the separating axis and hence the separating axis exists.
sepBy := math.Abs(Dot(t, b.yAxis)) > b.extents.Y+
math.Abs(Dot(a.xAxis.MultiplyByScalar(a.extents.X), b.yAxis))+
math.Abs(Dot(a.yAxis.MultiplyByScalar(a.extents.Y), b.yAxis))
// If the separating axis exists.
if sepAx || sepAy || sepBx || sepBy {
return false, nil
}
// If the separating axis doesn't exist, the rectangles intersect.
return true, nil
}
// CollisionRectangleToCircle detects if there's an intersection between
// an oriented rectangle and a circle.
func CollisionRectangleToCircle(rect *Rectangle, circle *Circle) (bool, error) {
if rect == nil {
return false, fmt.Errorf("the rectangle is nil")
}
if circle == nil {
return false, fmt.Errorf("the circle is nil")
}
// Transform the circle center coordinates from the world space
// to the rectangle's local space.
t := circle.center.Subtract(rect.center)
theta := -rect.angle
t = t.Rotate(theta)
localCircle := &Circle{
center: t,
radius: circle.radius,
}
localRect := &aabb{
min: NewVector(-rect.extents.X, -rect.extents.Y),
max: NewVector(rect.extents.X, rect.extents.Y),
}
return localRect.collidesCircle(localCircle)
}
// CollisionCircleToCircle detects if there's an intersection
// between two circles.
func CollisionCircleToCircle(a, b *Circle) (bool, error) {
if a == nil {
return false, fmt.Errorf("the first circle is nil")
}
if b == nil {
return false, fmt.Errorf("the second circle is nil")
}
t := a.center.Subtract(b.center)
radiiSum := a.radius + b.radius
return t.SquaredMagnitude() <= radiiSum*radiiSum, nil
}
// IntersectionLineToLine detects if two lines
// intersect.
func IntersectionLineToLine(a, b *Line) (bool, error) {
if a == nil {
return false, fmt.Errorf("the first line is nil")
}
if b == nil {
return false, fmt.Errorf("the second line is nil")
}
aMin, aMax := a.GetBoundingBox()
bMin, bMax := b.GetBoundingBox()
// If one line fully contains another.
if a.ContainsPoint(b.p) && a.ContainsPoint(b.q) ||
b.ContainsPoint(a.p) && b.ContainsPoint(a.q) {
return true, nil
}
aBox := &aabb{min: aMin, max: aMax}
bBox := &aabb{min: bMin, max: bMax}
overlap, err := aBox.collidesAABB(bBox)
if err != nil {
return false, err
}
aTouchesB, err := a.touchesOrCrosses(b)
bTouchesA, err := b.touchesOrCrosses(a)
return overlap &&
aTouchesB && bTouchesA, nil
}
// IntersectionLineToCircle detects if a line and a circle do intersect.
func IntersectionLineToCircle(line *Line, circle *Circle) (bool, error) {
if line == nil {
return false, fmt.Errorf("the line is nil")
}
if circle == nil {
return false, fmt.Errorf("the circle is nil")
}
if circle.ContainsPoint(line.p) || circle.ContainsPoint(line.q) {
return true, nil
}
pq := line.q.Subtract(line.p)
t := Dot(circle.center.Subtract(line.p), pq) / Dot(pq, pq)
if t < 0.0 || t > 1.0 {
return false, nil
}
closestPoint := line.p.Add(pq.MultiplyByScalar(t))
return SquaredDistance(circle.center, closestPoint) <= circle.radius*circle.radius, nil
}
// IntersectionLineToRectangle detects if there's an intersection between
// a line and a rectangle.
func IntersectionLineToRectangle(line *Line, rect *Rectangle) (bool, error) {
if line == nil {
return false, fmt.Errorf("the line is nil")
}
if rect == nil {
return false, fmt.Errorf("the rectangle is nil")
}
// The method for two rectangles is as well appropriate
// for line and rectangle because line segment is just
// a rectangle with no Y extent.
lineAxisX, err := line.q.Subtract(line.Center()).Normalize()
if err != nil {
return false, err
}
lineAxisY := lineAxisX.Rotate(90)
lineExtent := line.Length() / 2
t := line.Center().Subtract(rect.center)
sepAx := math.Abs(Dot(t, rect.xAxis)) > rect.extents.X+
math.Abs(Dot(lineAxisX.MultiplyByScalar(lineExtent), rect.xAxis))
sepAy := math.Abs(Dot(t, rect.yAxis)) > rect.extents.Y+
math.Abs(Dot(lineAxisX.MultiplyByScalar(lineExtent), rect.yAxis))
sepLineX := math.Abs(Dot(t, lineAxisX)) > lineExtent+
math.Abs(Dot(rect.xAxis.MultiplyByScalar(rect.extents.X), lineAxisX))+
math.Abs(Dot(rect.yAxis.MultiplyByScalar(rect.extents.Y), lineAxisX))
sepLineY := math.Abs(Dot(t, lineAxisY)) >
math.Abs(Dot(rect.xAxis.MultiplyByScalar(rect.extents.X), lineAxisY))+
math.Abs(Dot(rect.yAxis.MultiplyByScalar(rect.extents.Y), lineAxisY))
if sepAx || sepAy || sepLineX || sepLineY {
return false, nil
}
return true, nil
}