Am I getting closer?

This is a discussion on Am I getting closer? within the Game Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    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;
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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);
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    Yes, I was guessing and still am.
    Why does an absolute value have to be used? You know any good links on this topic?
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    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.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903

    Re: Am I getting closer?

    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;
    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
    Last edited by jverkoey; 03-16-2003 at 07:42 PM.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    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.
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: Re: Am I getting closer?

    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 )

    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.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    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.
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global scope and passing pointer
    By Dave++ in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2007, 03:52 PM
  2. the misterious case of the non-migrating function...
    By taguato in forum Linux Programming
    Replies: 9
    Last Post: 09-07-2004, 09:18 PM
  3. Replies: 4
    Last Post: 11-12-2002, 05:26 AM
  4. program closer
    By canine in forum Windows Programming
    Replies: 2
    Last Post: 01-23-2002, 05:21 PM
  5. effects echoing closer to home
    By iain in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 09-15-2001, 11:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21