converted intersection to return point pointers
This commit is contained in:
parent
282bcb448f
commit
8a5e55e410
21
collision.go
21
collision.go
@ -4,14 +4,15 @@ import (
|
|||||||
"math"
|
"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)
|
t := p2.Sub(p1).Cross(v2) / v1.Cross(v2)
|
||||||
s := p2.Sub(p1).Cross(v1) / v1.Cross(v2)
|
s := p2.Sub(p1).Cross(v1) / v1.Cross(v2)
|
||||||
if t > 0 && t < 1 && s > 0 && s < 1 {
|
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 {
|
func RectRectIntersection(r1 AABB2d, r2 AABB2d) bool {
|
||||||
@ -20,14 +21,14 @@ func RectRectIntersection(r1 AABB2d, r2 AABB2d) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// returns collision, inside, point of collision
|
// 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
|
||||||
start_inside := PointInRect(p, r)
|
start_inside := PointInRect(p, r)
|
||||||
end_inside := PointInRect(p.Add(v), r)
|
end_inside := PointInRect(p.Add(v), r)
|
||||||
if start_inside && end_inside {
|
if start_inside && end_inside {
|
||||||
inside = true
|
inside = true
|
||||||
return collision, inside, p
|
return collision, inside, nil
|
||||||
} else {
|
} else {
|
||||||
wall_left, col1 := Intersection(p, r.A, v, Vector2d{X: 0, Y: r.B.Y - r.A.Y})
|
wall_left, col1 := Intersection(p, r.A, v, Vector2d{X: 0, Y: r.B.Y - r.A.Y})
|
||||||
if wall_left {
|
if wall_left {
|
||||||
@ -46,7 +47,7 @@ func RectIntersection(r AABB2d, p Point2d, v Vector2d) (bool, bool, Point2d) {
|
|||||||
return wall_bottom, inside, col4
|
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
|
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
|
// TODO - need to test point in polygon for start and end to detect wholly
|
||||||
// inside situations
|
// inside situations
|
||||||
intersection := false
|
intersection := false
|
||||||
i_point := Point2d{}
|
var iPoint *Point2d
|
||||||
distance_sq := float32(math.MaxFloat32)
|
distance_sq := float32(math.MaxFloat32)
|
||||||
|
|
||||||
for point, _ := range r.Points {
|
for point, _ := range r.Points {
|
||||||
@ -80,13 +81,13 @@ func PolygonIntersection(r Polygon2d, p Point2d, v Vector2d) (bool, Point2d) {
|
|||||||
intersection = true
|
intersection = true
|
||||||
d_sq := pt.Sub(p).MagSquared()
|
d_sq := pt.Sub(p).MagSquared()
|
||||||
if d_sq < distance_sq {
|
if d_sq < distance_sq {
|
||||||
i_point = pt
|
iPoint = pt
|
||||||
distance_sq = d_sq
|
distance_sq = d_sq
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return intersection, i_point
|
return intersection, iPoint
|
||||||
}
|
}
|
||||||
|
|
||||||
func PointInPolygon(p Point2d, py Polygon2d) bool {
|
func PointInPolygon(p Point2d, py Polygon2d) bool {
|
||||||
|
@ -220,7 +220,7 @@ func TestRectIntersect(t *testing.T) {
|
|||||||
type result struct {
|
type result struct {
|
||||||
collided bool
|
collided bool
|
||||||
inside bool
|
inside bool
|
||||||
pos Point2d
|
pos *Point2d
|
||||||
}
|
}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
rect AABB2d
|
rect AABB2d
|
||||||
@ -239,7 +239,7 @@ func TestRectIntersect(t *testing.T) {
|
|||||||
expected: result{
|
expected: result{
|
||||||
collided: false,
|
collided: false,
|
||||||
inside: true,
|
inside: true,
|
||||||
pos: Point2d{1, 1},
|
pos: nil,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
// bottom
|
// bottom
|
||||||
@ -253,7 +253,7 @@ func TestRectIntersect(t *testing.T) {
|
|||||||
expected: result{
|
expected: result{
|
||||||
collided: true,
|
collided: true,
|
||||||
inside: false,
|
inside: false,
|
||||||
pos: Point2d{10, 5},
|
pos: &Point2d{10, 5},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
// wall left
|
// wall left
|
||||||
@ -267,7 +267,7 @@ func TestRectIntersect(t *testing.T) {
|
|||||||
expected: result{
|
expected: result{
|
||||||
collided: true,
|
collided: true,
|
||||||
inside: false,
|
inside: false,
|
||||||
pos: Point2d{0, 5},
|
pos: &Point2d{0, 5},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
// wall right
|
// wall right
|
||||||
@ -281,7 +281,7 @@ func TestRectIntersect(t *testing.T) {
|
|||||||
expected: result{
|
expected: result{
|
||||||
collided: true,
|
collided: true,
|
||||||
inside: false,
|
inside: false,
|
||||||
pos: Point2d{10, 5},
|
pos: &Point2d{10, 5},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
// wall top
|
// wall top
|
||||||
@ -295,7 +295,7 @@ func TestRectIntersect(t *testing.T) {
|
|||||||
expected: result{
|
expected: result{
|
||||||
collided: true,
|
collided: true,
|
||||||
inside: false,
|
inside: false,
|
||||||
pos: Point2d{5, 10},
|
pos: &Point2d{5, 10},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
// outside
|
// outside
|
||||||
@ -309,7 +309,7 @@ func TestRectIntersect(t *testing.T) {
|
|||||||
expected: result{
|
expected: result{
|
||||||
collided: false,
|
collided: false,
|
||||||
inside: false,
|
inside: false,
|
||||||
pos: Point2d{5, -1},
|
pos: nil,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -322,10 +322,15 @@ func TestRectIntersect(t *testing.T) {
|
|||||||
t.Errorf("RectIntersection Error")
|
t.Errorf("RectIntersection Error")
|
||||||
t.Errorf("unexpected inside: expected: %t, actual: %t", test.expected.inside, inside)
|
t.Errorf("unexpected inside: expected: %t, actual: %t", test.expected.inside, inside)
|
||||||
}
|
}
|
||||||
if pos != test.expected.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)
|
t.Errorf("unexpected collision point: expected: %+v, actual: %+v", test.expected.pos, pos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user