# Thread: The Old Bouncing Ball

1. ## The Old Bouncing Ball

Hi I was creating a simple ball movement for one of my games, and as I was typing it out, the maths part of my brain was screaming at me "Hey there is a formula here your doing all this typing for nothing", and I couldn't put my finger on what the formula was. i was wondering if anyone could help me?
Code:
```void Ball::collisionWindowTest()
{
// See if ball is about to leave the play area from top
if (itsY == 0)
{
// Switch the direction value
switch (itsDirection)
{
// Direction is equal to up
case (0):
{
itsDirection = 4;
break;
}
// Direction is up and right
case (1):
{
itsDirection = 3;
break;
}
// Direction is up and left
case (7):
{
itsDirection = 5;
break;
}
default:
break;
}
}
// See if ball is about to leave the play area from bottom
if (itsY == 448)
{
// Switch the direction value
switch (itsDirection)
{
// Coming in from the bottom right
case (3):
{
itsDirection = 1;
break;
}
// Coming in from the bottom
case (4):
{
itsDirection = 0;
break;
}
// coming in from the bottom left
case (5):
{
itsDirection = 7;
}
}
}
// See if the ball is about to leave the play area from left
if (itsX == 0)
{
// Switch the directon value
switch (itsDirection)
{
// coming in from the bottom left
case (5):
{
itsDirection = 3;
break;
}
// coming in from the left
case (6):
{
itsDirection = 2;
break;
}
// coming in from top left
case (7):
{
itsDirection = 1;
break;
}
}
}
// See if the ball is about to leave the play area from right
if (itsX == 608)
{
// Switch teh direction value
switch (itsDirection)
{
// leaving top right
case (1):
{
itsDirection = 7;
break;
}
// Leaving right
case (2):
{
itsDirection = 6;
break;
}
// Leaving bottom right
case (3):
{
itsDirection = 5;
break;
}
}
}
}```

2. It's easier if you store the x and y velocities separately.

Code:
```if( x < minX || x > maxX ) {

xVel = -xVel;
x += 2 * xVel;

}

if( y < minY || y > maxY ) {

yVel = -yVel;
y += 2 * yVel

}```

3. This will give you some ideas...

4. Thanks one of those articles helped me out a lot because i didn't get the concept behing sSquared's code, but there was a diagram about the angle of the ball that has helped me out no end. Thanks a lot