private static Projection ProjectOn(this RectangleCollider rectCollider, Vector2 vector) { Requires.NotNull(rectCollider, nameof(rectCollider)); float min = Vector2.Dot(rectCollider.GetPoints2D()[0], vector); float max = min; for (int i = 1; i < rectCollider.GetPoints2D().Length; ++i) { float dot = Vector2.Dot(rectCollider.GetPoints2D()[i], vector); min = Math.Min(dot, min); max = Math.Max(dot, max); } return(new Projection(min, max)); }
/// <summary> /// Since a rectangle is a type of parallelogram, I only need to get two axes - two adjacent ones. In the /// more general case, I would get the normals of all the edges in the convex polygon. /// This function assumes the "GetPoints2D()" method is ordered (first and second points are an edge, /// second and third are an edge, etc) /// </summary> /// <param name="rectCollider">The <see cref="RectangleCollider"/> to get all testing axes</param> /// <returns>An <see cref="IEnumerable{T}"/> that needs to be tested.</returns> private static IEnumerable <Vector2> GetTestingAxes(RectangleCollider rectCollider) { Requires.NotNull(rectCollider, nameof(rectCollider)); Vector2 edge1 = rectCollider.GetPoints2D()[1] - rectCollider.GetPoints2D()[0]; Vector2 axis1 = edge1.Normal(); axis1.Normalize(); yield return(axis1); Vector2 edge2 = rectCollider.GetPoints2D()[0] - rectCollider.GetPoints2D()[3]; Vector2 axis2 = edge2.Normal(); axis2.Normalize(); yield return(axis2); }
/// <summary> /// Since a rectangle is a type of parallelogram, I only need to get two axes - two adjacent ones. In the /// more general case, I would get the normals of all the edges in the convex polygon. /// This function assumes the "GetPoints2D()" method is ordered (first and second points are an edge, /// second and third are an edge, etc) /// </summary> /// <param name="rectCollider">The <see cref="RectangleCollider"/> to get all testing axes</param> /// <returns>An <see cref="IEnumerable{T}"/> that needs to be tested.</returns> private static IEnumerable<Vector2> GetTestingAxes(RectangleCollider rectCollider) { Requires.NotNull(rectCollider, nameof(rectCollider)); Vector2 edge1 = rectCollider.GetPoints2D()[1] - rectCollider.GetPoints2D()[0]; Vector2 axis1 = edge1.Normal(); axis1.Normalize(); yield return axis1; Vector2 edge2 = rectCollider.GetPoints2D()[0] - rectCollider.GetPoints2D()[3]; Vector2 axis2 = edge2.Normal(); axis2.Normalize(); yield return axis2; }