Bouncing ball - help ??

This is a discussion on Bouncing ball - help ?? within the C Programming forums, part of the General Programming Boards category; I'm writing a little program where a ball is bouncing at some walls. How do i get the right code ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    64

    Bouncing ball - help ??

    I'm writing a little program where a ball is bouncing at some walls.
    How do i get the right code for bouncing of a line..??

    Gugge

    Hope some of you can answer this....
    some of the code I got:
    ----------------

    typedef struct line lineT;
    struct line
    {
    int x1, x2;
    int y1, y2;
    };

    typedef struct ball ballT;
    struct ball
    {
    int x,y,r; /*r = radius */ /* RADIUS1 = 8 */
    };

    void bounce_check_ball(int *x, int *y, int *xstep, int *ystep, ballT *ball, lineT lines)
    {
    if(*ball[0] <= lines[6]) /* Theres something WRONG here */
    {
    *y = lines[6]-1;
    *ystep = -1*(random(2)+1);
    }
    }
    main()......

    lineT lines[30]; /* Array to keep all borderlines */
    ballT ball[1];

    /* A line whice the ball should bounce off at */
    lines[6].x1 = 510;
    lines[6].y1 = 116;
    lines[6].x2 = 100;
    lines[6].y2 = 116;

    ball[0].x = x; /* Startcoordinater for Ball */
    ball[0].y = y; /* ----------||----------- */
    ball[0].r = RADIUS1; /* Radius for ball[0] */

    while(1)
    {........
    bounce_check_ball(&x, &y, &xstep, &ystep, &ball, lineT lines[6]);

    x += xstep;
    y += ystep;

    setcolor(LIGHTCYAN);
    restorescreen((x-RADIUS1),(y-RADIUS1), &ball1);
    delay(15);
    restorescreen((oldx-RADIUS1),(oldy-RADIUS1), &blank); oldx=x; oldy=y;

    return 0;
    }
    !G!

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Could you be a little more descriptive? I am having trouble understanding completely, but here is my suggestion from what I can tell...
    Code:
    typedef struct
    {
     int xPos, yPos;
     int xStep, int yStep;
     int radius;
    } Ball; // Each ball has its own velocity, so I added it to the struct.
    
    // Checks to see if the ball *bounceMe intersects line
    //  *bounceOffMe and changes the velocity of bounceMe
    //  appropriately if it does.  Returns whether *bounceMe actually
    //  hit *bounceOffMe
    int bounceBall (Ball * bounceMe, lineT const * bounceOffMe)
    {
     int bounce = 0;
    
     int A, B, C, X, Y;
     int xStepNew;
     int yStepNew;
    
     if (ballIntersectsLine (bounceMe, bounceOffMe) {
      bounce = 1;
    
      X = bounceOffMe -> x2 - bounceOffMe -> x1;
      Y = bounceOffMe -> y2 - bounceOffMe -> y1;
      A = X * Y;
      B = X * X - Y * Y;
      C = X * X + Y * Y;
     
      xStepNew = (2 * bounceMe -> yStep * A + bounceMe -> xStep * B) / C;
      yStepNew = (2 * bounceMe -> xStep * A - bounceMe -> yStep * B) / C;
    
      bounceMe -> xStep = xStepNew;
      bounceMe -> yStep = yStepNew;
     }
     return bounce;
    }
    As you can see, the formula for a bounce is something of a monster (it is possible I got the formula wrong even, but I'm pretty sure this is it).
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    64
    tx for answering. I will try to figure out your code tomorrow, It's getting late overhere..
    It's difficult to get all the code in here, since my code i very long all inclusive.

    Sometimes it's much easiere to see what the problem is, when the code is in the editor..

    I hope i can use you answer, or else I will ask some more..

    Gugge
    !G!

  4. #4
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    I got a bouncing ball program to work, if you need any help, i'll give you the source.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    64
    Tx I would love to see that.
    I got the ball to bounce of lines (walls) in 1 rectangle fx. (1,1,639,479) /* graphic mode 640x480) */

    But the real trouble is comming when I try to add more lines on the screen, both vertikal- , horizontal- and odd lines...

    my email is gugge@image.dk

    tx very much..

    Gugge
    !G!

  6. #6
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    It's in Direct X though, I should've posted that before, sorry. If you still want me to post it, I will

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    64
    sure still like to see it..
    Did you program it in C??

  8. #8
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Nope, C++, but

    1. It should still work.
    2. I'll post it here or PM you when I get more time later on.

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, 04:41 AM
  2. bouncing ball
    By planet_abhi in forum Game Programming
    Replies: 2
    Last Post: 11-10-2003, 07:18 AM
  3. The Old Bouncing Ball
    By bartybasher in forum Game Programming
    Replies: 3
    Last Post: 08-19-2003, 04:06 AM
  4. Simple bouncing ball program
    By funkydude9 in forum Game Programming
    Replies: 3
    Last Post: 08-18-2003, 11:00 PM
  5. Bouncing ball
    By SKINp in forum Game Programming
    Replies: 4
    Last Post: 01-13-2003, 02:26 PM

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