/// <summary> /// Adapted from OLC Tutorial /// </summary> /// <param name="body"></param> /// <param name="target"></param> /// <param name="contactPoint"></param> /// <param name="contactNormal"></param> /// <param name="contactTime"></param> /// <param name="dt"></param> /// <returns></returns> public static bool DynamicRectVsRect(SimplePhysicsBody body, RectangleF target, out Vector2 contactPoint, out Vector2 contactNormal, out float contactTime, float dt) { contactPoint = Vector2.Zero; contactNormal = Vector2.Zero; contactTime = 0f; if (body.Velocity == Vector2.Zero) { return(false); } var bodyRect = body.ColliderRect; var expandedTarget = new RectangleF(target.Position - bodyRect.Size / 2, target.Size + bodyRect.Size); if (RayVsRect(bodyRect.Center, body.Velocity * dt, expandedTarget, out contactPoint, out contactNormal, out contactTime)) { if (contactTime < 1f && contactTime >= 0f) { return(true); } } return(false); }
/// <summary> /// Assuming this is a dynamic body, resolve a collision against this other body /// </summary> /// <param name="other"></param> public void CollideWith(SimplePhysicsBody other, float dt) { var otherCollider = other.ColliderRect; if (DynamicRectVsRect(this, otherCollider, out var contactPoint, out var contactNormal, out var contactTime, dt)) { this.collisionsThisFrame.Add(new CollideMoment(contactTime, otherCollider, contactNormal)); } }