The Old Bouncing Ball

This is a discussion on The Old Bouncing Ball within the Game Programming forums, part of the General Programming Boards category; Hi I was creating a simple ball movement for one of my games, and as I was typing it out, ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    76

    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. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    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
    
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    This will give you some ideas...
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create a bouncing ball?
    By Swerve in forum C++ Programming
    Replies: 7
    Last Post: 09-12-2008, 03:41 AM
  2. bouncing ball
    By planet_abhi in forum Game Programming
    Replies: 2
    Last Post: 11-10-2003, 06:18 AM
  3. Simple bouncing ball program
    By funkydude9 in forum Game Programming
    Replies: 3
    Last Post: 08-18-2003, 10:00 PM
  4. Bouncing ball
    By SKINp in forum Game Programming
    Replies: 4
    Last Post: 01-13-2003, 01:26 PM
  5. Bouncing ball - help ??
    By Gugge in forum C Programming
    Replies: 7
    Last Post: 04-13-2002, 02:34 PM

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