# How to perform a sliding collision between a square and a circle?

Please point me to the duplicate if this has been asked before. I couldn't find it.

Basically, I am creating a 2d top-down game where the player is represented by a circle, and the tiles are represented by 16x16 squares. I have gotten collision detection working, such that the player cannot collide with tiles, by using the following algorithm:

let curPosition = player.position;
let newPosition = player.position + player.velocity;

if (canTouchPosition(newPosition.x, curPosition.y)) {
player.position.x = newPos.x;
}

if (canTouchPosition(curPosition.x, newPosition.y)) {
player.position.y = newPos.y;
}

if (canTouchPosition(newPosition.x, newPosition.y)) {
player.position.x = newPos.x;
player.position.y = newPos.y;
}


This works, except there's a problem. If the player is on the edge of a tile and moving to the right, I want them to slide past it, instead of getting stuck. Like so:

See how currently the circle is stuck on the corner when trying to move right? Instead I want it to slide along the corner until it is able to move freely, and then continue moving to the right. I can't quite figure this out. I figure a circle would make this easier, but if a square is instead easier to perform this operation on, maybe I'll use one of those instead? I mostly just want to get it working.

• Thanks for the links. This seems really complicated though? I can't imagine something like this is more than a few lines of code, no? – Ryan Peschel Mar 28 at 22:49
• It depends how general it needs to be. The two answers above aren't for your exact use case, but for more general situations that could include circles of different sizes or other obstacle shapes. Take the inspiration, then simplify to the parts you need. – DMGregory Mar 28 at 23:14
• Yeah the issue is that I'm having trouble understanding all the math in the answers I've seen, so I was looking for a more specific solution, so that I wouldn't be so overwhelmed. – Ryan Peschel Mar 28 at 23:16
• Displacing along the minimum separation vector might be enough for your needs then. You can get small glitches that way, but it is much simpler than a water-tight continuous collision solution. – DMGregory Mar 28 at 23:18