Nez.Portable/Utils/Extensions/RectangleExt.cs
/// <summary>
/// returns true if the boxes are colliding
/// moveX and moveY will return the movement that b1 must move to avoid the collision
/// </summary>
/// <param name="other">Other.</param>
/// <param name="moveX">Move x.</param>
/// <param name="moveY">Move y.</param>
returns true if the boxes are colliding
moveX and moveY will return the movement that b1 must move to avoid the collision
returns true if the boxes are colliding moveX and moveY will return the movement that b1 must move to avoid the collision
public static bool collisionCheck(ref Rectangle rect, ref Rectangle other, out float moveX, out float moveY)
        {
            moveX = moveY = 0.0f;
            var l = other.X - (rect.X + rect.Width);
            var r = (other.X + other.Width) - rect.X;
            var t = other.Y - (rect.Y + rect.Height);
            var b = (other.Y + other.Height) - rect.Y;
            if (l > 0 || r < 0 || t > 0 || b < 0)
                return false;
            moveX = Math.Abs(l) < r ? l : r;
            moveY = Math.Abs(t) < b ? t : b;
            if (Math.Abs(moveX) < Math.Abs(moveY))
                moveY = 0.0f;
            else
                moveX = 0.0f;
            return true;
        }
public static bool collisionCheck ( ref Rectangle rect , ref Rectangle other , out float moveX , out float moveY ) { moveX = moveY = 0.0f ; var l = other . X - ( rect . X + rect . Width ) ; var r = ( other . X + other . Width ) - rect . X ; var t = other . Y - ( rect . Y + rect . Height ) ; var b = ( other . Y + other . Height ) - rect . Y ; if ( l > 0 || r < 0 || t > 0 || b < 0 ) return false ; moveX = Math . Abs ( l ) < r ? l : r ; moveY = Math . Abs ( t ) < b ? t : b ; if ( Math . Abs ( moveX ) < Math . Abs ( moveY ) ) moveY = 0.0f ; else moveX = 0.0f ; return true ; }
returns true if the boxes are colliding
moveX and moveY will return the movement that b1 must move to avoid the collision
returns true if the boxes are colliding moveX and moveY will return the movement that b1 must move to avoid the collision
// check that there was a collision // find the offset of both sides // only use whichever offset is the smallest
Rectangle
ref
float
out
other
None
moveX
None
moveY
None