# trouble understanding collision detection code

This is a discussion on trouble understanding collision detection code within the Game Programming forums, part of the General Programming Boards category; I am trying to decipherandre lamothe's freakout demo but im not getting this snippet of code Code: // test of ...

1. ## 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!!!!!!

2. 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

3. 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.

4. 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.

5. 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. lol poor bloke, he said he only just started

Popular pages Recent additions