Fixed collision detection in probes
This was a problem before when the probe hit an object that was further than another object first. Now we traverse all objects all the time to find the closest one. \o/
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
v "bitbucket.org/hackerbots/vector"
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCollision(t *testing.T) {
|
||||
tests := []struct {
|
||||
g game
|
||||
r Robot
|
||||
target v.Vector2d
|
||||
location *v.Point2d
|
||||
collision bool
|
||||
}{
|
||||
// should intersect with first box
|
||||
{
|
||||
g: game{
|
||||
width: 800,
|
||||
height: 400,
|
||||
obstacles: []Obstacle{
|
||||
Obstacle{
|
||||
Bounds: v.AABB2d{
|
||||
v.Point2d{200, 100},
|
||||
v.Point2d{300, 200},
|
||||
},
|
||||
},
|
||||
Obstacle{
|
||||
Bounds: v.AABB2d{
|
||||
v.Point2d{400, 200},
|
||||
v.Point2d{600, 300},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
r: Robot{
|
||||
Position: v.Point2d{100, 100},
|
||||
// Heading: v.Vector2d{1, 1},
|
||||
},
|
||||
target: v.Vector2d{900, 350},
|
||||
location: &v.Point2d{200, 138.88889},
|
||||
collision: true,
|
||||
},
|
||||
// shouldn't intersect at all
|
||||
{
|
||||
g: game{
|
||||
width: 800,
|
||||
height: 400,
|
||||
obstacles: []Obstacle{
|
||||
Obstacle{
|
||||
Bounds: v.AABB2d{
|
||||
v.Point2d{200, 100},
|
||||
v.Point2d{300, 200},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
r: Robot{
|
||||
Position: v.Point2d{100, 100},
|
||||
},
|
||||
target: v.Vector2d{0, 0},
|
||||
collision: false,
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
collision, location, _ := test.r.checkCollisions(&test.g, test.target)
|
||||
if collision != test.collision {
|
||||
t.Errorf("expected: %t, actual: %t", test.collision, collision)
|
||||
}
|
||||
if test.location == nil {
|
||||
if location != nil {
|
||||
t.Errorf("expected: %+v, actual: %+v", test.location, location)
|
||||
}
|
||||
}
|
||||
if test.location != nil {
|
||||
if location == nil {
|
||||
t.Errorf("expected: %+v, actual: %+v", test.location, location)
|
||||
} else {
|
||||
if math.Abs(float64(location.X-test.location.X)) > 1e-4 || math.Abs(float64(location.Y-test.location.Y)) > 1e-4 {
|
||||
t.Errorf("expected: %+v, actual: %+v", test.location, location)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// XXX
|
||||
func TestBotBotCollisions(t *testing.T) {
|
||||
t.Skip("NYI")
|
||||
}
|
||||
Reference in New Issue
Block a user