Thread: trouble understanding collision detection code

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    167

    trouble understanding collision detection code

    I am trying to decipherandre lamothe's freakout demo but im not getting this snippet of code

    Code:
     
    // test of the ball has hit the paddle
    if (ball_y > (SCREEN_HEIGHT/2) && ball_dy > 0)
       {
       // extract leading edge of ball
       int x = ball_x+(BALL_SIZE/2);
       int y = ball_y+(BALL_SIZE/2);
    
       // test for collision with paddle
       if ((x >= paddle_x && x <= paddle_x+PADDLE_WIDTH) &&
           (y >= paddle_y && y <= paddle_y+PADDLE_HEIGHT))
           {
           // reflect ball
           ball_dy=-ball_dy;
    
           // push ball out of paddle since it made contact
           ball_y+=ball_dy;
    I am referring to the test for collision with the paddle. I don't understand how x can be greater than paddle_x and less than paddle_x + PADDLE_WIDTH at the same time. PADDLE_X is 32. Help!!!!!!
    silk.odyssey

  2. #2
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    Its simple math, if you dont get it then there are two conclusions.
    You need to return to elementry school math class, and
    Game programming isn't for you.

    Assume paddle_width is 20
    assume X is 40
    32 < 40 < (32+20)
    32 < 40 < 52

    Infact, the numbers 33 through 51 are both greater then paddle_x and less then paddle_x + paddle_width
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    167
    LOL

    I see it now, don't know why i didn't before it seems so obvious now .

    You need to return to elementry school math class
    Well im not that great with math so maybe I should

    Game programming isn't for you.
    Well im not going to give up on the second day or whenever I have a little problem.
    silk.odyssey

  4. #4
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    Quote Originally Posted by silk.odyssey
    Well im not that great with math so maybe I should
    Yes you should double up on your math skills. The snippet you posted is the SIMPLE collision for 2d space. Things get a lot more complicated in a three dimensional system. I suggest you go to www.gamedev.net have a loook at their book list. There is one called math for game programming, or something along those lines that you would benifit from.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    That collision detection code won't work with high speed objects.

    What you need is to do is compute the time at which the two objects collide. If the interval is empty then there is no collision in the next frame.

    Draw the problem out on a piece of paper.


    4 cases here:
    [*] = moving box -> Box A
    [/] = stationary box -> Box B
    --- = number line

    This sample is taken from 3D Math Primer for Graphics and Game Development, Fletcher Dunn and Ian Parberry

    Code:
    t=0   (-x)[***]------[/////////]--------(+x)
    t=t.e (-x)------[***][/////////]--------(+x)
    hit	(-x)------------[/[***]/]--------(+x)
    t=t.l  (-x)------------[/////////][***]--(+x)
    t=1   (-x)------------[/////////]---[***](+x)
    At t=0 box A is completely on the left side of box B.
    At t=1 box A is completely on the right side of box B.

    t.e(tenter)=point at which box A begins to overlap box B

    t.l(tleave)=point at which box A no longer overlaps box B

    d is the component of the displacement vector, in this case x.
    t is a time on the number line.

    For the moving box or box A.
    Amin(t)=left side of box A
    Amax(t)=right side of box A.

    Amin(t)=Amin(0)+td
    Amax(t)=Amax(0)+td

    For the stationary box or box B
    Bmin(t)=Bmin(0)+td
    Bmax(t)=Bmax(0)+td

    t.e=tenter
    t.l=tleave

    At point of collision:

    Amax(t.e)=Bmin
    Amax(0)+(t.e)d=Bmin
    (t.e)d=Bmin-Amax(0)
    (t.e)=Bmin-Amax(0)/d

    Now solve for tleave:

    Amin(t.l)=Bmax
    Amin(0)+(t.l)d=Bmax
    (t.l)d=Bmax-Amin(0)
    (t.l)=Bmax-Amin(0)/d

    • If denominator is zero then boxes always overlap or never overlap
    • If box A begins on right side of box B then tenter will be greater than tleave -> swap their values prior to testing
    • If values of tenter and tleave are outside of range 0...1 then: if tenter>1 or tleave<0 there is no overlap and the boxes never collide
    • If the interval is empty then the boxes never collide.
    • If the interval is outside of the range 0...1 then there is no collision in the period of time being tested against



    Apply this for your second dimension, namely y, and you will have a way to test for any type of collision in a 2D environment. Notice we are testing time here, not against coordinates which can skip over one another.

  6. #6
    Registered User MicroFiend's Avatar
    Join Date
    Nov 2002
    Posts
    80
    lol poor bloke, he said he only just started
    You can stop change as easily as u can drink the sea with a fork

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2009, 08:37 PM
  2. Quad equation for collision detection?
    By joeprogrammer in forum Game Programming
    Replies: 1
    Last Post: 06-09-2006, 10:59 PM
  3. Ray tracer and collision detection
    By hdragon in forum Game Programming
    Replies: 31
    Last Post: 01-19-2006, 11:09 AM
  4. Collision with quads?
    By SyntaxBubble in forum Game Programming
    Replies: 6
    Last Post: 01-18-2002, 06:17 PM
  5. collision detection
    By tHaPuTeR in forum Game Programming
    Replies: 11
    Last Post: 09-22-2001, 10:53 AM