# Circle Collision Sliding Response?

## Recommended Posts

kappa1    114
i'm currently having a problem trying to get 2d circle collision response working, what i need is for the 2d circles to slide off each other when they collide (not bounce). for circle collision check i'm doing if ((circle1.x - circle2.x) * (circle1.x - circle2.x) + (circle1.y - circle2.y) * (circle1.y - circle2.y) < circle1.radius * circle2.radius) the circles also have a velocity at which they move, what i need to do, is get the circles that collide to side of each other then continue moving to the destination they were going to, any help on a solutin would be appreciated? thanks

##### Share on other sites
jyk    2094
You might try something like this (typed into the post, so could have errors):
Vector2 diff = circle2.pos - center1.pos;float dist = diff.lengthSquared();float r = circle1.radius + circle2.radius;if (dist < r * r) {    dist = std::sqrt(dist);    diff /= dist;    float offset = (r - dist) * 0.5f;    circle1.pos += diff * offset;    circle2.pos -= diff * offset;}
I suppose the chances are low that I got all that right, but you might try it. This should work as long as the circles aren't moving too fast. They won't slide off each other if they happen to hit exactly head on, but they wouldn't in real life either (and in any case this is probably unlikely to happen).

##### Share on other sites
kappa1    114
thx for the reply, unfortuntly code doesn't work as hoped, when the circles touch they start shaking like crazy and sometimes disappear of the screen :)

another thing code above doesn't take velocity into account, so if once circle is stationary the moving one should slide of it and the stationary one should remain where it is, thx for the help anyway, any more pointers would be appreciated?

thanks

##### Share on other sites
jyk    2094
Quote:
 Original post by kappa1thx for the reply, unfortuntly code doesn't work as hoped, when the circles touch they start shaking like crazy and sometimes disappear of the screen :)
Well, I know the algorithm works as advertised because I've used it often, but maybe I messed something up when I typed it into the post. Anyway, sorry it didn't work - maybe someone else can spot the error. (Also, you could post your implementation - perhaps you introduced an error when integrating it into your code.)
Quote:
 another thing code above doesn't take velocity into account, so if once circle is stationary the moving one should slide of it and the stationary one should remain where it is, thx for the help anyway, any more pointers would be appreciated?
If it's a simple matter of one circle being stationary and immovable, you can simply handle the offset differently - that really shouldn't be a problem. If you want something that is dependent on velocity, mass, and other physical characteristics, then you're getting out of the realm of ad hoc solutions and into the area of physical simulation (which is a bit more complicated).

##### Share on other sites
kappa1    114
thx for the reply heres my implementation of the code above

float xdiff = circle2.x - circle1.x;
float ydiff = circle2.y - circle1.y;

float dist = (float) (xdiff*xdiff + ydiff*ydiff);

if (dist < circle1.radius * circle2.radius) {

dist = (float) Math.sqrt(dist);

xdiff /= dist;
ydiff /= dist;

float offset = (circle1.radius - dist) * 0.5f;

circle1.x += xdiff * offset;
circle1.y += ydiff * offset;

circle2.x -= xdiff * offset;
circle2.y -= ydiff * offset;
}

##### Share on other sites
jyk    2094
Quote:
 Original post by kappa1thx for the reply heres my implementation of the code abovefloat xdiff = circle2.x - circle1.x;float ydiff = circle2.y - circle1.y;float dist = (float) (xdiff*xdiff + ydiff*ydiff); if (dist < circle1.radius * circle2.radius) { dist = (float) Math.sqrt(dist); xdiff /= dist; ydiff /= dist; float offset = (circle1.radius - dist) * 0.5f; circle1.x += xdiff * offset; circle1.y += ydiff * offset; circle2.x -= xdiff * offset; circle2.y -= ydiff * offset;}
Yeah, that's not right. This sort of thing will be easier to implement once you have a proper vector class, but meanwhile take a more careful look at what I posted and compare it to your implementation; there are a couple of things that you copied incorrectly.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account