# Correct velocity on collision between two rects

I have two moving objects. The objects consist of a velocity vector2, a Rectangle and a vector2 as position (origin in the middle of the rect).

Some rough pseudo code for my collision checking function:

foreach (object1 in objects)
foreach (object2 in objects)
intersectionRect = Rectangle.Intersect(object1.Rectangle, object2.Rectangle)
if (intersectionRect != Rectangle.Empty) // If colliding
ResolveCollision(object1, object2, intersectionRect) // does nothing currently
object1.position += object1.velocity
object2.position += object2.velocity


My problem now is how to do the collision resolution. I need help figuring out a formula to calculate the new velocities of the objects so that they only move as far as they need to not intersect anymore.

I have used google to find a solution, but the most stuff I found was how to do collisions and not how to resolve them.

• Your current code won't work btw, use brackets – Bálint Oct 11 '16 at 8:52
• Umm, that's kinda the point of pseudo-code: it doesn't have to work. – Tyyppi_77 Oct 11 '16 at 11:38

I need help figuring out a formula to calculate the new velocities of the objects so that they only move as far as they need to not intersect anymore.

The easiest answer to this is to change strategy from checking if they are colliding right now to check "if I make this move, will it be a collision?" and if so, don't allow the move.

If you don't want to that you will need to calculate penetration depth(this can be somewhat awkward to google..) and then separate the boxes based on this. But essentially calculate how far into box A box B is and adjust position away from the point of intersection.

When @boredatwork mentioned penetration depth it reminded me of a method I saw in one of the XNA samples a while back. It might be useful here:

    /// <summary>
/// Calculates the signed depth of intersection between two rectangles.
/// </summary>
/// <returns>
/// The amount of overlap between two intersecting rectangles. These
/// depth values can be negative depending on which wides the rectangles
/// intersect. This allows callers to determine the correct direction
/// to push objects in order to resolve collisions.
/// If the rectangles are not intersecting, Vector2.Zero is returned.
/// </returns>
public Vector2 IntersectionDepth(RectangleF other)
{
// Calculate half sizes.
var thisHalfWidth = Width / 2.0f;
var thisHalfHeight = Height / 2.0f;
var otherHalfWidth = other.Width / 2.0f;
var otherHalfHeight = other.Height / 2.0f;

// Calculate centers.
var centerA = new Vector2(Left + thisHalfWidth, Top + thisHalfHeight);
var centerB = new Vector2(other.Left + otherHalfWidth, other.Top + otherHalfHeight);

// Calculate current and minimum-non-intersecting distances between centers.
var distanceX = centerA.X - centerB.X;
var distanceY = centerA.Y - centerB.Y;
var minDistanceX = thisHalfWidth + otherHalfWidth;
var minDistanceY = thisHalfHeight + otherHalfHeight;

// If we are not intersecting at all, return (0, 0).
if (Math.Abs(distanceX) >= minDistanceX || Math.Abs(distanceY) >= minDistanceY)
return Vector2.Zero;

// Calculate and return intersection depths.
var depthX = distanceX > 0 ? minDistanceX - distanceX : -minDistanceX - distanceX;
var depthY = distanceY > 0 ? minDistanceY - distanceY : -minDistanceY - distanceY;
return new Vector2(depthX, depthY);
}


Keep in mind this is in the context of my RectangleF class but it should give you the right idea.