-
Notifications
You must be signed in to change notification settings - Fork 2
/
queries.go
182 lines (143 loc) · 3.65 KB
/
queries.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
package cirno
import (
"fmt"
"github.com/golang-collections/collections/queue"
)
// Raycast casts a ray in the space and returns the hit shape closest
// to the origin of the ray.
//
// Ray cannot hit against the shape within which it's located.
func (space *Space) Raycast(origin, direction Vector, distance float64, mask int32) (Shape, Vector, error) {
if Sign(direction.X) == 0 && Sign(direction.Y) == 0 {
return nil, Zero(), fmt.Errorf("the direction vector is Zero()")
}
if distance <= 0 {
distance = Distance(space.min, space.max)
}
normDir, err := direction.Normalize()
if err != nil {
return nil, Zero(), err
}
ray, err := NewLine(origin, origin.Add(
normDir.MultiplyByScalar(distance)))
if err != nil {
return nil, Zero(), err
}
ray.SetMask(mask)
nodeQueue := queue.New()
nodeQueue.Enqueue(space.tree.root)
minExists := false
var (
minSquaredDistance float64
hitShape Shape
hit Vector
)
for nodeQueue.Len() > 0 {
node := nodeQueue.Dequeue().(*quadTreeNode)
overlapped, err := node.boundary.collidesLine(ray)
if err != nil {
return nil, Zero(), err
}
if !overlapped {
continue
}
// If the node is a leaf.
if node.northWest == nil {
for shape := range node.shapes {
raycastHit, err := ResolveCollision(ray, shape, space.useTags)
if err != nil {
return nil, Zero(), err
}
if raycastHit && !shape.ContainsPoint(ray.p) {
contacts, err := Contact(ray, shape)
if err != nil {
return nil, Zero(), err
}
for _, contact := range contacts {
sqrDistance := SquaredDistance(ray.p, contact)
if !minExists {
hit = contact
hitShape = shape
minSquaredDistance = sqrDistance
minExists = true
} else if sqrDistance < minSquaredDistance {
hit = contact
hitShape = shape
minSquaredDistance = sqrDistance
}
}
}
}
} else {
nodeQueue.Enqueue(node.northEast)
nodeQueue.Enqueue(node.northWest)
nodeQueue.Enqueue(node.southEast)
nodeQueue.Enqueue(node.southWest)
}
}
return hitShape, hit, nil
}
// Boxcast casts a box in the space and returns all the
// shapes overlapped by this box.
func (space *Space) Boxcast(rect *Rectangle) (Shapes, error) {
if rect == nil {
return nil, fmt.Errorf("the rectangle is nil")
}
nodeQueue := queue.New()
nodeQueue.Enqueue(space.tree.root)
shapes := make(Shapes, 0)
for nodeQueue.Len() > 0 {
node := nodeQueue.Dequeue().(*quadTreeNode)
overlapped, err := node.boundary.collidesRectangle(rect)
if err != nil {
return nil, err
}
if !overlapped {
continue
}
if node.northWest == nil {
for shape := range node.shapes {
boxcastHit, err := ResolveCollision(rect, shape, space.useTags)
if err != nil {
return nil, err
}
if boxcastHit {
shapes.Insert(shape)
}
}
}
}
return shapes, nil
}
// Circlecast casts a circle in the space and returns all the
// shapes overlapped by the circle.
func (space *Space) Circlecast(circle *Circle) (Shapes, error) {
if circle == nil {
return nil, fmt.Errorf("the circle is nil")
}
nodeQueue := queue.New()
nodeQueue.Enqueue(space.tree.root)
shapes := make(Shapes, 0)
for nodeQueue.Len() > 0 {
node := nodeQueue.Dequeue().(*quadTreeNode)
overlapped, err := node.boundary.collidesCircle(circle)
if err != nil {
return nil, err
}
if !overlapped {
continue
}
if node.northWest == nil {
for shape := range node.shapes {
circlecastHit, err := ResolveCollision(circle, shape, space.useTags)
if err != nil {
return nil, err
}
if circlecastHit {
shapes.Insert(shape)
}
}
}
}
return shapes, nil
}