Exemplo n.º 1
0
        public bool DoesIntersect(LineCollider otherLine, Vector2 p1, Vector2 p2)
        {
            float k1 = (EndPoint.Y - p1.Y) / (EndPoint.X - p1.X);
            float k2 = (otherLine.EndPoint.Y - p2.Y) / (otherLine.EndPoint.X - p2.X);

            if (k1 == k2)
            {
                return(false);
            }

            float xIntersect = (p2.Y - p1.Y) / (k1 - k2);
            float yIntersect = k1 * xIntersect + p1.Y;

            return(MyMaths.DistanceCubed(p1, EndPoint) < MyMaths.DistanceCubed(p1, new Vector2(xIntersect, yIntersect)));
        }
Exemplo n.º 2
0
        public Vector2 BoxIntersection(BoxCollider bc, Vector2 lineStart, Vector2 boxPosition)
        {
            LineCollider topRight   = new LineCollider(new Vector2(boxPosition.X, boxPosition.Y));
            LineCollider BottomLeft = new LineCollider(new Vector2(boxPosition.X + bc.Size.X, boxPosition.Y + bc.Size.Y));

            Vector2 intersection = Vector2.Zero;

            if (DoesIntersect(topRight, lineStart, boxPosition + new Vector2(0, bc.Size.Y)))
            {
                intersection = Intersection(topRight, lineStart, boxPosition + new Vector2(0, bc.Size.Y));
            }

            if (DoesIntersect(topRight, lineStart, boxPosition + new Vector2(bc.Size.X, 0)))
            {
                Vector2 tempIntersection = Intersection(topRight, lineStart, boxPosition + new Vector2(bc.Size.X, 0));
                if (MyMaths.DistanceCubed(lineStart, tempIntersection) > MyMaths.DistanceCubed(lineStart, intersection))
                {
                    intersection = tempIntersection;
                }
            }

            if (DoesIntersect(BottomLeft, lineStart, boxPosition + new Vector2(bc.Size.X, 0)))
            {
                Vector2 tempIntersection = Intersection(BottomLeft, lineStart, boxPosition + new Vector2(bc.Size.X, 0));
                if (MyMaths.DistanceCubed(lineStart, tempIntersection) > MyMaths.DistanceCubed(lineStart, intersection))
                {
                    intersection = tempIntersection;
                }
            }

            if (DoesIntersect(BottomLeft, lineStart, boxPosition + new Vector2(0, bc.Size.Y)))
            {
                Vector2 tempIntersection = Intersection(BottomLeft, lineStart, boxPosition + new Vector2(0, bc.Size.Y));
                if (MyMaths.DistanceCubed(lineStart, tempIntersection) > MyMaths.DistanceCubed(lineStart, intersection))
                {
                    intersection = tempIntersection;
                }
            }

            return(intersection);
        }