Beispiel #1
53
        /// 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;
        }
Beispiel #2
4
        /// 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;
        }
Beispiel #3
1
        /// <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) {

        }
Beispiel #4
1
 /// <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();
 }
Beispiel #5
1
 public BBox Intersect(BBox other)
 {
     return new BBox(Rectangle.Intersect(box, other.box));
 }
Beispiel #6
1
 /**
  * Handle collision events.
  **/
 public override void Collide(GameObject other, BBox region)
 {
     if (!HasCollided && (!other.Equals(owner)))
     {
         OnCollision();
         /**base.Collide(other, region);**/
     }
 }