Breakout Collision Detection Algorithm Help

This is a discussion on Breakout Collision Detection Algorithm Help within the Game Programming forums, part of the General Programming Boards category; I'm having trouble with my breakout clone. So far, for test purposes, I'm having the ball bounce off the boundaries ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    Ohio
    Posts
    37

    Question Breakout Collision Detection Algorithm Help

    I'm having trouble with my breakout clone.

    So far, for test purposes, I'm having the ball bounce off the boundaries of the window the game is played in (800 x 600). My current problem is that I can't get an effective boundary collision detection algorithm off the ground.

    My main problem is that I'm stumped with an effective way to start the ball moving. I can get the ball moving however I can't adapt the direction for when it collides with the boundary of the room. For instance, to narrow down the problem, I have the ball inheritly moving in a diagonally left direction toward the almost top left corner of the window. When the ball hits the left boundary, if it's going in an upward direction, then the only thing that really needs to change is the x direction right? If this is the case, how would I correctly adapt the new numbers to the ball? This is where I'm stumped at.

    If anyone can point me in the right direction, I'd greatly appreciate it. Thanks in advance.

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    24
    While I haven't reached the point where I can test this out myself, I believe having two signed variables for velocity (x and y) would do the trick. While this doesn't account for a change of speed that might result from the collision, it should be a good start.

    If x is negative, then the ball is travelling one way along the x axis, if x is positive it is travelling the other way and if it is zero it is stationary on the x axis. Same thing for y. Depending on the side impacted, you simply invert the velocity on the appropriate axis, eg

    Code:
    if(xsidehit()) {
      x = !x;
    }
    would change the direction on the x axis if a collision was detected on the left or right side (I'm assuming that you have set up collision boxes, if not, this link leads to a little introduction).

    http://www.gamedev.net/reference/art...article735.asp

    Some of the other articles could be of interest as well.

    EDIT: As was pointed out below, that won't yield the correct answer. I confused the ! operator (which returns true or false) with the 'inversion' operator (returns the negative value - negative of a negative is positive). At least I didn't get the assignment and equality operators confused.
    Last edited by ruthgrin; 08-24-2006 at 06:44 PM. Reason: Correcting an operator error

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Quote Originally Posted by ruthgrin
    Code:
    if(xsidehit()) {
      x = !x;
    }
    Could you tell me what you think !x is if x = 5?

    That is to say x is moving 5 points to the right for every point it moves on the y axis...
    Sent from my iPadŽ

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Code:
    void Game::CheckWalls()
    {
      if (m_pball.x>=walls.right || m_pball.x<=walls.left) m_pball.vx*=-1.0f;
      if (m_pball.y>=walls.bottom || m_pball.y<=walls.top) m_pball.vy*=-1.0f;
    
    }
    
    void Game::UpdateBall(float fFrameDelta)
    {
      m_pBall.x+=(m_pBall.vx*m_pBall.speed)*fFrameDelta;
      m_pBall.y+=(m_pBall.vy*m_pBall.speed)*fFrameDelta;
    }
    
    
    void Game::Setup(float angle,float speed,float cx,float cy)
    {
      m_pball->vx=cosf(angle);
      m_pball->vy=sinf(angle);
      m_pball->speed=speed;
      m_pball->x=cx;
      m_pball->y=cy;
    }
    If you want to use non-orthogonal walls you will have to collision detect and then if true multiply ball velocity vector by the normal vector of the wall.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > My main problem is that I'm stumped with an effective way to start the ball moving.
    Think of a velocity and an angle

    From which you calculate
    dx = velocity * cos(angle)
    dy = velocity * sin(angle)

    Now a simple bounce off a vertical wall would be
    dx = -dx;

    But to do that using angles and velocities requires a bit of calculation (just draw it out on paper, you should be able to figure out a simple bounce for the angle).

    When you've done that, then you can say
    newVelocity = velocity + ( rand() % 3 - 1 ); // velocity +/- 1
    newAngle = angle + ( rand() % 3 - 1 ); // angle +/- 1 degree
    Then calculate the new effective dx and dy until you hit the next wall.

    Finish off by having some range checks to limit how fast things can get
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Salem is correct for 2D walls. You will need a starting position and ending position (ending will be the contact point on the wall). Once you have these you can make a triangle and do some good old fashioned trig to get the resulting angle.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Location
    Ohio
    Posts
    37
    > My main problem is that I'm stumped with an effective way to start the ball moving.
    Think of a velocity and an angle

    From which you calculate
    dx = velocity * cos(angle)
    dy = velocity * sin(angle)
    In order to understand my new question, know that the ball starts off directly in the center of the bat and about 10 pixels above it.

    My question here is that I want to ball to start moving in a random angle but within a range of two angles. See I assume that if the ball starts moving directly up, it's moving at a 90 degree angle. The farthest to the left or right I would want it to start is 90 +- 45 degrees. So the range of angles I would want is from 45 degrees to 135 degress. How would I be able to get an angle between those ranges using the rand() function? I don't really want to have the ball move in one static direction everytime the game is played or a level is started.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    How to generate a rand within a range is in the FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Collision Detection Problems
    By Dark_Phoenix in forum Game Programming
    Replies: 1
    Last Post: 12-17-2006, 02:25 PM
  2. breakout style game collision detection
    By m00se123 in forum Game Programming
    Replies: 6
    Last Post: 10-31-2002, 08:18 AM
  3. bounding box collision detection
    By DavidP in forum Game Programming
    Replies: 7
    Last Post: 07-07-2002, 11:43 PM
  4. Replies: 4
    Last Post: 05-03-2002, 09:40 PM
  5. Collision detection algorithm
    By Hannwaas in forum Game Programming
    Replies: 5
    Last Post: 11-30-2001, 12:27 PM

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