# Simple Collision Resolution between 2 moving objects

I have two circles. And they can move in any direction. Their movement is based on these values:

circle1.xSpeed & circle1.ySpeed


And the same for the second circle. Now I know when they collide with eachother:

var dx = this.x - other.x;
var dy = this.y - other.y;
var intersectDist = (this.scale + other.scale) - Math.sqrt(dx * dx + dy * dy);
if (intersectDist >= 0) {
// MOVE OBJECTS APART:
intersectDist /= 2;
this.x -= intersectDist * Math.cos(this.dir);
this.y -= intersectDist * Math.sin(this.dir);
other.x -= intersectDist * Math.cos(other.dir);
other.y -= intersectDist * Math.sin(other.dir);

}


Im not sure if that is the best way to move the objects apart, but the main issue is the resulting x and y speeds for each object. How can I calculate those?

• Hi, at work so don't have the time to write a proper answer (can get to it later) but this article gamasutra.com/view/feature/131424/… has a good explanation of how to resolve collisions. Aug 16 '16 at 11:52

A way I would usually handle this is: assuming that your objects have assigned masses which could be their dimensions or material or type base metrics. take both objects and calculate the force by multiplying their mass*acceleration "look up the acceleration formula"! after you have the force just check which has the greater force.

Example:

One = object one;

Two = object two;

XVelocity One = 4 pixels per frame;

XVelocity Two = 8 pixels per frame;

Mass One = Radius of Circle One which is = 20;

Mass two = Radius of Circle Two which is 16;

Force One = 4 * 20; use Velocity if no accelaration!!

Force Two = 8 * 16; use velocity if no accelaration!!

Force One = 80;

Force Two = 128;

EnergyLoss One = (Force One / Force Two)*XVelocity One = 2.5 = 0.25 "Put a zero in front"

EnergyLoss Two = (Force Two / Force One)*Xvelocity two = 12.8 = 0.12 "Put a zero in front"

New XVelocity One = XVelocityOne*Energyloss;

New XVelocity Two = XVelocityTwo*Energyloss;

New XVelocity One = (0.8/1.28)-4; New XVelocity two = (1.28/0.8)-8;

This process is not complete and the formulas could be adjusted but I hope this puts you on the right path for collision ressolution!

I usually use something like this to resolve my collisions! I hope this helps!