This commit is contained in:
Stephen McQuay 2014-01-07 22:29:53 -08:00
parent 30aee4f09f
commit c4932922d8
2 changed files with 50 additions and 18 deletions

View File

@ -19,6 +19,7 @@ func RectRectIntersection(r1 AABB2d, r2 AABB2d) bool {
r1.A.Y < r2.B.Y && r1.B.Y > r2.A.Y) r1.A.Y < r2.B.Y && r1.B.Y > r2.A.Y)
} }
// returns collision, inside, point of collision
func RectIntersection(r AABB2d, p Point2d, v Vector2d) (bool, bool, Point2d) { func RectIntersection(r AABB2d, p Point2d, v Vector2d) (bool, bool, Point2d) {
collision := false collision := false
inside := false inside := false

View File

@ -216,26 +216,57 @@ func TestPtInRect(t *testing.T) {
} }
func TestRectIntersect(t *testing.T) { func TestRectIntersect(t *testing.T) {
p1 := Point2d{10, 0} type result struct {
v1 := Vector2d{0, 10} collided bool
inside bool
pA := Point2d{5, 5} pos Point2d
pB := Point2d{15, 15} }
r := AABB2d{ tests := []struct {
A: pA, rect AABB2d
B: pB, p Point2d
v Vector2d
expected result
}{
{
rect: AABB2d{
A: Point2d{5, 5},
B: Point2d{15, 15},
},
p: Point2d{10, 0},
v: Vector2d{0, 10},
expected: result{
collided: true,
inside: false,
pos: Point2d{10, 5},
},
},
{
rect: AABB2d{
A: Point2d{0, 0},
B: Point2d{10, 10},
},
p: Point2d{1, 1},
v: Vector2d{2, 2},
expected: result{
collided: false,
inside: true,
pos: Point2d{1, 1},
},
},
}
for _, test := range tests {
coll, inside, pos := RectIntersection(test.rect, test.p, test.v)
if coll != test.expected.collided {
t.Errorf("unexpected collision: expected: %t, actual: %t", test.expected.collided, coll)
}
if inside != test.expected.inside {
t.Errorf("RectIntersection Error")
}
if pos != test.expected.pos {
t.Errorf("RectIntersection Error")
}
} }
coll, inside, pos := RectIntersection(r, p1, v1)
if coll != true {
t.Errorf("RectIntersection Error")
}
if inside != false {
t.Errorf("RectIntersection Error")
}
if pos.X != 10 && pos.Y != 5 {
t.Errorf("RectIntersection Error")
}
} }
func TestRectRectIntersectionTrue(t *testing.T) { func TestRectRectIntersectionTrue(t *testing.T) {