Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Ball b;
- PVector t1;
- boolean grabbed;
- ArrayList<particle> ps;
- float pmx, pmy;
- void setup() {
- fullScreen(P3D);
- orientation(LANDSCAPE);
- b = new Ball();
- t1 = new PVector();
- grabbed = false;
- ps = new ArrayList<particle>();
- pmx=mouseX;
- pmy=mouseY;
- }
- void draw() {
- noStroke();
- beginShape();
- fill(202, 251, 122, 128);
- vertex(0, 0);
- fill(85, 247, 175, 128);
- vertex(0, height);
- fill(135, 235, 234, 128);
- vertex(width, height);
- fill(236, 240, 239, 128);
- vertex(width, 0);
- endShape(CLOSE);
- b.run();
- for (int i = ps.size()-1; i >=0; i--) {
- ps.get(i).run();
- if (ps.get(i).vel.mag() <= 0.1) {
- ps.remove(i);
- }
- }
- if (grabbed) {
- strokeWeight(10);
- stroke(255);
- line(b.loc.x, b.loc.y, mouseX, mouseY);
- }
- pmx=mouseX;
- pmy=mouseY;
- }
- class Ball {
- PVector loc, acc, vel;
- float size;
- Ball() {
- size = min(width, height)/4;
- loc = new PVector(random(size/2, width-size/2), random(size/2, height-size/2));
- vel = new PVector(0, 0);
- acc = new PVector(0, 0);
- }
- void run() {
- noStroke();
- fill(255);
- vel.add(acc).mult(0.98);
- loc.add(vel);
- ellipse(loc.x, loc.y, size, size);
- loc.x = constrain(loc.x, size/2, width-size/2);
- loc.y = constrain(loc.y, size/2, height-size/2);
- if (loc.x <= size/2 || loc.x >= width-size/2) {
- vel.x *= -1;
- }
- if (loc.y <= size/2 || loc.y >= height-size/2) {
- vel.y *= -1;
- }
- acc.mult(0);
- }
- }
- void mousePressed() {
- if (dist(mouseX, mouseY, b.loc.x, b.loc.y) < b.size/2) {
- t1 = new PVector(mouseX, mouseY);
- grabbed = true;
- }
- }
- void mouseReleased() {
- if (grabbed) {
- b.acc = new PVector(mouseX, mouseY).sub(t1).div(8);
- grabbed = false;
- }
- }
- void mouseDragged() {
- if (grabbed) {
- t1=b.loc.copy();
- ps.add(new particle(new PVector(mouseX, mouseY).sub(new PVector(pmx, pmy))));
- }
- }
- class particle {
- PVector loc, vel;
- particle(PVector v) {
- loc = new PVector(mouseX, mouseY);
- vel = v.copy();
- }
- void run() {
- vel.mult(0.9);
- loc.add(vel);
- fill(255);
- noStroke();
- ellipse(loc.x, loc.y, vel.mag(), vel.mag());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement