# Thread: Breakout Collision Detection Algorithm Help

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

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

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

6. 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. > 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. How to generate a rand within a range is in the FAQ