Obj. C problem

Posts: 2
Joined: 2010.12
Post: #1
So I just started out coding for the iPhone. I'm reading from a book and building a brick breaker game. Everything is going smooth so far, I've taken C++ before so I understanding most of it. There is just one part I need clarification on and want to know if you guys can help.

//This is for the collision of the ball and the paddle
ball.center.y <= paddle.center.y + 16

I have a few of these and want to know briefly what it means. I think I understand most of it, I just don't know what the + 16 is for, and the other lines of this part of the code also have something similar with a + 32 and a -32. I'm guess these numbers are the pixels, but I'm still kind of confused.
Quote this message in a reply
Posts: 625
Joined: 2007.09
Post: #2

The 16 is used for collision detection. Without the '+16,' your ball would have no edge - your ball would only bounce off the paddle when the center of the ball hit the center of the paddle. Thus, the '16' represents the radius of your ball. If you change the 16 to 20, the ball will be bigger - if you change it to 10, it will be smaller.

As for what units you're using, I'd have to see the book, but I'd imagine its referencing 16 pixels.

- Lincoln Green
Quote this message in a reply
Posts: 2
Joined: 2010.12
Post: #3
oh alright that makes sense.

So what about for the -32 or -16? and the less than or greater than? I wish they wouldve clarified it a bit more in the book. But yes they are referring to pixels. Thanks for replying it really helped. Smile This is the code snippet of the paddle collision section I just dont understand why they minus 16 here and 32 there. The book says one section check for the ball hitting the paddle and the other check that the ball has not gotten stuck inside the paddle.

BOOL paddleCollision = ball.center.y >= paddle.center.y - 16 &&
ball.center.y <= paddle.center.y + 16 &&
ball.center.x > paddle.center.x - 32 &&
ball.center.x < paddle.center.x + 32;

if(paddleCollision) {
ballMovement.y = -ballMovement.y;

if (ball.center.y >= paddle.center.y - 16 && ballMovement.y < 0){
ball.center = CGPointMake(ball.center.x, paddle.center.y - 16);
} else if (ball.center.y <= paddle.center.y + 16 && ballMovement.y > 0){
ball.center = CGPointMake(ball.center.x, paddle.center.y + 16);
}else if (ball.center.x >= paddle.center.x - 32 && ballMovement.x < 0){
ball.center = CGPointMake(paddle.center.x - 32, ball.center.y);
}else if (ball.center.x <= paddle.center.x + 32 && ballMovement.x > 0) {
ball.center = CGPointMake(paddle.center.x + 32, ball.center.y);
Quote this message in a reply
⌘-R in Chief
Posts: 1,277
Joined: 2002.05
Post: #4
16 would be the ball's radius and half of the height of the paddle. 32 should be half of the paddle's width minus the radius of the ball. I hope the code that comes with the book uses constants. Those "magic numbers" are poor style.

"paddleCollison = ..." checks for x and y intersection of the ball and the paddle.

The if statements after simply move the ball's bounds outside of the paddle's bounds. It's resolving the fact that the ball shouldn't ever be *inside* of the paddle, it should bounce off. The reason it checks for ballMovement being positive or negative is to know which side of the paddle the ball should be moved to (above or below, left or right).
Quote this message in a reply
Post Reply