Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
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

1 Answer 1

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. :)

share|improve this answer
    
Yeah it's ok I commented on it like not that long ago :P –  RandyGaul Aug 1 '13 at 7:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.