Bouncing ball

This is a discussion on Bouncing ball within the Game Programming forums, part of the General Programming Boards category; I can make a ball bounce around the screen and it's not too difficult. The way I do it though ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    18

    Bouncing ball

    I can make a ball bounce around the screen and it's not too difficult. The way I do it though the angles are always the same, by simply adding or subtracting 1 to the X and Y coordinates. What I am wondering is how I would be able to make the ball bounce at different angles. Correct angles. I know I would have to use the SIN, or COSINE function. Can someone shed a little light on this for me please. Thank you.
    Be yourself for those who mind don't matter, and those who matter don't mind.

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    the angle of incidence is always the same as the angle of reflection. if you take your motion, and break it into horizontal and vertical force components... [in a real gravity simulation]... you'll note that there is no force applied in the horizontal direction, and acceleration is always negative [back down to earth]... so if you have a constant horizontal motion, and an accelerating vertical speed... you get correct, and realistic motion.

    in order to get the correct angles, you have to calculate the initial vertical speed of the ball, and the angle of incidence it makes on the plane it attacks. also, if that plane is sloped, you also need to account for that.

    is there anything specific you wanted to know besides the physics of it? do you have any examples?

    hth

    PS i like your signature.
    hasafraggin shizigishin oppashigger...

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    #define PI 3.14159
    #define DEGTORAD(x) (double)((double)x*PI/(double)180)

    x+=cos(DEGTORAD(ballangle))*ballspeed;
    y+=sin(DEGTORAD(ballangle))*ballspeed;

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    For the right wall
    xvel = -xvel;
    yvel = yvel;

    For the left wall
    xvel = -xvel;
    yvel = yvel;

    For the top wall
    xvel = xvel;
    yvel = -yvel;

    And so on ...

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Actually, to get the ball to bounce off at the correct angle you have to do more than that.

    You have to keep track of the balls last position on the screen. When it hits a wall you then create a triangle of sorts and do the standard trig to get the return angle.

    Code:
    struct ball
    {
      double x;
      double y;
      double lastx;
      double lasty;
      double spd;
      double xinc;
      double yinc;
      void Update(void) {x+=(xinc*spd);y+=(yinc*spd);};
    }Ball;
    
    void CheckWallHit(void)
    {
      if (ball.y<0)
      {
         //Ball in 2nd quadrant -,+
         double diffy=ball.lasty-ball.y;
         double diffx=ball.lastx-ball.x;
         diffx*=diffx;
         diffy*=diffy;
         double hyp=sqr(diffx+diffy);
         if (diffx==0.0f)
         {
            //Ball returns at 90 or 180 - depending on your orientation
            ball.yinc=1;
            ball.xinc=0;
         }
         else
         {
            ball.yinc=(diffy/hyp);
            ball.xinc=-(diffx/hyp);        
         }
         ball.lastx=ball.x;
         ball.lasty=ball.y;
      }
    }
    Now to check for the rest of the walls, you use the same algo and check for x<=left x>=right and y>=bottom. Just change the signs to get the ball to bounce correctly.

    1st quad -,- Lower left of screen
    2nd quad -,+ Upper left of screen
    3rd quad +,- Upper right of screen
    4th quad +,+ Lower right of screen

    This is not using cos and sin per se like my first example. It breaks the x,y into sep components. Actually all that is happening is that you are taking the cos and sin of the angle when you divide by the hyp.

    cos=opp/hyp
    sin=adj/hyp
    tan=opp/adj

    or
    (O)scar (H)ad (A) (H)old (O)n (A)rthur

    cos - O/H
    sin - A/H
    tan -O/A

    It does use the sqrt() function which is dreadfully slow, but it's ok for pong and balls bouncing. My program has over 1000 balls bouncing at a time and it is very fast. Should be no problem for 1 ball. My floating point calcs are handled by the FPU, though, so you may not be able to get as many balls pinging off of the walls.

    I believe this example's orientation is:
    Code:
            270
             |
    180  <-  *  ->   0
             |
            90
    (Had to use code section to preserve tabs)

    At the corners this algo will perform 2 square roots since both x and y will have hit the walls. But you should not notice it.

    For ball acceleration and projectile stuff, check out www.programmersheaven.com and about a million other sites. Look it up on a search engine. Hey DA why didn't you give him the projectile formula? I've forgotten it.

  6. #6
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Ok you have a ball going at m velocity which hits the wall on
    the right at an angle of incidence of theta. Then decomposing
    the velocity
    m_x = sin(theta)
    m_y = cos(theta)

    After it hits the wall the angle of incidence is the same at the magnitude is assumed to be the same.

    m'_x = -m sin(theta)
    m'_y = m cos(theta)

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    Thank you for all the help, I will try messing with it when i get back to school and I will tell you how it turns out. Thank you again. I love this forum.

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Quick question for you..are you just adding and/or subtracting a value from the y coordinate of the ball when bouncing??

    coz if you are, then the ball will not look like it's bouncing naturally (ie, with gravity ).

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    The algo that I gave will produce a pong ball effect. For projectile bouncing or projectile motion I referred to www.programmersheaven.com. This is not projectile motion.

    Also the angle of incidence is not equal to the angle that it hits the wall. This is why I broke the velocity into two component x and y vectors. I've tried this simply by taking the sin cos of the angle, but you must find the return angle, not the angle it hit the wall at. Just inversing the x and y vectors when it hits left,top,right,bottom repesctively will not work. The ball will not bounce off the walls at the correct angles.

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    Yes I like my signature as well. I got it from my brother, who I believe got it from Dr Seuss. As of now I am just adding and subtracting from the x and y coordinates. When I thought about how to do it that is the only way i came up with. I will keep trying it this way until I get it, maybe after I'll try it the other way. How would you suggest doing the other way? Let me know so I can start to think about it. Good bye.
    Be yourself for those who mind don't matter, and those who matter don't mind.

  11. #11
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    The way that I showed in my first post will work for all angles and all return angles.

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. The Old Bouncing Ball
    By bartybasher in forum Game Programming
    Replies: 3
    Last Post: 08-19-2003, 03:06 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