Thread: Need some help with a bouncing ball algorithm

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    5

    Need some help with a bouncing ball algorithm

    Hi, I've been working on a bouncing ball program with linked lists, however I am having some trouble with the very last part which is to spawn balls indefinitely while the program is running instead of just the 5 starting balls.

    The Code: main.c * GitHub

    Any help would be appreciated!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,489
    If you're going to post links to code on github, at least make sure it's a compilable project instead of some random snippet.

    While you're at it, make some functions as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    5
    Ah sorry, entire code :list.c * GitHub

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,489
    Perhaps you could add
    Code:
    if ( event.key.keysym.sym == SDLK_SPACE ) {
      ball = CreateObject(screen,sphere_model,SPHERE_NUMTRIANGLES);
      printf("SpeedX: %f\tSpeedy: %f\tTX: %f\t TY: %f\n", ball->speedx, ball->speedy,ball->tx, ball->ty);
      list_addlast(list,ball);
    }
    I'll leave you to figure out where it should be added.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2016
    Posts
    5
    Ah I totally should have thought of that, thanks!

  6. #6
    Registered User
    Join Date
    Nov 2016
    Posts
    5
    I also have bit of an issue with removing the balls 5 seconds after they stop, I currently use an SDL command that essentially times out the screen for 5 seconds before deleting the balls, but that is not the smoothest of solutions. Is there any neat way to count down seconds in code before executing something?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,489
    First of all, you need a condition which says 'stopped'. Bear in mind that since your velocities are floats, so you need to be aware that you can't easily do == 0.0
    Question 14.5

    Second, you need to add a member to each ball instance along the lines of
    time_t stoppedMovingTime;
    which you set to the result of time() when you've decided it's stopped moving.

    Then on every iteration, you call difftime() with that time and the current time to figure out if 5 seconds has elapsed. If it has, then remove that ball instance.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Nov 2016
    Posts
    5
    ok, i'll try that, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bouncing a ball
    By ammad in forum C++ Programming
    Replies: 16
    Last Post: 08-27-2009, 05:02 PM
  2. Bouncing Ball
    By The Brain in forum Windows Programming
    Replies: 12
    Last Post: 12-29-2006, 08:56 AM
  3. Bouncing Ball Algorithm
    By Stan100 in forum Game Programming
    Replies: 12
    Last Post: 04-06-2003, 07:36 PM
  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

Tags for this Thread