# Circle vs. Rect Collision accuracy problem

The blue ball is dynamic and is supposed to bounce off the walls, as well as the rectangle. Currently, the green rectangle is adding an invisible line to the scene, which doesn't let my ball go where I want it to. You can see the issue on the picture below and also see my source code.

Source code for dynamic circle vs. stationary rectangle collision:

public void collisionRect() {
double dx = Math.abs(circle1.getLayoutX() - rect1.getLayoutX());
double dy = Math.abs(circle1.getLayoutY() - rect1.getLayoutY());
double rect1XRadius = rect1.getWidth() / 2;
double rect1YRadius = rect1.getHeight() / 2;

boolean xWalls = dx <= rect1XRadius;
boolean yWalls = dy <= rect1YRadius;

if (xWalls) { //if the ball hits any of the X walls
c1SpeedX = c1SpeedX * -1; //Invert direction of X
}
if (yWalls) { //if the ball hits any of the Y walls
c1SpeedY = c1SpeedY * -1; //Invert direction of Y
}

}


I don't understand where I've made a mistake and how I can make my code work. Source code examples will be greatly appreciated! Thanks.

• Anything unclear in the question? Please let me know, I really want to figure this out. Nov 25, 2013 at 21:32
• possible duplicate gamedev.stackexchange.com/questions/63421/… Nov 26, 2013 at 5:19