From 111adc1dea441d1a45289f1d3234643b792c23a3 Mon Sep 17 00:00:00 2001 From: Lev Date: Mon, 7 Jun 2021 00:02:33 -0500 Subject: [PATCH] Update added balls on drag --- .../Topic 3/3.2.3/sketch.js | 45 +++++++++++++------ 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/CM2030 Graphics Programming/Topic 3/3.2.3/sketch.js b/CM2030 Graphics Programming/Topic 3/3.2.3/sketch.js index a171216..d1b9c2a 100644 --- a/CM2030 Graphics Programming/Topic 3/3.2.3/sketch.js +++ b/CM2030 Graphics Programming/Topic 3/3.2.3/sketch.js @@ -8,32 +8,51 @@ var ball; /////////////////////////////////////////////// function setup() { createCanvas(600, 400); - ball = new Ball(); + balls = []; } /////////////////////////////////////////////// function draw() { background(0); + let gravity = createVector(0, 0.1); - ball.applyForce(gravity); - let friction = ball.velocity.copy(); - friction.mult(-1); - friction.normalize(); - friction.mult(0.01); - ball.applyForce(friction); - ball.run(); + for (let i = 0; i < balls.length; i++) { + let ball = balls[i]; + + let friction = ball.velocity.copy(); + friction.mult(-1); + friction.normalize(); + friction.mult(0.01); + + let momentum = new createVector(width/2 - mouseX, height/2 - mouseY); + momentum.normalize(); + momentum.mult(0.1); + + ball.applyForce(gravity); + ball.applyForce(friction); + ball.applyForce(momentum); + ball.run(); + } } function mouseClicked() { - ball.location = new createVector(width / 2, height / 2); + //ball.location = new createVector(width / 2, height / 2); +} + +function mouseDragged() { + + if(mouseButton === LEFT) + balls.push(new Ball(mouseX, mouseY)); + else + balls = []; } /////////////////////////////////////////////// class Ball { - constructor() { + constructor(x, y) { this.acceleration = new createVector(0, 0); - this.velocity = new createVector(0, 0); - this.location = new createVector(width / 2, height / 2); - this.size = 40; + this.velocity = new createVector(random(-3, 3), random(-3, 3)); + this.location = new createVector(x, y); + this.size = random (10, 60); } run() {