Exemplo n.º 1
0
        public bool IsColliding(ICollidable secondShape)
        {
            if (secondShape is Convex)
            {
                Convex other = (secondShape as Convex);

                for (int i = 0; i < other.points.Length; i++)
                {
                    if (IsColliding(other.points[i] + other.Position))
                    {
                        return(true);
                    }
                }

                for (int i = 0; i < points.Length; i++)
                {
                    if (other.IsColliding(points[i] + Position))
                    {
                        return(true);
                    }
                }

                return(false);
            }
            else if (secondShape is AABox)
            {
                AABox other = (AABox)secondShape;

                for (int i = 0; i < points.Length; i++)
                {
                    if (other.IsColliding(points[i]))
                    {
                        return(true);
                    }
                }

                Vector2[] corners = new Vector2[] { new Vector2(other.edges.Left, other.edges.Top), new Vector2(other.edges.Right, other.edges.Top),
                                                    new Vector2(other.edges.Right, other.edges.Bottom), new Vector2(other.edges.Left, other.edges.Bottom) };
                for (int i = 0; i < 4; i++)
                {
                    if (IsColliding(corners[i]))
                    {
                        return(true);
                    }
                }

                return(false);
            }

            throw new NotImplementedException("The collision calculation " + secondShape.GetType().Name + " - Convex has not been implemented.");
        }
Exemplo n.º 2
0
        //Bug:   Will return false if both objects occupy the exact same space
        //Note:  Will return false as long as no points from the object being compared to are within the current object.
        //       Even if a line from the other object is going through the current collision data.

        //REPLACE:  Instead try system where you check the points of the object you want to move against the
        //line you are colliding with, find the projection vector from the average of the points that are
        //past the line

        //Find line closest to the points inside the SetPiece
        /// <summary>
        /// Takes a list of points and finds the most effecient way to shove those points out of the polygon.
        /// </summary>
        /// <param name="otherObject">Colliding points to be pushed out of the polygon. IsColliding can return such a list.</param>
        /// <returns>Vector2 representing the way to stop collision that moves the offending points the least.</returns>
        public Vector2 FindDisplacementVector(Convex other)
        {
            Vector2 displacementVector = Vector2.Zero;
            int     h           = points.Length - 1;
            float   closestLine = float.MaxValue;

            for (int i = 0; i < points.Length; i++)
            {
                float temp = AverageDistanceFromPointsToLine(points[i] + Position, points[h] + Position, other.points, other.Position);
                if (Math.Abs(temp) < Math.Abs(closestLine))
                {
                    displacementVector = new Vector2(-((points[i].Y + Position.Y) - (points[h].Y + Position.Y)), (points[i].X + Position.X) - (points[h].X + Position.X));
                    closestLine        = temp;
                }
                h = i;
            }
            displacementVector.Normalize();
            displacementVector *= closestLine;
            return(displacementVector);
        }