/// <summary> /// Checks for collision between two figures/shapes with the use of SAT (Seperating Axis Theorem), and returns the pushvector, which is zero if there is no collision. /// </summary> /// <param name="edgesFigure1"></param> /// <param name="posFigure1"></param> /// <param name="edgesFigure2"></param> /// <param name="posFigure2"></param> /// <returns></returns> static public Vector2 CheckV2(Vector2[,] edgesFigure1, Vector2 posFigure1, Vector2[,] edgesFigure2, Vector2 posFigure2) { //Gets the axes for both figures. Vector2[] axes1 = Normals(edgesFigure1); Vector2[] axes2 = Normals(edgesFigure2); //The push vectors List <Vector2> axisNorms = new List <Vector2>(); // loop over the axes1 for (int i = 0; i < axes1.GetLength(0); i++) { Vector2 axis = axes1[i]; // project both shapes onto the axis Projection p1 = FigureProjection(edgesFigure1, axis, posFigure1); Projection p2 = FigureProjection(edgesFigure2, axis, posFigure2); // do the projections overlap? if (!p1.Overlapping(p2)) { // then we can guarantee that the shapes do not overlap, so return a vector with 0 in length return(Vector2.Zero); } else { double pushScalar = p1.OverlappingV2(p2); Vector2 norm = Vector2.Normalize(axis); axisNorms.Add((float)pushScalar * norm); } } // loop over the axes2 for (int i = 0; i < axes2.GetLength(0); i++) { Vector2 axis = axes2[i]; // project both shapes onto the axis Projection p1 = FigureProjection(edgesFigure1, axis, posFigure1); Projection p2 = FigureProjection(edgesFigure2, axis, posFigure2); // do the projections overlap? if (!p1.Overlapping(p2)) { // then we can guarantee that the shapes do not overlap, so return a vector with 0 in length return(Vector2.Zero); } else { double pushScalar = p1.OverlappingV2(p2); Vector2 norm = Vector2.Normalize(axis); axisNorms.Add((float)pushScalar * norm); } } // if we get here then we know that every axis had overlap on it // so we can guarantee an intersection //Finds the smallest push vector Vector2 pushVector = new Vector2(1000000, 0); for (int i = 0; i < axisNorms.Count; i++) { if (axisNorms[i].Length() < pushVector.Length()) { pushVector = -axisNorms[i]; } } //Returns the push vector return(pushVector); }
/// <summary> /// Returns a push scalar. /// </summary> /// <param name="other"></param> /// <returns></returns> public double OverlappingV2(Projection other) { return(this.Max - other.Min); //return (this.Min < other.Min) ? (this.Max - other.Min) : (this.Min - other.Max); }