From c4932922d8529665216e457a48159bcee5027e37 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Tue, 7 Jan 2014 22:29:53 -0800 Subject: [PATCH] WIP --- collision.go | 1 + govector_test.go | 67 +++++++++++++++++++++++++++++++++++------------- 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/collision.go b/collision.go index 2a7a631..2d9cee4 100644 --- a/collision.go +++ b/collision.go @@ -19,6 +19,7 @@ func RectRectIntersection(r1 AABB2d, r2 AABB2d) bool { 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) { collision := false inside := false diff --git a/govector_test.go b/govector_test.go index 4b1a63b..1c71104 100644 --- a/govector_test.go +++ b/govector_test.go @@ -216,26 +216,57 @@ func TestPtInRect(t *testing.T) { } func TestRectIntersect(t *testing.T) { - p1 := Point2d{10, 0} - v1 := Vector2d{0, 10} - - pA := Point2d{5, 5} - pB := Point2d{15, 15} - r := AABB2d{ - A: pA, - B: pB, + type result struct { + collided bool + inside bool + pos Point2d + } + tests := []struct { + rect AABB2d + 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) {