# Problems with moving 2D circle/box collision detection

This is my first game ever and I'm a newbie in computer physics. I've got this code for the collision detection and it works fine for BOTTOM and TOP collision.It miss the collision detection with the paddle's edge and angles so I've (roughly) tried to implement it.

Main method that is called for bouncing, it checks if it bounce with wall, or with top (+ right/left side) or with bottom (+ right/left side):

protected void handleBounces(float px, float py) {
handleWallBounce(px, py);
if(mBall.y < getHeight()/4){
if (handleRedFastBounce(mRed, px, py)) return;
if (handleRightSideBounce(mRed,px,py)) return;
if (handleLeftSideBounce(mRed,px,py)) return;
}
if(mBall.y > getHeight()/4 * 3){
if (handleBlueFastBounce(mBlue, px, py)) return;
if (handleRightSideBounce(mBlue,px,py)) return;
if (handleLeftSideBounce(mBlue,px,py)) return;
}
}


This is the code for the BOTTOM bounce:

protected boolean handleRedFastBounce(Paddle paddle, float px, float py) {
if (mBall.goingUp() == false)
return false;

// next position
tx = mBall.x;

// actual position
ptx = px;

xc = tx + (tx - ptx) * dyp / (ty - pty);

{

mBall.x = xc;
increaseDifficulty();
return true;
}
else return false;
}


As long as I understood it should be something like this:

So I tried to make the "left side" and "right side" bounce method:

protected boolean handleLeftSideBounce(Paddle paddle, float px, float py){

// next position
ty = mBall.y;

// actual position
pty = py;

yc = ty + (pty - ty) * dyp / (ptx - tx);

System.out.println("left side bounce1");
System.out.println("yc: " + yc + "top: " + paddle.getTop() + " bottom: " + paddle.getBottom());
System.out.println("left side bounce2");
mBall.y = yc;
increaseDifficulty();
return true;
}
}
return false;
}


I think I'm quite near to the solution but I'm having big troubles with the new "yc" formula. I tried so many versions of it but since I don't know the theory behind it I can't adjust for the Y axis. Since the Y axis is inverted I even tried this: yc = ty - (pty - ty) * dyp / (ptx - tx);

I tried Googling it but I can't seem to find a solution for it. Also this method fails when ball touches the angle and I don't think is a nice way because it just test "one" point of the ball and probably there will be many cases in which the ball won't bounce.

• The question is verbose and hard to understand. I'd like to help but I don't want to spend 30 minutes reading your post :( – RandyGaul Jun 18 '13 at 19:54
• Did you figure this out? – ashes999 Dec 29 '13 at 11:43

Well here it goes. It seems your trying to account for different collisions individually. Which, instead, you should just have one robust circle-rectangle collision code. And use your velocities and positions to determine which paddle has been collided with.

This method will return whether a collision has occurred. It CAN detect collisions with the rectangles corners.

public bool Intersects(Rectangle rect){
float x = clamp(position.x, rect.x, rect.x + rect.w);
float y = clamp(position.y, rect.y, rect.y + rect.h);
return distance(position, Vector2(x, y)) < radius;
}


Then, when collision occurs, you can invert the ball.vel.x, or if you have paddles on the top and bottom, then the ball.vel.y.

I'm sorry if none of this helped, but I'm lost as to exactly what your looking for.

I'll be happy to help if you have anymore questions. :) Good luck!

-Mike

EDIT Annnnd I just realized I totally necro'd this post. Sorry. :)

• Yeah it's ok I commented on it like not that long ago :P – RandyGaul Aug 1 '13 at 7:55