jeudi 7 septembre 2017

Random Start Location For Balls, avoiding Collisions

I am aware there are some similar questions to this existing, however my code is slightly different and I feel it's almost finished I just need a slight tweak but I'm not sure exactly what.

My goal is to spawn the circle in the 900x600 canvas, without them colliding initially. The amount of balls is specified by the user up to a maximum of 100.

My method so far:

                startCounter = 8;
                balls = new ArrayList<>();


                for (int i = 0; i < amount; i++) {
                    diameter = random.nextInt(30) + 10 + diameterRange;//generate diameter value for ball
                    startCounter += diameter + 25;//stop balls from spawning on top of eachother
                    BouncingBall ball = makeBall(startCounter, diameter, i);
                    if (Physics.Collision(ball, balls)||ball.getXPosition()> 850||ball.getYPosition()>550){//avoid starting collisions

                        continue;
                    }

                    balls.add(ball);

                  }

Currently this almost works perfectly, except it will just not create balls if they fail the if statement, resulting in fewer balls than the user specifies.

I tried adding i--; to the contents of the if statement, but this doesn't work and balls seem to spawn incorrectly in this case.

Thanks:)




Aucun commentaire:

Enregistrer un commentaire