# Am I getting closer?

• 03-15-2003
lambs4
Am I getting closer?
I'm still having difficults writting the collision code for my breakout clone. The right side isn't colliding.

left
Code:

```if(ball.x+ball.width >= blocks[x][y].x &&   ball.x+ball.width <= blocks[x][y].x+blocks[x][y].width/2 &&   ball.y > blocks[x][y].y &&   ball.y <= blocks[x][y].y+blocks[x][y].height)       ball.dx = -ball.dx;```
right
Code:

```if(ball.x+ball.width >= blocks[x][y].x+blocks[x][y].width/2 &&   ball.x <= blocks[x][y].x+blocks[x][y].width &&   ball.y > blocks[x][y].y &&   ball.y <= blocks[x][y].y+blocks[x][y].height)       ball.dx = ball.dx;```
• 03-16-2003
Magos
Quote:

ball.dx = ball.dx;
Exactly what does this line do? Nothing at all!

I guess you meant this:

Left bounce:
ball.dx = -abs(ball.dx);

Right bounce:
ball.dx = abs(ball.dx);
• 03-16-2003
lambs4
Yes, I was guessing and still am.
Why does an absolute value have to be used? You know any good links on this topic?
• 03-16-2003
no-one
because less than zero is left and greater than is right, so abs() is a guarantee that movement is greater than or equal to zero or right, and not left.
• 03-16-2003
jverkoey
Re: Am I getting closer?
Quote:

Originally posted by lambs4
I'm still having difficults writting the collision code for my breakout clone. The right side isn't colliding.

left
Code:

```if(ball.x+ball.width >= blocks[x][y].x &&   ball.x+ball.width <= blocks[x][y].x+blocks[x][y].width/2 &&   ball.y > blocks[x][y].y &&   ball.y <= blocks[x][y].y+blocks[x][y].height)       ball.dx = -ball.dx;```
right
Code:

```if(ball.x+ball.width >= blocks[x][y].x+blocks[x][y].width/2 &&   ball.x <= blocks[x][y].x+blocks[x][y].width &&   ball.y > blocks[x][y].y &&   ball.y <= blocks[x][y].y+blocks[x][y].height)       ball.dx = ball.dx;```

Quote:

Originally posted by Magos
Exactly what does this line do? Nothing at all!

I guess you meant this:

Left bounce:
ball.dx = -abs(ball.dx);

Right bounce:
ball.dx = abs(ball.dx);

or, you know, lmao, you could just combine both if statements, like so:

Code:

```if((ball.x+ball.width >= blocks[x][y].x && ball.x+ball.width <= blocks[x][y].x+blocks[x][y].width/2 && ball.y > blocks[x][y].y && ball.y <= blocks[x][y].y+blocks[x][y].height)) || (ball.x+ball.width >= blocks[x][y].x+blocks[x][y].width/2 && ball.x <= blocks[x][y].x+blocks[x][y].width && ball.y > blocks[x][y].y && ball.y <= blocks[x][y].y+blocks[x][y].height))       ball.dx = -ball.dx;```
-edit-
erk, had to fix all that code, sheesh, it was extending halfway to China :)

and that would simplify everything, because when you do ball.dx=-ball.dx; it is merely flipping the variable in a sense. Take this in to account:

int a=45;

a=-a;

what does a equal now? 45, because --45 is positive 45, because the two negatives cancel out and become a positive, now this:

int a=-34;

a=-a;

now a equals 34, because it is flipped again. There's no point in having an abs() function in there.....(i'm not flaming)

hope that helps
• 03-17-2003
lambs4
Quote:

Originally posted by no-one
because less than zero is left and greater than is right, so abs() is a guarantee that movement is greater than or equal to zero or right, and not left.
Thanks, I now understand.
• 03-17-2003
Magos
Re: Re: Am I getting closer?
Quote:

Originally posted by jverkoey
int a=45;

a=-a;

what does a equal now? 45, because --45 is positive 45, because the two negatives cancel out and become a positive

I don't know what kind of abstract mathematics you're using, but i get that to -45, not 45 :).

(I know, I know, it was most likely a typo... couldn't help it :p )

Quote:

Originally posted by jverkoey
There's no point in having an abs() function in there.....(i'm not flaming)

hope that helps

With abs(), the direction is guaranteed to be right (the direction right, not the right right... bah, you understand...:)). If you simply switch the direction on an overlap, and the ball doesn't 'leave' the block in the next frame it will be stuck inside the block. This will rarely happen when the ball's speed is constant and the block is not moving, but in other cases it will. This is simply a matter of reducing potential bugs.
• 03-20-2003
lambs4
Hey, thanks for the help. Now I have created a new problem. The paddle is divided into two sections right now. What changes would I need to divide it into three sections? left center and right.