Thread: Bouncing Ball Algorithm

  1. #1
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361

    Question Bouncing Ball Algorithm

    I've been playing around with WriteConsoleOutput() and other CHAR_INFO, SMALL_RECT stuff, and I soon want to be making a Pong clone. I try to make a ball bounce diagnally, but when it gets to the top it messes up. I don't care about my code, or fixing it, especially since it's just a driver program. But does anyone have a good ball algorithm(sp?)
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  2. #2
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179
    I wrote a couple of bouncing ball/paddle apps a little while ago.
    They should still be available here.
    Illusion and reality become impartiality and confidence.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    249

    Thumbs up

    Have you tried looking at the code in my game?

  4. #4
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179
    The best ball bouncing system that I've found goes like this:
    Code:
    struct ballt {
        int x, y, dx, dy,radius;
    }ball;
    
    //in main...
    ball.radius = 10;
    ball.x = (rand() % (SCREEN_W - ball.radius * 4)) + ball.radius * 2;
    ball.y = (rand() % (SCREEN_H - ball.radius * 4)) + ball.radius * 2;
    ball.dx = rand() % 2 * 2 - 1; //-1 or 1
    ball.dy = rand() % 2 * 2 - 1;
    circlefill(buffer,ball.x,ball.y,ball.radius,15);
    
    void move_ball() {
        ball.x += ball.dx;
        ball.y += ball.dy;
        if (ball.y <= ball.radius || ball.y >= SCREEN_H - ball.radius)
            ball.dy *= -1;
        if (ball.x <= ball.radius || ball.y >= SCREEN_W - ball.radius)
            ball.dx *= -1;
    }
    yeah? worked for me
    Illusion and reality become impartiality and confidence.

  5. #5
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    Bouncing Ball Algorithm
    I would expect a really good bouncing ball algorithm to use a quadratic equation

  6. #6
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179
    The appeal of this system is collision with the paddle:
    Code:
    if (ball.x <= paddle.width && ball.y >= paddle.y 
        && ball.y <= paddle.y + paddle.height) {
        ball.dx *= -1;
        ball.dy = int((float(ball.y - paddle.y + paddle.height / 2) / 
                       float(paddle.height / 2)) * 3.0);
    }
    see? easy.
    Illusion and reality become impartiality and confidence.

  7. #7
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361

    Red face Sorry...

    I would expect a really good bouncing ball algorithm to use a quadratic equation
    struct ballt {
    int x, y, dx, dy,radius;


    Sorry, this is my fault. You see, I'm making a simple ball test. My "ball" is really a char(219) and I just want it to bounce diagnally. Think of pong, with no paddles. I just couldn't do that. Does anyone have an algorithm (am I spelling that right?) for a simple char(219) bouncing "ball".
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  8. #8
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    Well I'm not going to continue to say too much because I really think this is something you can figure out on your own. I am just going to say that when things bounce, they don't just go at 90 degrees (sometimes it will, it depends on what angle it forms with the surface you are colliding against).

    EDIT: if you don't get what I just said, I'll draw a picture, but only if you care enough to ask.

  9. #9
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    things bounce off of a wall at the same angle that they collided...so to confuse you, the vector for the ball's approach to the wall mirrored over the wall's normal is the vector of the direction it will leave (pointed the wrong direction, but that's okay)

    Code:
    \    /
    a\  /b
    __\/___
    angle a= angle b
    Away.

  10. #10
    unless, of course, there is some english on the ball

  11. #11
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361
    Here's an example of what happens when I try:
    EDIT: My beutiful Ascii art did not come out right

    If it is going up and right, and hits the north part of the screen, it should go down and right (in the most simple case). Well mine hits the top and come back going left and down
    Last edited by Stan100; 04-06-2003 at 05:06 PM.
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  12. #12
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Originally posted by Stan100
    Here's an example of what happens when I try:
    EDIT: My beutiful Ascii art did not come out right

    If it is going up and right, and hits the north part of the screen, it should go down and right (in the most simple case). Well mine hits the top and come back going left and down
    if this is 2D ( i assume it is)

    you are reflecting both the y and x. that is why it comes back left and down. in general, if it hits the top or bottom, just reflect the y, if it hits the left or right, just reflect the x.

  13. #13
    I would expect a really good bouncing ball algorithm to use a quadratic equation
    Wouldn't an absolute value equation serve better for this use than a quadratic equation?

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. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. The Old Bouncing Ball
    By bartybasher in forum Game Programming
    Replies: 3
    Last Post: 08-19-2003, 03:06 AM
  4. Bouncing ball
    By SKINp in forum Game Programming
    Replies: 4
    Last Post: 01-13-2003, 02:26 PM
  5. Bouncing ball - help ??
    By Gugge in forum C Programming
    Replies: 7
    Last Post: 04-13-2002, 02:34 PM