Advertisement
Helium

Untitled

Jan 8th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.57 KB | None | 0 0
  1. private ArrayList<Ball> balls = new ArrayList<Ball>();
  2. private boolean clear = false;
  3.  
  4. void setup() {
  5.   size(1280, 900);
  6.   background(0);
  7.  
  8.   ellipseMode(CENTER);
  9.  
  10.   int[] init = {100, 100};
  11.   noStroke();
  12.  
  13.  
  14. }
  15.  
  16. void draw() {
  17.   // clears the sceren before starting
  18.   if (clear) {
  19.     background(0);
  20.   }
  21.  
  22.   for (int i = 0; i < balls.size(); i++) {
  23.     int radius = balls.get(i).getRadius();
  24.     int x = balls.get(i).getPosition()[0];
  25.     int y = balls.get(i).getPosition()[1];
  26.    
  27.     float vel_x = balls.get(i).getVelocity()[0];
  28.     float vel_y = balls.get(i).getVelocity()[1];
  29.    
  30.     int[] ball_color = balls.get(i).getColor();
  31.    
  32.     fill(ball_color[0], ball_color[1], ball_color[2]);
  33.    
  34.     ellipse(x, y, radius * 2, radius * 2);
  35.  
  36.     balls.get(i).updatePosition();
  37.   }
  38.  
  39.   balls.add(new Ball((int) random(500, 750), (int) random(300, 500)));
  40.  
  41.  ellipse(width / 2, height / 2, 50, 50);
  42.  
  43. }
  44.  
  45. void mouseClicked() {
  46.   balls.add(new Ball(mouseX, mouseY));
  47. }
  48.  
  49. void keyPressed() {
  50.    if (key == CODED) {
  51.      if (keyCode == UP) {
  52.        if (clear) {
  53.          clear = false;
  54.        } else {
  55.          clear = true;
  56.        }
  57.      }
  58.    }
  59. }
  60.  
  61. public static int shitcolor = 0;
  62.  
  63. class Ball {
  64.  
  65.   private int radius; //random later
  66.   private int[] colour = new int[3]; // random later
  67.  
  68.   // where the ball is
  69.   private int x;
  70.   private int y;
  71.  
  72.   // where the ball is moving
  73.   private float velocity_x;
  74.   private float velocity_y;
  75.  
  76.   // acceleration
  77.   private float acceleration_x = 0;
  78.   private float acceleration_y = 0;
  79.  
  80.   // relative to gravity
  81.   private int delta_x;
  82.   private int delta_y;
  83.   private float delta_d;
  84.   private float acceleration_angle;
  85.  
  86.   // how much it bounces
  87.   private float elasticity; // random later
  88.  
  89.   // how much it is affected by acceleration
  90.   private float friction;
  91.  
  92.  
  93.   private int add = 1;
  94.  
  95.   Ball(int initial_x, int initial_y) {
  96.     // setup position
  97.     this.x = initial_x;
  98.     this.y = initial_y;
  99.    
  100.     // setup random velocity
  101.     //this.velocity_x = random(1, 10);
  102.     //this.velocity_y = random(1, 10);
  103.    
  104.     this.velocity_x = 5;
  105.     this.velocity_y = 0;
  106.  
  107.     // properties
  108.     this.radius = 2;
  109.     this.colour[0] = shitcolor;
  110.     this.colour[1] = shitcolor;
  111.     this.colour[2] = shitcolor;
  112.    
  113.     //this.radius = (int) random(2, 20);
  114.     //this.colour[0] = (int) random(25, 255);
  115.     //this.colour[1] = (int) random(25, 255);
  116.     //this.colour[2] = (int) random(25, 255);
  117.    
  118.     this.elasticity = random(0.2, 0.95);
  119.     this.friction = random(0.05, 0.4);
  120.    
  121.    
  122.    
  123.     if (shitcolor < 255) {
  124.       shitcolor++;
  125.     }
  126.   }
  127.  
  128.   // METHODS
  129.   void updatePosition() {
  130.     // moves the object with velocity
  131.     this.x += this.velocity_x;
  132.     this.y += this.velocity_y;
  133.    
  134.     this.velocity_x += this.acceleration_x;
  135.     this.velocity_y += this.acceleration_y;
  136.    
  137.     /*// check for boundaries
  138.     // right
  139.     if ((this.x + radius) >= width) {
  140.       velocity_x *= -1;
  141.       this.x = width - radius;
  142.     }
  143.     // left
  144.     if ((this.x - radius) <= 0) {
  145.       velocity_x *= -1;
  146.       this.x = radius;
  147.     }
  148.     // bottom
  149.     if ((this.y + radius) >= height) {
  150.       velocity_y *= -1 * this.elasticity;
  151.       this.y = height - radius;
  152.     }
  153.     // top
  154.     if ((this.y - radius) <= 0) {
  155.       velocity_y *= -1;
  156.       this.y = radius;
  157.     }*/
  158.    
  159.     // gravity
  160.     // find where acceleration should be
  161.     this.delta_x = (width / 2) - this.x;
  162.     this.delta_y = (height / 2) - this.y;
  163.     this.delta_d = sqrt(sq(this.delta_x) + sq(this.delta_y));
  164.    
  165.     /*if (this.delta_x == 0 && this.delta_y > 0) { //above
  166.       this.acceleration_angle = 90;
  167.     } else if (this.delta_x == 0 && this.delta_y < 0) { //below
  168.       this.acceleration_angle = 270;
  169.     } else if (this.delta_y == 0 && this.delta_x > 0) { //left
  170.       this.acceleration_angle = 180;
  171.     } else if (this.delta_y == 0 && this.delta_x < 0) { //right
  172.       this.acceleration_angle = 0;
  173.     } else {
  174.       this.acceleration_angle = atan(this.delta_x / this.delta_y);
  175.     }*/
  176.    
  177.     /*/ execute acceleration
  178.     if (0 < this.acceleration_angle && this.acceleration_angle < 90) {
  179.       this.acceleration_x = -1 * sin(this.acceleration_angle) * 0.2;
  180.       this.acceleration_y = -1 * cos(this.acceleration_angle) * 0.2;
  181.     } else if (90 < this.acceleration_angle && this.acceleration_angle < 180) {
  182.       this.acceleration_x = sin(this.acceleration_angle) * 0.2;
  183.       this.acceleration_y = -1 * cos(this.acceleration_angle) * 0.2;
  184.     } else if (180 < this.acceleration_angle && this.acceleration_angle < 270) {
  185.       this.acceleration_x = sin(this.acceleration_angle) * 0.2;
  186.       this.acceleration_y = cos(this.acceleration_angle) * 0.2;
  187.     } else if (270 < this.acceleration_angle) {
  188.       this.acceleration_x = -1 * sin(this.acceleration_angle) * 0.2;
  189.       this.acceleration_y = cos(this.acceleration_angle) * 0.2;
  190.     }*/
  191.     this.acceleration_x = this.delta_x * 0.01;
  192.     this.acceleration_y = this.delta_y * 0.01;
  193.    
  194.   }
  195.  
  196.   // SETTERS
  197.   void setPosition(int x, int y) {
  198.     this.x = x;
  199.     this.y = y;
  200.   }  
  201.  
  202.   void setVelocity(int[] velocity) {
  203.     this.velocity_x = velocity[0];
  204.     this.velocity_y = velocity[1];
  205.   }
  206.  
  207.   // GETTERS
  208.   int getRadius() {
  209.     return this.radius;
  210.   }
  211.  
  212.   int[] getColor() {
  213.     return this.colour;
  214.   }
  215.  
  216.   int[] getPosition() {
  217.     int[] return_pos = {x, y};
  218.     return return_pos;
  219.   }
  220.  
  221.   float[] getVelocity() {
  222.     float[] return_vel = {this.velocity_x, this.velocity_y};
  223.     return return_vel;
  224.   }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement