From d33f351f2478d965dd166029ace9354c16ac6648 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Wed, 7 Feb 2024 23:30:31 +0200 Subject: [PATCH] complete exercise 1.8 --- 1. Vectors/8. Mouse Attractor/index.html | 15 +++++++ 1. Vectors/8. Mouse Attractor/sketch.js | 52 ++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 1. Vectors/8. Mouse Attractor/index.html create mode 100644 1. Vectors/8. Mouse Attractor/sketch.js diff --git a/1. Vectors/8. Mouse Attractor/index.html b/1. Vectors/8. Mouse Attractor/index.html new file mode 100644 index 0000000..c390616 --- /dev/null +++ b/1. Vectors/8. Mouse Attractor/index.html @@ -0,0 +1,15 @@ + + + + + + + +
+
+ + diff --git a/1. Vectors/8. Mouse Attractor/sketch.js b/1. Vectors/8. Mouse Attractor/sketch.js new file mode 100644 index 0000000..c9e02de --- /dev/null +++ b/1. Vectors/8. Mouse Attractor/sketch.js @@ -0,0 +1,52 @@ +class Walker { + constructor(x = 0, y = 0) { + this.position = createVector(x, y) + this.velocity = createVector(0, 0) + this.accelaration = createVector(0, 0) + } + + step() { + let target = createVector(mouseX, mouseY) + let difference = p5.Vector.sub(target, this.position) + this.accelaration = difference.copy() + this.accelaration.setMag(1000/difference.mag()) + + const dt = deltaTime/1000 + this.velocity.add(p5.Vector.mult(this.accelaration, dt)) + this.position.add(p5.Vector.mult(this.velocity, dt)) + + + if (this.position.x < 0) { + this.position.x += width + } + if (this.position.x > width) { + this.position.x -= width + } + + if (this.position.y < 0) { + this.position.y += height + } + if (this.position.y > height) { + this.position.y -= height + } + } + + draw() { + stroke(0) + circle(this.position.x, this.position.y, 20) + } +} + +let walker + +function setup() { + createCanvas(640, 240) + background(255) + walker = new Walker(width/2, height/2) +} + +function draw() { + background(255) + walker.step() + walker.draw() +}