Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem with multiple bouncing balls program in C

Problem with multiple bouncing balls program in C

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsarchitecturehelp
15 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    I am afraid that information tells us nothing. You need to run your code in the debugger which will help you to find out exactly where it is stopping. But, as I already said, this library is very old and may not work under Windows.

    U Offline
    U Offline
    User 13600238
    wrote on last edited by
    #6

    I have seen similar projects which ran very good. Here is what it shows after the debugging: https://mega.nz/#!NRwEAJKR!KI5cTeFhAaagIaRlbBoba1ugKt1j2RYEgUkF4lc8Atc

    L 1 Reply Last reply
    0
    • U User 13600238

      I have seen similar projects which ran very good. Here is what it shows after the debugging: https://mega.nz/#!NRwEAJKR!KI5cTeFhAaagIaRlbBoba1ugKt1j2RYEgUkF4lc8Atc

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #7

      And what does that tell you?

      U 1 Reply Last reply
      0
      • J Jochen Arndt

        You might get out of bound array accesses when step is always >= 0.63:

        total=n*7;
        num=(int *) malloc(total*sizeof(int));
        // ...
        do
        {
        j=0;
        for(i=0; i<=n; i++)
        {
        if (step >= 0.03)
        {
        // Possible out of bound accesses here (j >= total)
        // ...
        j=j+7;
        }
        }
        }
        while (!kbhit());

        You have to change the for loop to

        for(i=0; iI also don't understand why you are only updating when step is large enough. If that does not happen, balls at the end of the array will be updated less often than the initial ones.

        You are checking if the new ball position is outside and inverts the direction if so but are still drawing the ball when outside. A better solution might be drawing it at the border (here for x):

        if (num[j]+num[j+4]>=getmaxx() || num[j]-num[j+4]<=0)
        {
        num[j+5] *= -1;
        if (num[j]+num[j+4] > getmaxx())
        num[j] = getmaxx() - num[j+4];
        if (num[j]-num[j+4] < 0)
        num[j] = num[j+4];
        }

        Finally it might not work as expected because you have up to 7 balls and did not handle overlapping ones. I guess you want to detect also ball collisions which would result in no overlapping. But that is also not handled by your code.

        U Offline
        U Offline
        User 13600238
        wrote on last edited by
        #8

        Well, it didn't work and yes I also want to handle overlapping balls and make them bounce together and change direction. But, I don't know how. However, I don't have up to 7 balls. I actually use 3, but each of them has 7 data (color, position, speed etc.). That's why I use i at the for loop to count the number of loops (each ball). Then I use j to go to the desired data of each ball. All the data is being inserted to the program from a .txt file as you can see. It might seem stupid that I don't understand some thing. However, I am a beginner and I am trying to learn. So, please help me and be patient. I really appreciate your help.

        J 1 Reply Last reply
        0
        • L Lost User

          And what does that tell you?

          U Offline
          U Offline
          User 13600238
          wrote on last edited by
          #9

          To tell the truth, I don't understand.

          L 1 Reply Last reply
          0
          • U User 13600238

            Well, it didn't work and yes I also want to handle overlapping balls and make them bounce together and change direction. But, I don't know how. However, I don't have up to 7 balls. I actually use 3, but each of them has 7 data (color, position, speed etc.). That's why I use i at the for loop to count the number of loops (each ball). Then I use j to go to the desired data of each ball. All the data is being inserted to the program from a .txt file as you can see. It might seem stupid that I don't understand some thing. However, I am a beginner and I am trying to learn. So, please help me and be patient. I really appreciate your help.

            J Offline
            J Offline
            Jochen Arndt
            wrote on last edited by
            #10

            Sorry, I missed the number of balls. But my other notes are still valid (out of bound array access and why not updating with small steps). For timing purposes (slower movement), wait outside the for loop. I suggest to start with one ball to implement and check the border bouncing. To be realistic, the new directions should be calculated according to the collision angle. Then proceed with two balls to make it simpler. For collison detection you need another loop (runing from currently processed ball index + 1 to number of balls) to check for collisions like with the borders. Upon a collision you have to set the collision position as new position for both balls and adjust the directions (which requires a flag / state that the other ball has been already processed within the current loop execution). Again, the new directions should be calculated according to the collision angles. To check the code, you can suppress the drawing and print out the positions and changings instead. Or write them to a text file so that you can inspect it later. For testing it might be also helpful to stop after each step and continue after a key press. So you can check your calculations.

            U 1 Reply Last reply
            0
            • J Jochen Arndt

              Sorry, I missed the number of balls. But my other notes are still valid (out of bound array access and why not updating with small steps). For timing purposes (slower movement), wait outside the for loop. I suggest to start with one ball to implement and check the border bouncing. To be realistic, the new directions should be calculated according to the collision angle. Then proceed with two balls to make it simpler. For collison detection you need another loop (runing from currently processed ball index + 1 to number of balls) to check for collisions like with the borders. Upon a collision you have to set the collision position as new position for both balls and adjust the directions (which requires a flag / state that the other ball has been already processed within the current loop execution). Again, the new directions should be calculated according to the collision angles. To check the code, you can suppress the drawing and print out the positions and changings instead. Or write them to a text file so that you can inspect it later. For testing it might be also helpful to stop after each step and continue after a key press. So you can check your calculations.

              U Offline
              U Offline
              User 13600238
              wrote on last edited by
              #11

              I have already made it with one ball and I tried to continue with two, three etc. However, I still do something wrong with more than one balls and I can't understand what. I have been working on this for almost a month and I have tried everything. I also searched the internet and asked at other forums too, but still nothing. I only managed to make it with one ball. I understand what you say and that's what I am thinking too, but I can't put it inside code and make it run correctly.

              J 1 Reply Last reply
              0
              • U User 13600238

                To tell the truth, I don't understand.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #12

                Then maybe you should forget this program and start learning C and how it operates in better detail. You cannot become a developer by copying other people's code and guesswork.

                1 Reply Last reply
                0
                • U User 13600238

                  I have already made it with one ball and I tried to continue with two, three etc. However, I still do something wrong with more than one balls and I can't understand what. I have been working on this for almost a month and I have tried everything. I also searched the internet and asked at other forums too, but still nothing. I only managed to make it with one ball. I understand what you say and that's what I am thinking too, but I can't put it inside code and make it run correctly.

                  J Offline
                  J Offline
                  Jochen Arndt
                  wrote on last edited by
                  #13

                  Your posted code does not check for ball to ball collisions. You have to implement it. There should be examples in the net for the algorithms.

                  1 Reply Last reply
                  0
                  • U User 13600238

                    Hello, I am trying to make a program in C with multiple bouncing balls. The problem is that I have created a code, but my program doesn’t run properly. To be more exact, I think the part where it reads the data is fine, but then there is a problem in the code of the graphics.h library. I cannot understand where the problem is, as it is the first time I use this library. Thank you in advamce. Here is my code:

                    #include #include #include #include #include int main()
                    {
                    FILE *input;
                    int n, *num, i, total, j;
                    if((input=fopen("data.txt", "r"))!=NULL)
                    {
                    fscanf(input, "%d", &n);
                    total=n*7;
                    num=(int *) malloc(total*sizeof(int));
                    if(num!=NULL)
                    {
                    for(i=0; i= 0.03)
                    {
                    previous = finish;
                    setfillstyle(SOLID_FILL,BLACK);
                    setcolor(BLACK);
                    fillellipse(num[j],num[j+1],num[j+4],num[j+4]);
                    num[j]+= num[j+5]*step;
                    num[j+1]+= num[j+6]*step;
                    if (num[j]+num[j+4]>=getmaxx() || num[j]-num[j+4]<=0)
                    {
                    num[j+5] *= -1;
                    }
                    if (num[j+1]+num[j+4]>=getmaxy() || num[j+1]-num[j+4]<=0)
                    {
                    num[j+6] *= -1;
                    }
                    setfillstyle(SOLID_FILL,RED);
                    setcolor(RED);
                    fillellipse(num[j],num[j+1],num[j+4],num[j+4]);
                    j=j+7;
                    }
                    }

                    V Offline
                    V Offline
                    Victor Nijegorodov
                    wrote on last edited by
                    #14

                    Are you trying to recreate a 30-years old xonix game? :-D :rolleyes: :laugh:

                    1 Reply Last reply
                    0
                    • U User 13600238

                      Hello, I am trying to make a program in C with multiple bouncing balls. The problem is that I have created a code, but my program doesn’t run properly. To be more exact, I think the part where it reads the data is fine, but then there is a problem in the code of the graphics.h library. I cannot understand where the problem is, as it is the first time I use this library. Thank you in advamce. Here is my code:

                      #include #include #include #include #include int main()
                      {
                      FILE *input;
                      int n, *num, i, total, j;
                      if((input=fopen("data.txt", "r"))!=NULL)
                      {
                      fscanf(input, "%d", &n);
                      total=n*7;
                      num=(int *) malloc(total*sizeof(int));
                      if(num!=NULL)
                      {
                      for(i=0; i= 0.03)
                      {
                      previous = finish;
                      setfillstyle(SOLID_FILL,BLACK);
                      setcolor(BLACK);
                      fillellipse(num[j],num[j+1],num[j+4],num[j+4]);
                      num[j]+= num[j+5]*step;
                      num[j+1]+= num[j+6]*step;
                      if (num[j]+num[j+4]>=getmaxx() || num[j]-num[j+4]<=0)
                      {
                      num[j+5] *= -1;
                      }
                      if (num[j+1]+num[j+4]>=getmaxy() || num[j+1]-num[j+4]<=0)
                      {
                      num[j+6] *= -1;
                      }
                      setfillstyle(SOLID_FILL,RED);
                      setcolor(RED);
                      fillellipse(num[j],num[j+1],num[j+4],num[j+4]);
                      j=j+7;
                      }
                      }

                      R Offline
                      R Offline
                      Rick York
                      wrote on last edited by
                      #15

                      For your next assignment, extend your program to work in three dimensions and use OpenGL to display the balls. For bonus credit, display the bounding cube with translucent walls. In other words, partially transparent but not totally. Good luck. :cool:

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups