This site requires JavaScript, please enable it in your browser!
Greenfoot back
Rubendg
Rubendg wrote ...

2017/10/27

Breakout: detect side collision between a ball and bricks

Rubendg Rubendg

2017/10/27

#
Hi, im doing a Breakout game for a class project. In the game, when the ball touches the left or right side of a brick, the balls just change his vertical speed and not his horizontal speed, so it doesnt bounce correctly. I have tried to fix it but i don't know how to do it, so every help or comment is really appreciated. This is the code that make the ball bounce when it touches a brick, but i cant make it to work correctly when it touches a side of the brick because i dont know how to detect when the ball is colliding with a side of the brick
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void reboteBarra() {
 
        Actor BloqueA = getOneIntersectingObject(BloqueA.class);
 
         
         
 
        if (BloqueA != null) {
            vel_y = -vel_y;
            score = score + 20;
            getWorld().showText("Puntuación = "+ score.toString(), 900, 20);
            Greenfoot.playSound("sonidos.wav");
             
        }
}
danpost danpost

2017/10/27

#
The simplest way to determine where the hit occurs on the brick is to separate the horizontal and vertical movements of the ball (do them individually, checking for collision after each). You will need another method like the one given above to deal with horizontal movement collisions. If you really want accurate brick-ball collision bouncing, check out the code to my Pong Pinball scenario.
You need to login to post a reply.