From 8a5e55e41014f69de78c586ba1095b6899aa8efd Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Wed, 15 Jan 2014 23:11:40 -0800 Subject: [PATCH] converted intersection to return point pointers --- collision.go | 21 +++++++++++---------- govector_test.go | 23 ++++++++++++++--------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/collision.go b/collision.go index 3edbbcd..64afe33 100644 --- a/collision.go +++ b/collision.go @@ -4,14 +4,15 @@ import ( "math" ) -func Intersection(p1, p2 Point2d, v1, v2 Vector2d) (bool, Point2d) { +func Intersection(p1, p2 Point2d, v1, v2 Vector2d) (bool, *Point2d) { t := p2.Sub(p1).Cross(v2) / v1.Cross(v2) s := p2.Sub(p1).Cross(v1) / v1.Cross(v2) if t > 0 && t < 1 && s > 0 && s < 1 { - return true, p1.Add(v1.Scale(t)) + intersection := p1.Add(v1.Scale(t)) + return true, &intersection } - return false, Point2d{0, 0} + return false, nil } func RectRectIntersection(r1 AABB2d, r2 AABB2d) bool { @@ -20,14 +21,14 @@ func RectRectIntersection(r1 AABB2d, r2 AABB2d) bool { } // 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 inside := false start_inside := PointInRect(p, r) end_inside := PointInRect(p.Add(v), r) if start_inside && end_inside { inside = true - return collision, inside, p + return collision, inside, nil } else { wall_left, col1 := Intersection(p, r.A, v, Vector2d{X: 0, Y: r.B.Y - r.A.Y}) if wall_left { @@ -46,7 +47,7 @@ func RectIntersection(r AABB2d, p Point2d, v Vector2d) (bool, bool, Point2d) { return wall_bottom, inside, col4 } - return false, false, p + return false, false, nil } } @@ -66,11 +67,11 @@ func ProjectPolygon(axis Vector2d, p Polygon2d) (min float32, max float32) { return min, max } -func PolygonIntersection(r Polygon2d, p Point2d, v Vector2d) (bool, Point2d) { +func PolygonIntersection(r Polygon2d, p Point2d, v Vector2d) (bool, *Point2d) { // TODO - need to test point in polygon for start and end to detect wholly // inside situations intersection := false - i_point := Point2d{} + var iPoint *Point2d distance_sq := float32(math.MaxFloat32) for point, _ := range r.Points { @@ -80,13 +81,13 @@ func PolygonIntersection(r Polygon2d, p Point2d, v Vector2d) (bool, Point2d) { intersection = true d_sq := pt.Sub(p).MagSquared() if d_sq < distance_sq { - i_point = pt + iPoint = pt distance_sq = d_sq } } } - return intersection, i_point + return intersection, iPoint } func PointInPolygon(p Point2d, py Polygon2d) bool { diff --git a/govector_test.go b/govector_test.go index dedf929..08bd84d 100644 --- a/govector_test.go +++ b/govector_test.go @@ -220,7 +220,7 @@ func TestRectIntersect(t *testing.T) { type result struct { collided bool inside bool - pos Point2d + pos *Point2d } tests := []struct { rect AABB2d @@ -239,7 +239,7 @@ func TestRectIntersect(t *testing.T) { expected: result{ collided: false, inside: true, - pos: Point2d{1, 1}, + pos: nil, }, }, // bottom @@ -253,7 +253,7 @@ func TestRectIntersect(t *testing.T) { expected: result{ collided: true, inside: false, - pos: Point2d{10, 5}, + pos: &Point2d{10, 5}, }, }, // wall left @@ -267,7 +267,7 @@ func TestRectIntersect(t *testing.T) { expected: result{ collided: true, inside: false, - pos: Point2d{0, 5}, + pos: &Point2d{0, 5}, }, }, // wall right @@ -281,7 +281,7 @@ func TestRectIntersect(t *testing.T) { expected: result{ collided: true, inside: false, - pos: Point2d{10, 5}, + pos: &Point2d{10, 5}, }, }, // wall top @@ -295,7 +295,7 @@ func TestRectIntersect(t *testing.T) { expected: result{ collided: true, inside: false, - pos: Point2d{5, 10}, + pos: &Point2d{5, 10}, }, }, // outside @@ -309,7 +309,7 @@ func TestRectIntersect(t *testing.T) { expected: result{ collided: false, inside: false, - pos: Point2d{5, -1}, + pos: nil, }, }, } @@ -322,8 +322,13 @@ func TestRectIntersect(t *testing.T) { t.Errorf("RectIntersection Error") t.Errorf("unexpected inside: expected: %t, actual: %t", test.expected.inside, inside) } - if pos != test.expected.pos { - t.Errorf("unexpected collision point: expected: %+v, actual: %+v", test.expected.pos, pos) + if test.expected.pos == nil && pos != nil { + t.Error("expected nil position, got non-nil") + } + if pos != nil && test.expected.pos != nil { + if *pos != *test.expected.pos { + t.Errorf("unexpected collision point: expected: %+v, actual: %+v", test.expected.pos, pos) + } } }