Problems with moving 2D circle/box collision detection

Posted by dario3004 on Game Development See other posts from Game Development or by dario3004
Published on 2012-04-21T17:46:25Z Indexed on 2013/10/30 10:25 UTC
Read the original article Hit count: 312

Filed under:
|
|

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;
    ty = mBall.y - mBall.getRadius();

    // actual position
    ptx = px;
    pty = py - mBall.getRadius();

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

    if ((ty < paddle.getBottom() && pty > paddle.getBottom()
            && xc > paddle.getLeft() && xc < paddle.getRight()))
            {

        mBall.x = xc;
        mBall.y = paddle.getBottom() + mBall.getRadius();
        mBall.bouncePaddle(paddle);
        playSound(mPaddleSFX);
        increaseDifficulty();
        return true;
    }
    else return false;
}

As long as I understood it should be something like this: enter image description here

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

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

    // next position
    tx = mBall.x + mBall.getRadius();
    ty = mBall.y;

    // actual position
    ptx = px + mBall.getRadius();
    pty = py;

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

    if (ptx < paddle.getLeft() && tx > paddle.getLeft()){
        System.out.println("left side bounce1");
        System.out.println("yc: " + yc + "top: " + paddle.getTop() + " bottom: " + paddle.getBottom());
        if (yc > paddle.getTop() && yc < paddle.getBottom()){
            System.out.println("left side bounce2");
            mBall.y = yc;
            mBall.x = paddle.getLeft() - mBall.getRadius();
            mBall.bouncePaddle(paddle);
            playSound(mPaddleSFX);
            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.

© Game Development or respective owner

Related posts about 2d

Related posts about java