public static bool pointToBox(Vector2 point, Box box, out CollisionResult result) { result = new CollisionResult(); if (box.containsPoint(point)) { // get the point in the space of the Box result.point = box.bounds.GetClosestPointOnRectangleBorderToPoint(point, out result.normal); result.minimumTranslationVector = point - result.point; return(true); } return(false); }
public static bool pointToBox( Vector2 point, Box box, out CollisionResult result ) { result = new CollisionResult(); if( box.containsPoint( point ) ) { // get the point in the space of the Box result.point = box.bounds.getClosestPointOnRectangleBorderToPoint( point, out result.normal ); result.minimumTranslationVector = point - result.point; return true; } return false; }
/// <summary> /// works for circles whos center is in the box as well as just overlapping with the center out of the box. /// </summary> /// <returns><c>true</c>, if to box was circled, <c>false</c> otherwise.</returns> /// <param name="circle">First.</param> /// <param name="box">Second.</param> /// <param name="result">Result.</param> public static bool circleToBox(Circle circle, Box box, out CollisionResult result) { result = new CollisionResult(); var closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position, out result.normal); // deal with circles whos center is in the box first since its cheaper to see if we are contained if (box.containsPoint(circle.position)) { result.point = closestPointOnBounds; // calculate mtv. Find the safe, non-collided position and get the mtv from that. var safePlace = closestPointOnBounds + result.normal * circle.radius; result.minimumTranslationVector = circle.position - safePlace; return(true); } float sqrDistance; Vector2.DistanceSquared(ref closestPointOnBounds, ref circle.position, out sqrDistance); // see if the point on the box is less than radius from the circle if (sqrDistance == 0) { result.minimumTranslationVector = result.normal * circle.radius; } else if (sqrDistance <= circle.radius * circle.radius) { result.normal = circle.position - closestPointOnBounds; var depth = result.normal.Length() - circle.radius; result.point = closestPointOnBounds; Vector2Ext.normalize(ref result.normal); result.minimumTranslationVector = depth * result.normal; return(true); } return(false); }
/// <summary> /// works for circles whos center is in the box as well as just overlapping with the center out of the box. /// </summary> /// <returns><c>true</c>, if to box was circled, <c>false</c> otherwise.</returns> /// <param name="circle">First.</param> /// <param name="box">Second.</param> /// <param name="result">Result.</param> public static bool circleToBox( Circle circle, Box box, out CollisionResult result ) { result = new CollisionResult(); var closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint( circle.position, out result.normal ); // deal with circles whos center is in the box first since its cheaper to see if we are contained if( box.containsPoint( circle.position ) ) { result.point = closestPointOnBounds; // calculate mtv. Find the safe, non-collided position and get the mtv from that. var safePlace = closestPointOnBounds + result.normal * circle.radius; result.minimumTranslationVector = circle.position - safePlace; return true; } float sqrDistance; Vector2.DistanceSquared( ref closestPointOnBounds, ref circle.position, out sqrDistance ); // see if the point on the box is less than radius from the circle if( sqrDistance == 0 ) { result.minimumTranslationVector = result.normal * circle.radius; } else if( sqrDistance <= circle.radius * circle.radius ) { result.normal = circle.position - closestPointOnBounds; var depth = result.normal.Length() - circle.radius; result.point = closestPointOnBounds; Vector2Ext.normalize( ref result.normal ); result.minimumTranslationVector = depth * result.normal; return true; } return false; }