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
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