Bouncing Balls, struggling with getting more than 1 (processing)












1















so I want to have 10 Balls bouncing up and down. So far I have managed to get 1 Ball to bounce, and to have something like gravity.
But now I want to add more balls, but I just can`t manage to do so. So far I tried to add an array, and then to use a loop, but nothing I tried worked for me yet.
Would appreciate if somebody could point me out to the Solution.



 Ball b; 

void setup() {
size(940, 660);
b = new Ball();
}

void draw() {
background(50);
fill(255);

b.display();
b.move();
}


and the class:



class Ball 
{
float circleX;
float circleY;
float speed;
float gravity=0.2;

Ball() {
speed = 0;
circleY = 0;
circleX = 200;
}

void move() {
speed = speed + gravity; //gravity draufrechnen
circleY = circleY + speed; //mit der geschwindigkeit bewegegn
if (circleY >= height){
speed = -speed; //andere richtung
circleY = height;
speed = speed*0.9;
}
}
void display() {
stroke(0);
fill(127);
ellipse(circleX, circleY, 50 , 50);
}
}









share|improve this question

























  • Is circleY and circleX the balls location? If so when you create multiple balls in your setup they will all be on-top of one another. You should modify your constructor to take in at least a different x value, then try b1 = new Ball(50), b2 = new Ball(200), etc... in your setup, then draw the balls.

    – M.G
    Nov 24 '18 at 0:34













  • Create a MCVE of at least one bouncing ball (as your title claims) and before you know it you will be shown how to make 50 bouncing balls.

    – DevilsHnd
    Nov 24 '18 at 1:45
















1















so I want to have 10 Balls bouncing up and down. So far I have managed to get 1 Ball to bounce, and to have something like gravity.
But now I want to add more balls, but I just can`t manage to do so. So far I tried to add an array, and then to use a loop, but nothing I tried worked for me yet.
Would appreciate if somebody could point me out to the Solution.



 Ball b; 

void setup() {
size(940, 660);
b = new Ball();
}

void draw() {
background(50);
fill(255);

b.display();
b.move();
}


and the class:



class Ball 
{
float circleX;
float circleY;
float speed;
float gravity=0.2;

Ball() {
speed = 0;
circleY = 0;
circleX = 200;
}

void move() {
speed = speed + gravity; //gravity draufrechnen
circleY = circleY + speed; //mit der geschwindigkeit bewegegn
if (circleY >= height){
speed = -speed; //andere richtung
circleY = height;
speed = speed*0.9;
}
}
void display() {
stroke(0);
fill(127);
ellipse(circleX, circleY, 50 , 50);
}
}









share|improve this question

























  • Is circleY and circleX the balls location? If so when you create multiple balls in your setup they will all be on-top of one another. You should modify your constructor to take in at least a different x value, then try b1 = new Ball(50), b2 = new Ball(200), etc... in your setup, then draw the balls.

    – M.G
    Nov 24 '18 at 0:34













  • Create a MCVE of at least one bouncing ball (as your title claims) and before you know it you will be shown how to make 50 bouncing balls.

    – DevilsHnd
    Nov 24 '18 at 1:45














1












1








1








so I want to have 10 Balls bouncing up and down. So far I have managed to get 1 Ball to bounce, and to have something like gravity.
But now I want to add more balls, but I just can`t manage to do so. So far I tried to add an array, and then to use a loop, but nothing I tried worked for me yet.
Would appreciate if somebody could point me out to the Solution.



 Ball b; 

void setup() {
size(940, 660);
b = new Ball();
}

void draw() {
background(50);
fill(255);

b.display();
b.move();
}


and the class:



class Ball 
{
float circleX;
float circleY;
float speed;
float gravity=0.2;

Ball() {
speed = 0;
circleY = 0;
circleX = 200;
}

void move() {
speed = speed + gravity; //gravity draufrechnen
circleY = circleY + speed; //mit der geschwindigkeit bewegegn
if (circleY >= height){
speed = -speed; //andere richtung
circleY = height;
speed = speed*0.9;
}
}
void display() {
stroke(0);
fill(127);
ellipse(circleX, circleY, 50 , 50);
}
}









share|improve this question
















so I want to have 10 Balls bouncing up and down. So far I have managed to get 1 Ball to bounce, and to have something like gravity.
But now I want to add more balls, but I just can`t manage to do so. So far I tried to add an array, and then to use a loop, but nothing I tried worked for me yet.
Would appreciate if somebody could point me out to the Solution.



 Ball b; 

void setup() {
size(940, 660);
b = new Ball();
}

void draw() {
background(50);
fill(255);

b.display();
b.move();
}


and the class:



class Ball 
{
float circleX;
float circleY;
float speed;
float gravity=0.2;

Ball() {
speed = 0;
circleY = 0;
circleX = 200;
}

void move() {
speed = speed + gravity; //gravity draufrechnen
circleY = circleY + speed; //mit der geschwindigkeit bewegegn
if (circleY >= height){
speed = -speed; //andere richtung
circleY = height;
speed = speed*0.9;
}
}
void display() {
stroke(0);
fill(127);
ellipse(circleX, circleY, 50 , 50);
}
}






java arrays object processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 0:58









Nick

29.7k121941




29.7k121941










asked Nov 24 '18 at 0:29









Victor Victor

61




61













  • Is circleY and circleX the balls location? If so when you create multiple balls in your setup they will all be on-top of one another. You should modify your constructor to take in at least a different x value, then try b1 = new Ball(50), b2 = new Ball(200), etc... in your setup, then draw the balls.

    – M.G
    Nov 24 '18 at 0:34













  • Create a MCVE of at least one bouncing ball (as your title claims) and before you know it you will be shown how to make 50 bouncing balls.

    – DevilsHnd
    Nov 24 '18 at 1:45



















  • Is circleY and circleX the balls location? If so when you create multiple balls in your setup they will all be on-top of one another. You should modify your constructor to take in at least a different x value, then try b1 = new Ball(50), b2 = new Ball(200), etc... in your setup, then draw the balls.

    – M.G
    Nov 24 '18 at 0:34













  • Create a MCVE of at least one bouncing ball (as your title claims) and before you know it you will be shown how to make 50 bouncing balls.

    – DevilsHnd
    Nov 24 '18 at 1:45

















Is circleY and circleX the balls location? If so when you create multiple balls in your setup they will all be on-top of one another. You should modify your constructor to take in at least a different x value, then try b1 = new Ball(50), b2 = new Ball(200), etc... in your setup, then draw the balls.

– M.G
Nov 24 '18 at 0:34







Is circleY and circleX the balls location? If so when you create multiple balls in your setup they will all be on-top of one another. You should modify your constructor to take in at least a different x value, then try b1 = new Ball(50), b2 = new Ball(200), etc... in your setup, then draw the balls.

– M.G
Nov 24 '18 at 0:34















Create a MCVE of at least one bouncing ball (as your title claims) and before you know it you will be shown how to make 50 bouncing balls.

– DevilsHnd
Nov 24 '18 at 1:45





Create a MCVE of at least one bouncing ball (as your title claims) and before you know it you will be shown how to make 50 bouncing balls.

– DevilsHnd
Nov 24 '18 at 1:45












1 Answer
1






active

oldest

votes


















1














Create a constructor in balls, where you can pass the initial x and y coordinate of a ball:



class Ball 
{
.....

Ball(int x, int y) {
speed = 0;
circleX = x;
circleY = y;
}

.....
}


Create an array of balls and initialize it in the setup function:



int no_of_balls = 10;
Ball balls = new Ball[no_of_balls];

void setup() {

for (int i=0; i<no_of_balls; ++i) {
balls[i] = new Ball(80 + i*80, i*5);
}

size(940, 660);
}


The balls can be initialized with different start heights by using Math.random():



for (int i=0; i<no_of_balls; ++i) {
balls[i] = new Ball( 80 + i*80, (int)(Math.random()*100.0) );
}


display and move the array of balls in draw:



void draw() {
background(50);
fill(255);

for (int i=0; i<no_of_balls; ++i) {
balls[i].display();
balls[i].move();
}
}


Preview (down scaled):








share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454196%2fbouncing-balls-struggling-with-getting-more-than-1-processing%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Create a constructor in balls, where you can pass the initial x and y coordinate of a ball:



    class Ball 
    {
    .....

    Ball(int x, int y) {
    speed = 0;
    circleX = x;
    circleY = y;
    }

    .....
    }


    Create an array of balls and initialize it in the setup function:



    int no_of_balls = 10;
    Ball balls = new Ball[no_of_balls];

    void setup() {

    for (int i=0; i<no_of_balls; ++i) {
    balls[i] = new Ball(80 + i*80, i*5);
    }

    size(940, 660);
    }


    The balls can be initialized with different start heights by using Math.random():



    for (int i=0; i<no_of_balls; ++i) {
    balls[i] = new Ball( 80 + i*80, (int)(Math.random()*100.0) );
    }


    display and move the array of balls in draw:



    void draw() {
    background(50);
    fill(255);

    for (int i=0; i<no_of_balls; ++i) {
    balls[i].display();
    balls[i].move();
    }
    }


    Preview (down scaled):








    share|improve this answer




























      1














      Create a constructor in balls, where you can pass the initial x and y coordinate of a ball:



      class Ball 
      {
      .....

      Ball(int x, int y) {
      speed = 0;
      circleX = x;
      circleY = y;
      }

      .....
      }


      Create an array of balls and initialize it in the setup function:



      int no_of_balls = 10;
      Ball balls = new Ball[no_of_balls];

      void setup() {

      for (int i=0; i<no_of_balls; ++i) {
      balls[i] = new Ball(80 + i*80, i*5);
      }

      size(940, 660);
      }


      The balls can be initialized with different start heights by using Math.random():



      for (int i=0; i<no_of_balls; ++i) {
      balls[i] = new Ball( 80 + i*80, (int)(Math.random()*100.0) );
      }


      display and move the array of balls in draw:



      void draw() {
      background(50);
      fill(255);

      for (int i=0; i<no_of_balls; ++i) {
      balls[i].display();
      balls[i].move();
      }
      }


      Preview (down scaled):








      share|improve this answer


























        1












        1








        1







        Create a constructor in balls, where you can pass the initial x and y coordinate of a ball:



        class Ball 
        {
        .....

        Ball(int x, int y) {
        speed = 0;
        circleX = x;
        circleY = y;
        }

        .....
        }


        Create an array of balls and initialize it in the setup function:



        int no_of_balls = 10;
        Ball balls = new Ball[no_of_balls];

        void setup() {

        for (int i=0; i<no_of_balls; ++i) {
        balls[i] = new Ball(80 + i*80, i*5);
        }

        size(940, 660);
        }


        The balls can be initialized with different start heights by using Math.random():



        for (int i=0; i<no_of_balls; ++i) {
        balls[i] = new Ball( 80 + i*80, (int)(Math.random()*100.0) );
        }


        display and move the array of balls in draw:



        void draw() {
        background(50);
        fill(255);

        for (int i=0; i<no_of_balls; ++i) {
        balls[i].display();
        balls[i].move();
        }
        }


        Preview (down scaled):








        share|improve this answer













        Create a constructor in balls, where you can pass the initial x and y coordinate of a ball:



        class Ball 
        {
        .....

        Ball(int x, int y) {
        speed = 0;
        circleX = x;
        circleY = y;
        }

        .....
        }


        Create an array of balls and initialize it in the setup function:



        int no_of_balls = 10;
        Ball balls = new Ball[no_of_balls];

        void setup() {

        for (int i=0; i<no_of_balls; ++i) {
        balls[i] = new Ball(80 + i*80, i*5);
        }

        size(940, 660);
        }


        The balls can be initialized with different start heights by using Math.random():



        for (int i=0; i<no_of_balls; ++i) {
        balls[i] = new Ball( 80 + i*80, (int)(Math.random()*100.0) );
        }


        display and move the array of balls in draw:



        void draw() {
        background(50);
        fill(255);

        for (int i=0; i<no_of_balls; ++i) {
        balls[i].display();
        balls[i].move();
        }
        }


        Preview (down scaled):









        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 7:52









        Rabbid76Rabbid76

        37.7k113248




        37.7k113248
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454196%2fbouncing-balls-struggling-with-getting-more-than-1-processing%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python