/// <summary> /// Returns the collisionvector of a corner collision calculated with a circle in the center of the collision box. /// </summary> /// <param name="obj1">The object colliding with the corner.</param> /// <param name="corner">The circle for this corner.</param> /// <returns>A Vector2 of the collision.</returns> public static Vector2 DiagonalCollision(CollisionObject obj1, Circle corner) { #region variables from obj1 Vector2 speed = obj1.GetSpeedTotal(); Vector2 startPos = obj1.GetCenter(); #endregion #region Get line from movement vector and position Line trajectoryObj = new Line(startPos, startPos + speed); #endregion #region Find where line intersects circle Vector2[] intersects = corner.Intersects(trajectoryObj); #endregion #region Find which one of the intersections is closer int closestIndex = GetClosestVector(intersects, startPos); if (closestIndex == -1) { return(Vector2.Zero); } Vector2 closest = intersects[closestIndex]; #endregion #region Define startpoint of vector Vector2 startPoint = closest - speed; #endregion #region Construct a line through the intersection found earlier and the center of the circle Line middlePointThroughIntersect = new Line(closest, corner.center); #endregion #region Construct a line perpandicular to this line through the startpoint Line PerpendicularLineThroughStart = new Line(new Vector2(middlePointThroughIntersect.twoPointFormY, -middlePointThroughIntersect.twoPointFormX), startPoint); #endregion #region Find the intersection of the perpandicular with the line through the center of the circle Vector2 intersectPerp = PerpendicularLineThroughStart.Intersects(middlePointThroughIntersect); #endregion #region Find vector from startposition to intersect of perpandicular and center line Vector2 diffStartIntersect = intersectPerp - startPoint; #endregion #region Add vector to the intersection to find the reflected point Vector2 endPoint = intersectPerp + diffStartIntersect; #endregion #region Return vector from intersect of incoming vector's intersect with circle to reflected point return(-(endPoint - closest) - speed); #endregion //Illustration in documentation }