Beispiel #1
0
        /// <summary>
        /// iterates all the edges of the polygon and gets the closest point on any edge to point. Returns via out the squared distance
        /// to the closest point and the normal of the edge it is on.
        /// </summary>
        /// <returns>The closest point on polygon to point.</returns>
        /// <param name="point">Point.</param>
        /// <param name="distanceSquared">Distance squared.</param>
        /// <param name="edgeNormal">Edge normal.</param>
        public Vector2 getClosestPointOnPolygonToPoint(Vector2 point, out float distanceSquared, out Vector2 edgeNormal)
        {
            distanceSquared = float.MaxValue;
            edgeNormal      = Vector2.Zero;
            var   closestPoint = Vector2.Zero;
            float tempDistanceSquared;

            for (var i = 0; i < points.Length; i++)
            {
                var j = i + 1;
                if (j == points.Length)
                {
                    j = 0;
                }

                var closest = ShapeCollisions.closestPointOnLine(points[i], points[j], point);
                Vector2.DistanceSquared(ref point, ref closest, out tempDistanceSquared);

                if (tempDistanceSquared < distanceSquared)
                {
                    distanceSquared = tempDistanceSquared;
                    closestPoint    = closest;

                    // get the normal of the line
                    var line = points[j] - points[i];
                    edgeNormal.X = line.Y;
                    edgeNormal.Y = -line.X;
                }
            }

            return(closestPoint);
        }
Beispiel #2
0
        public override bool pointCollidesWithShape(Vector2 point, out CollisionResult result)
        {
            if (isUnrotated)
            {
                return(ShapeCollisions.pointToBox(point, this, out result));
            }

            return(base.pointCollidesWithShape(point, out result));
        }
Beispiel #3
0
        public override bool PointCollidesWithShape(System.Numerics.Vector2 point, out CollisionResult result)
        {
            if (IsUnrotated)
            {
                return(ShapeCollisions.PointToBox(point, this, out result));
            }

            return(base.PointCollidesWithShape(point, out result));
        }
Beispiel #4
0
        public override bool CollidesWithShape(Shape other, out CollisionResult result)
        {
            // special, high-performance cases. otherwise we fall back to Polygon.
            if (IsUnrotated && other is Box && (other as Box).IsUnrotated)
            {
                return(ShapeCollisions.BoxToBox(this, other as Box, out result));
            }

            // TODO: get Minkowski working for circle to box
            //if( other is Circle )

            // fallthrough to standard cases
            return(base.CollidesWithShape(other, out result));
        }
Beispiel #5
0
        public override bool collidesWithShape(Shape other, out CollisionResult result)
        {
            if (other is Polygon)
            {
                return(ShapeCollisions.polygonToPolygon(this, other as Polygon, out result));
            }

            if (other is Circle && ShapeCollisions.circleToPolygon(other as Circle, this, out result))
            {
                result.invertResult();
                return(true);
            }

            throw new NotImplementedException(string.Format("overlaps of Polygon to {0} are not supported", other));
        }
Beispiel #6
0
        public override bool overlaps(Shape other)
        {
            CollisionResult result;

            if (other is Polygon)
            {
                return(ShapeCollisions.polygonToPolygon(this, other as Polygon, out result));
            }

            if (other is Circle)
            {
                ShapeCollisions.circleToPolygon(other as Circle, this, out result);
            }

            throw new NotImplementedException(string.Format("overlaps of Polygon to {0} are not supported", other));
        }
Beispiel #7
0
        public override bool collidesWithShape(Shape other, out CollisionResult result)
        {
            if (other is Polygon)
            {
                return(ShapeCollisions.polygonToPolygon(this, other as Polygon, out result));
            }

            if (other is Circle && ShapeCollisions.circleToPolygon(other as Circle, this, out result))
            {
                // TODO: flip the result since the colliding objects are reversed
                throw new NotImplementedException("ShapeCollisionResult will probably be wrong due to the result needing to be flipped. TODO");
                //return true;
            }

            throw new NotImplementedException(string.Format("overlaps of Polygon to {0} are not supported", other));
        }
Beispiel #8
0
        public override bool collidesWithShape(Shape other, out CollisionResult result)
        {
            if (other is Box && (other as Box).isUnrotated)
            {
                return(ShapeCollisions.circleToBox(this, other as Box, out result));
            }

            if (other is Circle)
            {
                return(ShapeCollisions.circleToCircle(this, other as Circle, out result));
            }

            if (other is Polygon)
            {
                return(ShapeCollisions.circleToPolygon(this, other as Polygon, out result));
            }

            throw new NotImplementedException(string.Format("Collisions of Circle to {0} are not supported", other));
        }
Beispiel #9
0
        public override bool overlaps(Shape other)
        {
            CollisionResult result;

            if (other is Box)
            {
                return(Collisions.rectToCircle(ref other.bounds, position, radius));
            }

            if (other is Circle)
            {
                return(Collisions.circleToCircle(position, radius, other.position, (other as Circle).radius));
            }

            if (other is Polygon)
            {
                return(ShapeCollisions.circleToPolygon(this, other as Polygon, out result));
            }

            throw new NotImplementedException(string.Format("overlaps of Circle to {0} are not supported", other));
        }
Beispiel #10
0
        public override bool Overlaps(Shape other)
        {
            CollisionResult result;

            // Box is only optimized for unrotated
            if (other is Box && (other as Box).IsUnrotated)
            {
                return(Collisions.RectToCircle(ref other.bounds, position, Radius));
            }

            if (other is Circle)
            {
                return(Collisions.CircleToCircle(position, Radius, other.position, (other as Circle).Radius));
            }

            if (other is Polygon)
            {
                return(ShapeCollisions.CircleToPolygon(this, other as Polygon, out result));
            }

            throw new NotImplementedException(string.Format("overlaps of Circle to {0} are not supported", other));
        }
Beispiel #11
0
 public override bool pointCollidesWithShape(Vector2 point, out CollisionResult result)
 {
     return(ShapeCollisions.pointToCircle(point, this, out result));
 }
Beispiel #12
0
 public override bool collidesWithLine(Vector2 start, Vector2 end, out RaycastHit hit)
 {
     hit = new RaycastHit();
     return(ShapeCollisions.lineToCircle(start, end, this, out hit));
 }
Beispiel #13
0
 public override bool PointCollidesWithShape(Vector2 point, out CollisionResult result)
 {
     return(ShapeCollisions.PointToPoly(point, this, out result));
 }
Beispiel #14
0
 public override bool PointCollidesWithShape(System.Numerics.Vector2 point, out CollisionResult result)
 {
     return(ShapeCollisions.PointToCircle(point, this, out result));
 }
Beispiel #15
0
 public override bool CollidesWithLine(System.Numerics.Vector2 start, System.Numerics.Vector2 end, out RaycastHit hit)
 {
     hit = new RaycastHit();
     return(ShapeCollisions.LineToCircle(start, end, this, out hit));
 }