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.

Rectangle-Circle Collision Detection

  • \$\begingroup\$ 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. \$\endgroup\$
    – Victor T.
    Mar 7, 2017 at 18:12
  • \$\begingroup\$ Are the rectangles axis alignes? \$\endgroup\$
    – Bálint
    Mar 7, 2017 at 18:13
  • \$\begingroup\$ 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. \$\endgroup\$ Mar 7, 2017 at 18:28
  • \$\begingroup\$ Yes y pozition never changes @Bálint \$\endgroup\$ Mar 7, 2017 at 18:30
  • \$\begingroup\$ That isn't what axis alignes means, an axis aligned rectangle can't rotate \$\endgroup\$
    – Bálint
    Mar 7, 2017 at 18:33

1 Answer 1


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:

enter image description here

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .