Breakout ball collision detection, bouncing against the walls

Posted by Sri Harsha Chilakapati on Game Development See other posts from Game Development or by Sri Harsha Chilakapati
Published on 2012-07-06T00:20:19Z Indexed on 2012/07/06 3:24 UTC
Read the original article Hit count: 303

I'm currently trying to program a breakout game to distribute it as an example game for my own game engine. http://game-engine-for-java.googlecode.com/

But the problem here is that I can't get the bouncing condition working properly. Here's what I'm using.

public void collision(GObject other){
    if (other instanceof Bat || other instanceof Block){
        bounce();
    } else if (other instanceof Stone){
        other.destroy();
        bounce();
    }
    //Breakout.HIT.play();
}

And here's by bounce() method

public void bounce(){
    boolean left = false;
    boolean right = false;
    boolean up = false;
    boolean down = false;
    if (dx < 0) {
        left = true;
    } else if (dx > 0) {
        right = true;
    }
    if (dy < 0) {
        up = true;
    } else if (dy > 0) {
        down = true;
    }
    if (left && up) {
        dx = -dx;
    }
    if (left && down) {
        dy = -dy;
    }
    if (right && up) {
        dx = -dx;
    }
    if (right && down) {
        dy = -dy;
    }
}

The ball bounces the bat and blocks but when the block is on top of the ball, it won't bounce and moves upwards out of the game.

What I'm missing? Is there anything to implement? Please help me..

Thanks

© Game Development or respective owner

Related posts about java

Related posts about game-design