Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private ArrayList<Ball> balls = new ArrayList<Ball>();
- private boolean clear = false;
- void setup() {
- size(1280, 900);
- background(0);
- ellipseMode(CENTER);
- int[] init = {100, 100};
- noStroke();
- }
- void draw() {
- // clears the sceren before starting
- if (clear) {
- background(0);
- }
- for (int i = 0; i < balls.size(); i++) {
- int radius = balls.get(i).getRadius();
- int x = balls.get(i).getPosition()[0];
- int y = balls.get(i).getPosition()[1];
- float vel_x = balls.get(i).getVelocity()[0];
- float vel_y = balls.get(i).getVelocity()[1];
- int[] ball_color = balls.get(i).getColor();
- fill(ball_color[0], ball_color[1], ball_color[2]);
- ellipse(x, y, radius * 2, radius * 2);
- balls.get(i).updatePosition();
- }
- balls.add(new Ball((int) random(500, 750), (int) random(300, 500)));
- ellipse(width / 2, height / 2, 50, 50);
- }
- void mouseClicked() {
- balls.add(new Ball(mouseX, mouseY));
- }
- void keyPressed() {
- if (key == CODED) {
- if (keyCode == UP) {
- if (clear) {
- clear = false;
- } else {
- clear = true;
- }
- }
- }
- }
- public static int shitcolor = 0;
- class Ball {
- private int radius; //random later
- private int[] colour = new int[3]; // random later
- // where the ball is
- private int x;
- private int y;
- // where the ball is moving
- private float velocity_x;
- private float velocity_y;
- // acceleration
- private float acceleration_x = 0;
- private float acceleration_y = 0;
- // relative to gravity
- private int delta_x;
- private int delta_y;
- private float delta_d;
- private float acceleration_angle;
- // how much it bounces
- private float elasticity; // random later
- // how much it is affected by acceleration
- private float friction;
- private int add = 1;
- Ball(int initial_x, int initial_y) {
- // setup position
- this.x = initial_x;
- this.y = initial_y;
- // setup random velocity
- //this.velocity_x = random(1, 10);
- //this.velocity_y = random(1, 10);
- this.velocity_x = 5;
- this.velocity_y = 0;
- // properties
- this.radius = 2;
- this.colour[0] = shitcolor;
- this.colour[1] = shitcolor;
- this.colour[2] = shitcolor;
- //this.radius = (int) random(2, 20);
- //this.colour[0] = (int) random(25, 255);
- //this.colour[1] = (int) random(25, 255);
- //this.colour[2] = (int) random(25, 255);
- this.elasticity = random(0.2, 0.95);
- this.friction = random(0.05, 0.4);
- if (shitcolor < 255) {
- shitcolor++;
- }
- }
- // METHODS
- void updatePosition() {
- // moves the object with velocity
- this.x += this.velocity_x;
- this.y += this.velocity_y;
- this.velocity_x += this.acceleration_x;
- this.velocity_y += this.acceleration_y;
- /*// check for boundaries
- // right
- if ((this.x + radius) >= width) {
- velocity_x *= -1;
- this.x = width - radius;
- }
- // left
- if ((this.x - radius) <= 0) {
- velocity_x *= -1;
- this.x = radius;
- }
- // bottom
- if ((this.y + radius) >= height) {
- velocity_y *= -1 * this.elasticity;
- this.y = height - radius;
- }
- // top
- if ((this.y - radius) <= 0) {
- velocity_y *= -1;
- this.y = radius;
- }*/
- // gravity
- // find where acceleration should be
- this.delta_x = (width / 2) - this.x;
- this.delta_y = (height / 2) - this.y;
- this.delta_d = sqrt(sq(this.delta_x) + sq(this.delta_y));
- /*if (this.delta_x == 0 && this.delta_y > 0) { //above
- this.acceleration_angle = 90;
- } else if (this.delta_x == 0 && this.delta_y < 0) { //below
- this.acceleration_angle = 270;
- } else if (this.delta_y == 0 && this.delta_x > 0) { //left
- this.acceleration_angle = 180;
- } else if (this.delta_y == 0 && this.delta_x < 0) { //right
- this.acceleration_angle = 0;
- } else {
- this.acceleration_angle = atan(this.delta_x / this.delta_y);
- }*/
- /*/ execute acceleration
- if (0 < this.acceleration_angle && this.acceleration_angle < 90) {
- this.acceleration_x = -1 * sin(this.acceleration_angle) * 0.2;
- this.acceleration_y = -1 * cos(this.acceleration_angle) * 0.2;
- } else if (90 < this.acceleration_angle && this.acceleration_angle < 180) {
- this.acceleration_x = sin(this.acceleration_angle) * 0.2;
- this.acceleration_y = -1 * cos(this.acceleration_angle) * 0.2;
- } else if (180 < this.acceleration_angle && this.acceleration_angle < 270) {
- this.acceleration_x = sin(this.acceleration_angle) * 0.2;
- this.acceleration_y = cos(this.acceleration_angle) * 0.2;
- } else if (270 < this.acceleration_angle) {
- this.acceleration_x = -1 * sin(this.acceleration_angle) * 0.2;
- this.acceleration_y = cos(this.acceleration_angle) * 0.2;
- }*/
- this.acceleration_x = this.delta_x * 0.01;
- this.acceleration_y = this.delta_y * 0.01;
- }
- // SETTERS
- void setPosition(int x, int y) {
- this.x = x;
- this.y = y;
- }
- void setVelocity(int[] velocity) {
- this.velocity_x = velocity[0];
- this.velocity_y = velocity[1];
- }
- // GETTERS
- int getRadius() {
- return this.radius;
- }
- int[] getColor() {
- return this.colour;
- }
- int[] getPosition() {
- int[] return_pos = {x, y};
- return return_pos;
- }
- float[] getVelocity() {
- float[] return_vel = {this.velocity_x, this.velocity_y};
- return return_vel;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement