/// from http://pastie.org/3152377 /// <summary> /// Get the depth of the vertical intersection between the two boxes. /// </summary> /// <param name="boxA">A box.</param> /// <param name="boxB">Another box.</param> /// <returns>The depth of the vertical intersection, negative if up, /// positive if down.</returns> public static float GetVerticalIntersectionDepth(BBox boxA, BBox boxB) { // Calculate half sizes. float halfHeightA = boxA.Height / 2.0f; float halfHeightB = boxB.Height / 2.0f; // Calculate centers. float centerA = boxA.Top + halfHeightA; float centerB = boxB.Top + halfHeightB; // Calculate current and minimum-non-intersecting distances between centers. float distanceY = centerA - centerB; float minDistanceY = halfHeightA + halfHeightB; // If we are not intersecting at all, return (0, 0). if (Math.Abs(distanceY) >= minDistanceY) return 0f; // Calculate and return intersection depths. return distanceY > 0 ? minDistanceY - distanceY : -minDistanceY - distanceY; }
/// from http://pastie.org/3152377 /// <summary> /// Get the depth of the horizontal interesection between two boxes. /// </summary> /// <param name="boxA">A box.</param> /// <param name="boxB">Another box.</param> /// <returns>The depth of the horizontal intersection, negative if left, /// positive if right.</returns> public static float GetHorizontalIntersectionDepth(BBox boxA, BBox boxB) { // Calculate half sizes. float halfWidthA = boxA.Width / 2.0f; float halfWidthB = boxB.Width / 2.0f; // Calculate centers. float centerA = boxA.Left + halfWidthA; float centerB = boxB.Left + halfWidthB; // Calculate current and minimum-non-intersecting distances between centers. float distanceX = centerA - centerB; float minDistanceX = halfWidthA + halfWidthB; // If we are not intersecting at all, return (0, 0). if (Math.Abs(distanceX) >= minDistanceX) return 0f; // Calculate and return intersection depths. return distanceX > 0 ? minDistanceX - distanceX : -minDistanceX - distanceX; }
/// <summary> /// Resolves the collision in a region between this object /// and another. /// </summary> /// <param name="other">The other object being collided with.</param> /// <param name="region">The region of collision.</param> public virtual void Collide(GameObject other, BBox region) { }
/// <summary> /// Whether or not this object should collide with another object. /// </summary> /// <param name="other">The other object to check for a collision /// against.</param> /// <param name="region">The region of collision. An empty bounding box /// if the objects do not collide.</param> /// <returns>True if the objects should collide, false otherwise.</returns> internal bool ShouldCollide(GameObject other, out BBox region) { region = BBox.Intersect(other.BBox); return !region.IsEmpty(); }
public BBox Intersect(BBox other) { return new BBox(Rectangle.Intersect(box, other.box)); }
/** * Handle collision events. **/ public override void Collide(GameObject other, BBox region) { if (!HasCollided && (!other.Equals(owner))) { OnCollision(); /**base.Collide(other, region);**/ } }