Processing Project
unknown
java
2 years ago
4.4 kB
3
Indexable
// needs to install the library ControlP5 through Sketch -> Manage Libraries // You can find it by just searching "p5" import java.lang.Math; import controlP5.*; ControlP5 cp5; int rad = 30; int numBalls = 1500; int og = numBalls; int[] dirs = {-1, 1}; class Ball { private float x, y, xSpeed, ySpeed; private int xDir, yDir, rad; private int[] colors; Ball(float _x, float _y, float _xSpeed, float _ySpeed, int _xDir, int _yDir, int[] _colors, int _rad) { this.x = _x; this.y = _y; this.xSpeed = _xSpeed; this.ySpeed = _ySpeed; this.xDir = _xDir; this.yDir = _yDir; this.colors = _colors; this.rad = _rad; } public void setX(float x) { this.x = x; } public void setY(float y) { this.y = y; } public void setColor(int[] colors) { //reset colors int[] temp = {}; colors = temp; // add new colors this.colors[0] = colors[0]; this.colors[1] = colors[1]; this.colors[2] = colors[2]; } public void setSpeed(float xSpeed, float ySpeed) { this.xSpeed = xSpeed; this.ySpeed = ySpeed; } public void setXDir(int xDir) { this.xDir *= xDir; } public void setYDir(int yDir) { this.yDir *= yDir; } public void setRad(int rad) { this.rad = rad; } public float getX() { return this.x; } public float getY() { return this.y; } public int[] getColor() { return this.colors; } public int getXDir() { return this.xDir; } public int getYDir() { return this.yDir; } public int getRad() { return this.rad; } public float getXSpeed() { return this.xSpeed; } public float getYSpeed() { return this.ySpeed; } } Ball[] balls; void setup() { initializeBalls(numBalls); cp5 = new ControlP5(this); // Number of Balls Slider cp5.addSlider("numBalls") .setPosition(width/2, 20) .setRange(0, numBalls) // Set the range of values for the slider .setValue(numBalls) .setLabel("Number of Balls"); //fullScreen(); size(800,800); noStroke(); ellipseMode(RADIUS); } void initializeBalls(int count) { int[] b = {0, 0, 0}; balls = new Ball[og + count]; for (int j = 0; j < balls.length; j++) { balls[j] = new Ball((float) (Math.random() * (width - rad*2) + rad), (float) (Math.random() * (width - rad*2) + rad), random(5, 20), random(5, 20), dirs[(int)(Math.random() * dirs.length)], dirs[(int)(Math.random() * dirs.length)], b, 30); } } void numBalls(int value) { numBalls = (int) value; // Update the numBalls variable with the slider value if (numBalls < og) { for (int i = og - 1; i >= numBalls; i--) { // Reset properties for removed balls balls[i].setX(0); balls[i].setY(0); } og = numBalls; // Update the original number of balls } else { // Add new balls if numBalls is increased initializeBalls(numBalls - og); og = numBalls; // Update the original number of balls } } int[] getColor() { int[] _color = new int[3]; int min = 0; int max = 255; int range = max - min + 1; // Increase range by 1 to include max value for (int i = 0; i < 3; i++) { _color[i] = (int) (Math.random() * range) + min; } return _color; } void changeColor(int index) { balls[index].setColor(getColor()); } int[] bgCol = getColor(); void draw() { background(bgCol[0], bgCol[1], bgCol[2]); for (int i = 0; i < numBalls; i++) { if (balls[i].getX() > width - balls[i].getRad() || balls[i].getX() < balls[i].getRad()) { balls[i].setXDir(-1); balls[i].setX(balls[i].getX() + (balls[i].getXSpeed() * balls[i].getXDir())); changeColor(i); } if (balls[i].getY() > height - balls[i].getRad() || balls[i].getY() < balls[i].getRad()) { balls[i].setYDir(-1); balls[i].setY(balls[i].getY() + (balls[i].getYSpeed() * balls[i].getYDir())); changeColor(i); } balls[i].setX(balls[i].getX() + (balls[i].getXSpeed() * balls[i].getXDir())); balls[i].setY(balls[i].getY() + (balls[i].getYSpeed() * balls[i].getYDir())); fill(balls[i].getColor()[0], balls[i].getColor()[1], balls[i].getColor()[2]); ellipse(balls[i].getX(), balls[i].getY(), balls[i].getRad(), balls[i].getRad()); } }
Editor is loading...
Leave a Comment