poly - line intersect and tests
This commit is contained in:
parent
5b6dac04e0
commit
6170691eb1
18
collision.go
18
collision.go
@ -61,9 +61,21 @@ 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, bool, Point2d) {
|
func PolygonIntersection(r Polygon2d, p Point2d, v Vector2d) (bool, Point2d) {
|
||||||
// TBD
|
|
||||||
return false, false, Point2d{}
|
for point, _ := range r.Points {
|
||||||
|
if point == len(r.Points) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
edgePt, edgeVec := r.Edge(point)
|
||||||
|
intersection, pt := Intersection(p, edgePt, v, edgeVec)
|
||||||
|
if intersection {
|
||||||
|
return intersection, pt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, Point2d{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func PolyPolyIntersection(p1 Polygon2d, v1 Vector2d, p2 Polygon2d, v2 Vector2d) bool {
|
func PolyPolyIntersection(p1 Polygon2d, v1 Vector2d, p2 Polygon2d, v2 Vector2d) bool {
|
||||||
|
@ -232,3 +232,57 @@ func TestRectIntersect(t *testing.T) {
|
|||||||
t.Errorf("RectIntersection Error")
|
t.Errorf("RectIntersection Error")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRectRectIntersectionTrue(t *testing.T) {
|
||||||
|
r1 := AABB2d{Point2d{10, 10}, Point2d{20, 20}}
|
||||||
|
r2 := AABB2d{Point2d{15, 15}, Point2d{25, 25}}
|
||||||
|
|
||||||
|
col := RectRectIntersection(r1, r2)
|
||||||
|
if !col {
|
||||||
|
t.Errorf("RectRect Error")
|
||||||
|
}
|
||||||
|
|
||||||
|
r1 = AABB2d{Point2d{10, 10}, Point2d{20, 20}}
|
||||||
|
r2 = AABB2d{Point2d{5, 15}, Point2d{30, 25}}
|
||||||
|
|
||||||
|
col = RectRectIntersection(r1, r2)
|
||||||
|
if !col {
|
||||||
|
t.Errorf("RectRect Error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRectRectIntersectionFalse(t *testing.T) {
|
||||||
|
r1 := AABB2d{Point2d{10, 10}, Point2d{14, 14}}
|
||||||
|
r2 := AABB2d{Point2d{15, 15}, Point2d{25, 25}}
|
||||||
|
|
||||||
|
col := RectRectIntersection(r1, r2)
|
||||||
|
if col {
|
||||||
|
t.Errorf("RectRect Error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPolyIntersect(t *testing.T) {
|
||||||
|
p1 := Polygon2d{}
|
||||||
|
p1.Points = append(p1.Points, Vector2d{3, 3})
|
||||||
|
p1.Points = append(p1.Points, Vector2d{5, 5})
|
||||||
|
p1.Points = append(p1.Points, Vector2d{5, 3})
|
||||||
|
|
||||||
|
i, p := PolygonIntersection(p1, Point2d{0, 0}, Vector2d{10, 10})
|
||||||
|
if i {
|
||||||
|
fmt.Printf("%v", p)
|
||||||
|
t.Errorf("PolyIntersect Error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPolyIntersectFail(t *testing.T) {
|
||||||
|
p1 := Polygon2d{}
|
||||||
|
p1.Points = append(p1.Points, Vector2d{3, 3})
|
||||||
|
p1.Points = append(p1.Points, Vector2d{5, 5})
|
||||||
|
p1.Points = append(p1.Points, Vector2d{5, 3})
|
||||||
|
|
||||||
|
i, p := PolygonIntersection(p1, Point2d{0, 0}, Vector2d{-10, -10})
|
||||||
|
if i {
|
||||||
|
fmt.Printf("%v", p)
|
||||||
|
t.Errorf("PolyIntersect Error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -15,6 +15,16 @@ type Polygon2d struct {
|
|||||||
Origin Point2d `json:"origin"`
|
Origin Point2d `json:"origin"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p Polygon2d) Edge(i int) (Point2d, Vector2d) {
|
||||||
|
if i >= len(p.Points)-1 {
|
||||||
|
return Point2d{}, Vector2d{}
|
||||||
|
}
|
||||||
|
|
||||||
|
p1 := p.Origin.Add(p.Points[i])
|
||||||
|
p2 := p.Origin.Add(p.Points[i+1])
|
||||||
|
return p1, p2.Sub(p1)
|
||||||
|
}
|
||||||
|
|
||||||
type Vector2d struct {
|
type Vector2d struct {
|
||||||
X float32 `json:"x"`
|
X float32 `json:"x"`
|
||||||
Y float32 `json:"y"`
|
Y float32 `json:"y"`
|
||||||
|
Loading…
Reference in New Issue
Block a user