/*WORKING*/ public static HullCollision2D detectCollisionResponse(CircleHull2D circle, AABBHull2D square) { float dist = Vector2.Distance(circle.position, square.GetClosestPoint(circle.position)); Vector2 squareClosestPoint = square.GetClosestPoint(circle.position); Vector2 circleClosestPoint = circle.GetClosestPoint(squareClosestPoint); //need to do second Debug.DrawLine(squareClosestPoint, circleClosestPoint); if (circle.radius > dist) { Vector2 pen = squareClosestPoint - circleClosestPoint; Vector2 penNorm = pen.normalized; if (penNorm.x > penNorm.y) { pen.y = 0.0f; } else { pen.x = 0.0f; } HullCollision2D collision; collision = new HullCollision2D(circle, square, penNorm.normalized, pen.magnitude); return(collision); } return(null); }
public static HullCollision2D detectCollisionResponse(AABBHull2D a, AABBHull2D b) { Dictionary <Vector2, float> axisValues = new Dictionary <Vector2, float>(); //norm to the overlap value axisValues.Add(Vector2.up, checkAxisPenetration(a, b, Vector2.up)); axisValues.Add(Vector2.right, checkAxisPenetration(a, b, Vector2.right)); Dictionary <Vector2, float> .Enumerator enumerator = axisValues.GetEnumerator(); enumerator.MoveNext(); KeyValuePair <Vector2, float> bestPenetration = enumerator.Current; //need to set one to compare too, no null value to just allow all checking in the for loop if (bestPenetration.Value == 0.0f) { return(null); } while (enumerator.MoveNext()) { if (Mathf.Abs(enumerator.Current.Value) < Mathf.Abs(bestPenetration.Value)) { bestPenetration = enumerator.Current; } if (bestPenetration.Value == 0.0f) { return(null); } } HullCollision2D collision; collision = new HullCollision2D(a, b, bestPenetration.Key, Mathf.Abs(bestPenetration.Value)); return(collision); }
public static bool checkAxis(OBBHull2D a, AABBHull2D b, Vector2 norm) { Vector2 aProjValues = getMinMaxProjectionValuesOnNorm(a, norm); Vector2 bProjValues = getMinMaxProjectionValuesOnNorm(b, norm); Debug.DrawLine(aProjValues.x * norm, aProjValues.y * norm, Color.blue); //this is dumb useful, the projected line Debug.DrawLine(bProjValues.x * norm, bProjValues.y * norm, Color.red); //this is dumb useful, the projected line return(detectCollisionFromMinMax(aProjValues, bProjValues)); }
/* * Circle V AABB */ public static bool detectCollision(CircleHull2D circle, AABBHull2D square) { Vector2 closestPoint = square.GetClosestPoint(circle.position); Debug.DrawLine(new Vector3(closestPoint.x, closestPoint.y, 0), circle.position); if (circle.radius > Vector2.Distance(circle.position, closestPoint)) { return(true); } return(false); }
/* * VERIFIED WORKING, based off OBB v OBB Collision */ public static bool detectCollision(OBBHull2D a, AABBHull2D b) { if (checkAxis(a, b, a.getXNormal()) && checkAxis(a, b, a.getYNormal()) && checkAxis(a, b, Vector2.left) && checkAxis(a, b, Vector2.up)) { return(true); } return(false); }
/* * AABB v AABB */ public static bool detectCollision(AABBHull2D a, AABBHull2D b) { for (int i = 0; i < 2; i++) //do for each axis { Vector2 minMaxA = new Vector2(a.position[i] - a.halfLength[i], a.position[i] + a.halfLength[i]); Vector2 minMaxB = new Vector2(b.position[i] - b.halfLength[i], b.position[i] + b.halfLength[i]); if (!detectCollisionFromMinMax(minMaxA, minMaxB)) { return(false); } } return(true); }
public static Vector4 getMinMaxProjectionValuesOnNorm(AABBHull2D hull, Vector2 norm) { float a, b, c, d; int axis = norm.x != 0 ? 0 : 1; //just to make a scaler, but in case the x axis is zero, and if they are both zero then what the hell u doing with a normal (0,0) a = proj(hull.position + hull.halfLength, norm)[axis] / norm[axis]; b = proj(hull.position - hull.halfLength, norm)[axis] / norm[axis]; c = proj(hull.position + new Vector2(hull.halfLength.x, -hull.halfLength.y), norm)[axis] / norm[axis]; d = proj(hull.position + new Vector2(-hull.halfLength.x, +hull.halfLength.y), norm)[axis] / norm[axis]; float min = Mathf.Min(new float[] { a, b, c, d }); float max = Mathf.Max(new float[] { a, b, c, d }); return(new Vector2(min, max)); }
public static float checkAxisPenetration(AABBHull2D a, AABBHull2D b, Vector2 norm) { Vector2 aProjValues = getMinMaxProjectionValuesOnNorm(a, norm); Vector2 bProjValues = getMinMaxProjectionValuesOnNorm(b, norm); Debug.DrawLine(aProjValues.x * norm, aProjValues.y * norm, Color.blue); //this is dumb useful, the projected line Debug.DrawLine(bProjValues.x * norm, bProjValues.y * norm, Color.red); //this is dumb useful, the projected line if (!detectCollisionFromMinMax(aProjValues, bProjValues)) { return(0.0f); } else { return(calculateMinMaxCollisionOverlap(aProjValues, bProjValues)); } }