3 Replies Latest reply: Dec 26, 2009 11:43 PM by 843853

# Arkanoid collision detection

Hey, im writing my first game: arkanoid
I have some problems with detecting collision between paddle and the ball.
The method ballCollision checks if the ball comes against the paddle.. but it doens't work?!
The second method schiet or in english "shoot" checks if the ball comes against the walls and than bounces back and changes direction.
I would like to write one method that i can use for my bricks and for the paddle. But my first priority is the collision between paddle and ball.

Here is my code so far:
``````
class Bal implements Runnable{
...
public void ballCollision(){
if((getX()+getBreedte()>peddel.getX()) && (getX()<peddel.getX()+peddel.getBreedte())){
setDX(-getDX());
}else if((getY()+getHoogte()>peddel.getY()) && (getY()<peddel.getY()+peddel.getHoogte())){
setDY(-getDY());
}
}

public void schiet(){
boolean ok;

System.out.println("x: " + x);
System.out.println("Peddel x: " + peddel.getX());
System.out.println("y: " + y);
System.out.println("Peddel y: " + peddel.getY());
ballCollision();

if(x + dx > 780 || x + dx < 0){
//780 is de rechterkant x
setDX(-dx);

//            dx = -dx;
}
if( y + dx < 0){
// y + dx > 560 ||
//0 is de bovenkant y
//560 is de onderkant y
//            dy = -dy;
setDY(-dy);
}
x+=dx;
y+=dy;
}
}..``````
Every help is apreciated

Edited by: Feyeuh on Dec 26, 2009 8:27 AM

Edited by: Feyeuh on Dec 26, 2009 8:29 AM

Edited by: Feyeuh on Dec 26, 2009 8:29 AM

Edited by: Feyeuh on Dec 26, 2009 9:32 AM

Edited by: Feyeuh on Dec 26, 2009 9:33 AM
• ###### 1. Re: Arkanoid collision detection
Feyeuh wrote:
``````public void ballCollision(){
if((getX()+getBreedte()>peddel.getX()) && (getX()<peddel.getX()+peddel.getBreedte())){
setDX(-getDX());
}else if((getY()+getHoogte()>peddel.getY()) && (getY()<peddel.getY()+peddel.getHoogte())){
setDY(-getDY());
}
}``````
As far as I can see you don't have any Y factor into your formula, so you're going to have columns of paddle collision from the top to the bottom of your screen depending on your paddle position. You also need to take into account "=" in your formulas and use "<=" and ">=" in your comparisons.

When you take Y into account in your formulas, you need to consider the thickness of your paddles and also your movement rate: ie, does the paddle lay along the path of your ball within 1 move, it is does, then you have to consider that it "hit" the ball. If your movement rate is more than the thickness of your paddle, then you may get into a "behind the paddle", but it should have been a hit scenario.
• ###### 2. Re: Arkanoid collision detection
I recently changed to with = <> :
``````   public void balCollision(){
System.out.println("COLLISION");
setDY(-getDY());
}
}``````
My code in words: if( x-coord bal >= paddle x) && ( x-coord bal <= paddle coord x + the length of the paddle) && ( y-coord bal <= paddle get y coord) && (y-coord bal >= y-coord paddle - lengte y paddle)

My dx and dy sets the direction of the balls movement.
getX and getY (gets x,y coordinates of the ball)

My paddle = 10 width and i use is in peddel.getWidth())
My movement rate is static(12):
``````    public void run(){
while (doorgaan) {
//first draw then sleep and move
panel.repaint();
sleep(12);
schiet();
}
}``````
What do you mean with Y factor? Didn't i used Y and paddle width(thickness)
How do i use the speed with it..?

Edited by: Feyeuh on Dec 26, 2009 1:48 PM

Edited by: Feyeuh on Dec 26, 2009 1:50 PM

Edited by: Feyeuh on Dec 26, 2009 1:50 PM
• ###### 3. Re: Arkanoid collision detection
In your OP your formula does not have any Y variable dependencies, so if your conditions ever become true, they will be true all the way down the page--from Y=0 to Y=ScreenHeight -1.

Could you give a brief description of what your code actually does, I think we all know what you would like it to do, but could you tell us what it does now?