# Determining Rectangle-Circle Collision Point

I'm developing a 2D game engine currently. I stuck on determining position of a rectangle that collides with a circle when its speed is added to its position. Rectangles have only x axis speed.

On picture rectangles are same. Left sided rectangles x,y,width and height values are known. Second rectangle should be next position. Circles radius, x and y values are known also. (x and y values are top left position of an object). I need to find second position of rectangle.

Edit: I can detect collision perfectly. But I need its new position.

• To answer this in a way that is useful to you, I would like to see how you are detecting the collision. Some of the values you already have calculated may be used to find the new position. Also, it may be easier to show you a method if we can use the same variables, style, and programming language. – Victor T. Mar 7 '17 at 18:12
• Are the rectangles axis alignes? – Bálint Mar 7 '17 at 18:13
• Simply i check distance between rectangle and circle and compare it with some variables. Like = float distX = Math.abs(circleMidX - rectangleMidX ); if(distX > (rectangle.width / 2 + circle.radius)) // No Collision @VictorT. – Barış Doğa Yavaş Mar 7 '17 at 18:28
• Yes y pozition never changes @Bálint – Barış Doğa Yavaş Mar 7 '17 at 18:30
• That isn't what axis alignes means, an axis aligned rectangle can't rotate – Bálint Mar 7 '17 at 18:33

Because y doesn't change and the rectangle is axis aligned we have 3 cases:

• The center of the circle is below the rectangle
• The center of the circle is aligned with the rectangle
• The center of the circle is above the rectangle.

For each case you first need to check whether the rectangle collides with the circle or not. You should create a bounding box from the last and current place of the rectangle and check if the circle collides with this rectangle to avoid fake negatives when the rectangle goes too fast. Here's an image to make it clearer:

Here ABC'D' is the bounding box.

After you're certain there's a collision, you need to check which above case it belongs to. If the bottom of the rectangle is above the center, then the first, if the top is above but the bottom is below, then it's the second case, and if the top is below the center, then it's the third.

If it's the first or the third case and the rectangle goes right (it has a positive velocity), then you need to set the center's x coordinate based on the algorithm:

newCenterX = circleCenterX - sqrt(r * r - pow(centerY - circleCenterY, 2)) - width / 2


where r is the radius of the circle.

If the rectangle is goes left (it has a negative x velocity), then the new center's x coordinate is

newCenterX = circleCenterX + sqrt(r * r - pow (centerY - circleCenterY, 2)) + width / 2


If it's the second case and the rectangle goes right, then the new center x is

newCenterX = circleCenterX - r - width / 2


And if it goes to left, the new x is

newCenterX = circleCenterX + r + width / 2