forked from Tarliton/collision2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollision_test.go
208 lines (190 loc) · 7.22 KB
/
collision_test.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
package collision2d_test
import (
"github.com/Tarliton/collision2d"
"github.com/stretchr/testify/assert"
"math"
"testing"
)
func TestCollisionPointInCircle(t *testing.T) {
point := collision2d.NewVector(11, 11)
circle := collision2d.NewCircle(collision2d.NewVector(10, 10), 5)
result := collision2d.PointInCircle(point, circle)
assert.Equal(t, true, result, "they should be equal")
}
func TestCollisionPointNotInCircle(t *testing.T) {
point := collision2d.NewVector(155, 11)
circle := collision2d.NewCircle(collision2d.NewVector(10, 10), 5)
result := collision2d.PointInCircle(point, circle)
assert.Equal(t, false, result, "they should be equal")
}
func TestCollisionPointInPolygon(t *testing.T) {
point := collision2d.NewVector(35, 5)
polygonCorners := [...]float64{
0, 0,
30, 0,
0, 30,
}
polygon := collision2d.NewPolygon(collision2d.NewVector(30, 0), collision2d.NewVector(0, 0), 0, polygonCorners[:])
result := collision2d.PointInPolygon(point, polygon)
assert.Equal(t, true, result, "they should be equal")
}
func TestCollisionPointNotInPolygon(t *testing.T) {
point := collision2d.NewVector(0, 0)
polygonCorners := [...]float64{
0, 0,
30, 0,
0, 30,
}
polygon := collision2d.NewPolygon(collision2d.NewVector(30, 0), collision2d.NewVector(0, 0), 0, polygonCorners[:])
result := collision2d.PointInPolygon(point, polygon)
assert.Equal(t, false, result, "they should be equal")
}
func TestCollisionTestCircleCircle(t *testing.T) {
circle1 := collision2d.NewCircle(collision2d.NewVector(0, 0), 20)
circle2 := collision2d.NewCircle(collision2d.NewVector(30, 0), 20)
result, response := collision2d.TestCircleCircle(circle1, circle2)
assert.Equal(t, true, result, "they should be equal")
assert.Equal(t, float64(10), response.Overlap, "they should be equal")
assert.Equal(t, true, response.OverlapV.X == float64(10) && response.OverlapV.Y == float64(0), "they should be equal")
}
func TestCollisionTestNotCircleCircle(t *testing.T) {
circle1 := collision2d.NewCircle(collision2d.NewVector(0, 0), 20)
circle2 := collision2d.NewCircle(collision2d.NewVector(30, 50), 20)
result, response := collision2d.TestCircleCircle(circle1, circle2)
assert.Equal(t, false, result, "they should be equal")
assert.Equal(t, -math.MaxFloat64, response.Overlap, "they should be equal")
assert.Equal(t, float64(0), response.OverlapV.X, "they should be equal")
assert.Equal(t, float64(0), response.OverlapV.Y, "they should be equal")
}
func TestCollisionTestPolygonCircle(t *testing.T) {
polygonCorners := [...]float64{
0, 0,
40, 0,
40, 40,
0, 40,
}
polygon := collision2d.NewPolygon(collision2d.NewVector(0, 0), collision2d.NewVector(0, 0), 0, polygonCorners[:])
circle := collision2d.NewCircle(collision2d.NewVector(50, 50), 20)
result, response := collision2d.TestPolygonCircle(polygon, circle)
assert.Equal(t, true, result, "they should be equal")
assert.Equal(t, float64(5.857864376269049), response.Overlap, "they should be equal")
assert.Equal(t, float64(4.14213562373095), response.OverlapV.X, "they should be equal")
assert.Equal(t, float64(4.14213562373095), response.OverlapV.Y, "they should be equal")
}
func TestCollisionTestNotPolygonCircle(t *testing.T) {
polygonCorners := [...]float64{
0, 0,
40, 0,
40, 40,
0, 40,
}
polygon := collision2d.NewPolygon(collision2d.NewVector(0, 0), collision2d.NewVector(0, 0), 0, polygonCorners[:])
circle := collision2d.NewCircle(collision2d.NewVector(200, 200), 1)
result, response := collision2d.TestPolygonCircle(polygon, circle)
assert.Equal(t, false, result, "they should be equal")
assert.Equal(t, -math.MaxFloat64, response.Overlap, "they should be equal")
assert.Equal(t, float64(0), response.OverlapV.X, "they should be equal")
assert.Equal(t, float64(0), response.OverlapV.Y, "they should be equal")
}
func TestCollisionTestCirclePolygon(t *testing.T) {
polygonCorners := [...]float64{
0, 0,
40, 0,
40, 40,
0, 40,
}
polygon := collision2d.NewPolygon(collision2d.NewVector(0, 0), collision2d.NewVector(0, 0), 0, polygonCorners[:])
circle := collision2d.NewCircle(collision2d.NewVector(50, 50), 20)
result, response := collision2d.TestCirclePolygon(circle, polygon)
assert.Equal(t, true, result, "they should be equal")
assert.Equal(t, float64(5.857864376269049), response.Overlap, "they should be equal")
assert.Equal(t, float64(-4.14213562373095), response.OverlapV.X, "they should be equal")
assert.Equal(t, float64(-4.14213562373095), response.OverlapV.Y, "they should be equal")
}
func TestCollisionTestPolygonPolygon(t *testing.T) {
polygonCorners := [...]float64{
0, 0,
40, 0,
40, 40,
0, 40,
}
polygon := collision2d.NewPolygon(collision2d.NewVector(0, 0), collision2d.NewVector(0, 0), 0, polygonCorners[:])
result, response := collision2d.TestPolygonPolygon(polygon, polygon)
assert.Equal(t, true, result, "they should be equal")
assert.Equal(t, float64(40), response.Overlap, "they should be equal")
assert.Equal(t, float64(0), response.OverlapV.X, "they should be equal")
assert.Equal(t, float64(40), response.OverlapV.Y, "they should be equal")
}
var response collision2d.Response
func BenchmarkCollisionPointInCircle(b *testing.B) {
circle := collision2d.NewCircle(collision2d.NewVector(0, 0), 20)
point := collision2d.NewVector(1, 2)
for i := 0; i < b.N; i++ {
collision2d.PointInCircle(point, circle)
}
}
func BenchmarkCollisionPointInPolygon(b *testing.B) {
polygonCorners := [...]float64{
0, 0,
40, 0,
40, 40,
0, 40,
}
polygon := collision2d.NewPolygon(collision2d.NewVector(0, 0), collision2d.NewVector(0, 0), 0, polygonCorners[:])
point := collision2d.NewVector(1, 2)
for i := 0; i < b.N; i++ {
collision2d.PointInPolygon(point, polygon)
}
}
func BenchmarkCollisionTestCircleCircle(b *testing.B) {
circle1 := collision2d.NewCircle(collision2d.NewVector(0, 0), 20)
circle2 := collision2d.NewCircle(collision2d.NewVector(30, 0), 20)
for i := 0; i < b.N; i++ {
_, response = collision2d.TestCircleCircle(circle1, circle2)
}
}
func BenchmarkCollisionTestPolygonCircle(b *testing.B) {
circle := collision2d.NewCircle(collision2d.NewVector(0, 0), 20)
polygonCorners := [...]float64{
0, 0,
40, 0,
40, 40,
0, 40,
}
polygon := collision2d.NewPolygon(collision2d.NewVector(0, 0), collision2d.NewVector(0, 0), 0, polygonCorners[:])
for i := 0; i < b.N; i++ {
_, response = collision2d.TestPolygonCircle(polygon, circle)
}
}
func BenchmarkCollisionTestCirclePolygon(b *testing.B) {
circle := collision2d.NewCircle(collision2d.NewVector(0, 0), 20)
polygonCorners := [...]float64{
0, 0,
40, 0,
40, 40,
0, 40,
}
polygon := collision2d.NewPolygon(collision2d.NewVector(0, 0), collision2d.NewVector(0, 0), 0, polygonCorners[:])
for i := 0; i < b.N; i++ {
_, response = collision2d.TestCirclePolygon(circle, polygon)
}
}
func BenchmarkCollisionTestPolygonPolygon(b *testing.B) {
polygonCorners1 := [...]float64{
0, 0,
40, 0,
40, 40,
0, 40,
}
polygon1 := collision2d.NewPolygon(collision2d.NewVector(0, 0), collision2d.NewVector(0, 0), 0, polygonCorners1[:])
polygonCorners2 := [...]float64{
0, 0,
40, 0,
40, 40,
0, 40,
}
polygon2 := collision2d.NewPolygon(collision2d.NewVector(15, 25), collision2d.NewVector(0, 0), 0, polygonCorners2[:])
for i := 0; i < b.N; i++ {
_, response = collision2d.TestPolygonPolygon(polygon1, polygon2)
}
}