-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcollision_test.go
More file actions
143 lines (110 loc) · 4.06 KB
/
collision_test.go
File metadata and controls
143 lines (110 loc) · 4.06 KB
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
package gophysx
import (
"github.com/stretchr/testify/require"
"math/rand"
"testing"
"time"
)
type TestClock struct {
currentTime time.Time
}
func (t *TestClock) Now() time.Time {
return t.currentTime
}
func (t *TestClock) AddTime(d time.Duration) {
t.currentTime = t.currentTime.Add(d)
}
func initSystem() (*System, *TestClock) {
clock := TestClock{time.Now()}
return Init(&clock), &clock
}
func makeTestObject(num_polygons int32) ([]Vector, Vector) {
var polys []Vector
for ; num_polygons > 0; num_polygons-- {
polys = append(polys, Vector{rand.Float64(), rand.Float64()})
}
position := Vector{0, 0}
return polys, position
}
func TestInitCollisionSystem(t *testing.T) {
system, _ := initSystem()
require.NotNil(t, system, "Init returned a nil value.")
require.Empty(t, system.objects, "Collision system should start empty.") // TODO change?
}
func TestGetObjectEmpty(t *testing.T) {
system, _ := initSystem()
_, err := system.GetObject(0)
require.NotNil(t, err, "Getting a non existant object should be nil")
}
func TestObjectBasics(t *testing.T) {
system, _ := initSystem()
polys, position := makeTestObject(4)
obj, err := system.AddObject(polys, position)
require.Nil(t, err, "Add object failed")
require.NotNil(t, obj, "Didn't return a valid object")
require.Equal(t, position, obj.Position(), "Positions are not the same")
require.Equal(t, Vector{0, 0}, obj.Velocity(), "Objects should start at rest")
obj2, err := system.GetObject(obj.Id())
require.Nil(t, err, "Get object failed")
require.NotNil(t, obj2, "Object should not be nil")
require.Equal(t, *obj, *obj2, "Objects should be equal")
err = obj.Remove()
require.Nil(t, err, "Remove object failed")
_, err = system.GetObject(obj.Id())
require.NotNil(t, err, "Shouldn't be able to see the object anymore")
}
func TestForceBasics(t *testing.T) {
system, _ := initSystem()
polys, position := makeTestObject(4)
obj, _ := system.AddObject(polys, position)
magnitude := 10.0
direction := Vector{1, 1}
f, err := obj.AddForce(magnitude, direction)
require.Equal(t, magnitude, f.Magnitude())
require.Equal(t, direction.Normalize(), f.Direction())
err = f.SetMagnitude(5.0)
require.Nil(t, err, "Updating a force failed")
f, err = obj.GetForce(f.Id()) // The value gets saved
require.Nil(t, err, "Should be allowed to get this force")
require.Equal(t, 5.0, f.Magnitude(), "Magnitude not updated")
err = f.SetDirection(Vector{5.0, 5.0})
require.Nil(t, err, "Updating a force failed")
f, err = obj.GetForce(f.Id()) // The value gets saved
require.Nil(t, err, "Should be allowed to get this force")
require.Equal(t, Vector{5.0, 5.0}.Normalize(), f.Direction())
require.Nil(t, f.Remove(), "Should be allowed to remove a force")
_, err = obj.GetForce(f.Id())
require.NotNil(t, err, "Should no longer be the force on the object")
}
func TestForceOverTime(t *testing.T) {
system, clock := initSystem()
polys, position := makeTestObject(4)
position = Vector{3, 3}
obj, _ := system.AddObject(polys, position)
magnitude := 1.0
direction := Vector{1, 0}
position = obj.Position()
require.Equal(t, Vector{3, 3}, position, "Haven't moved yet")
velocity := obj.Velocity()
require.Equal(t, Vector{}, velocity, "Haven't moved yet")
f, _ := obj.AddForce(magnitude, direction)
clock.AddTime(time.Second)
//fmt.Println("b4 Vel")
newVel := obj.Velocity()
require.Equal(t, Vector{1, 0}, newVel.Sub(velocity), "Velocity is incorrect")
//fmt.Println("b4 Pos")
newPos := obj.Position()
require.Equal(t, Vector{3.5, 3}, newPos, "Position is incorrect")
//fmt.Println("No time passes")
newPos2 := obj.Position()
require.Equal(t, newPos, newPos2, "Position shouldn't change after calling a second time")
newVel2 := obj.Velocity()
require.Equal(t, newVel, newVel2, "Velocity shouldn't change after calling a second time")
f.Remove()
//fmt.Println("Removed force and added time...")
clock.AddTime(time.Second)
newVel = obj.Velocity()
require.Equal(t, newVel2, newVel, "Velocity stays the same when no force")
newPos = obj.Position()
require.Equal(t, Vec(4.5, 3), newPos, "Position stays the same when no force")
}