mirror of
https://gitlab.com/MisterBiggs/Resume.git
synced 2025-06-16 01:16:39 +00:00
give mouse gravity
This commit is contained in:
parent
82d7d557f7
commit
1a0817ddff
113
p5/mover.js
113
p5/mover.js
@ -6,58 +6,73 @@
|
|||||||
// https://editor.p5js.org/codingtrain/sketches/MkLraatd
|
// https://editor.p5js.org/codingtrain/sketches/MkLraatd
|
||||||
|
|
||||||
class Mover {
|
class Mover {
|
||||||
constructor(x, y, vx, vy, m) {
|
constructor(x, y, vx, vy, m) {
|
||||||
this.pos = createVector(x, y);
|
this.pos = createVector(x, y);
|
||||||
this.maxSpeed = 10;
|
this.maxSpeed = 10;
|
||||||
this.vel = createVector(vx, vy);
|
this.vel = createVector(vx, vy);
|
||||||
this.acc = createVector(0, 0);
|
this.acc = createVector(0, 0);
|
||||||
this.mass = m;
|
this.mass = m;
|
||||||
this.r = sqrt(this.mass) * 0.75;
|
this.r = sqrt(this.mass) * 1;
|
||||||
this.col = color(255);
|
this.col = color(255);
|
||||||
}
|
}
|
||||||
|
|
||||||
attract(other) {
|
mouse_mass() {
|
||||||
let force = p5.Vector.sub(this.pos, other.pos);
|
if (focused) {
|
||||||
|
let force = p5.Vector.sub(createVector(mouseX, mouseY), this.pos);
|
||||||
let distanceSq = constrain(force.magSq(), 10, 1000);
|
let distanceSq = constrain(force.magSq(), 10, 1000);
|
||||||
let G = 3;
|
let G = 3;
|
||||||
let strength = (G * this.mass * other.mass) / distanceSq;
|
let strength = (G * this.mass * 50) / distanceSq;
|
||||||
force.setMag(strength);
|
force.setMag(strength);
|
||||||
other.applyForce(force);
|
this.applyForce(force);
|
||||||
|
|
||||||
|
// noStroke();
|
||||||
|
// fill("lightgreen");
|
||||||
|
// ellipse(mouseX, mouseY, 30);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
applyForce(force) {
|
|
||||||
let f = p5.Vector.div(force, this.mass);
|
attract(other) {
|
||||||
this.acc.add(f);
|
let force = p5.Vector.sub(this.pos, other.pos);
|
||||||
|
let distanceSq = constrain(force.magSq(), 10, 1000);
|
||||||
|
let G = 3;
|
||||||
|
let strength = (G * this.mass * other.mass) / distanceSq;
|
||||||
|
force.setMag(strength);
|
||||||
|
other.applyForce(force);
|
||||||
|
}
|
||||||
|
|
||||||
|
applyForce(force) {
|
||||||
|
let f = p5.Vector.div(force, this.mass);
|
||||||
|
this.acc.add(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
edges() {
|
||||||
|
if (this.pos.y >= height - this.r) {
|
||||||
|
this.pos.y = height - this.r;
|
||||||
|
this.vel.y *= -1;
|
||||||
|
} else if (this.pos.y <= this.r) {
|
||||||
|
this.pos.y = this.r;
|
||||||
|
this.vel.y *= -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
edges() {
|
if (this.pos.x >= width - this.r) {
|
||||||
if (this.pos.y >= height - this.r) {
|
this.pos.x = width - this.r;
|
||||||
this.pos.y = height - this.r;
|
this.vel.x *= -1;
|
||||||
this.vel.y *= -1;
|
} else if (this.pos.x <= this.r) {
|
||||||
} else if (this.pos.y <= this.r) {
|
this.pos.x = this.r;
|
||||||
this.pos.y = this.r;
|
this.vel.x *= -1;
|
||||||
this.vel.y *= -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.pos.x >= width - this.r) {
|
|
||||||
this.pos.x = width - this.r;
|
|
||||||
this.vel.x *= -1;
|
|
||||||
} else if (this.pos.x <= this.r) {
|
|
||||||
this.pos.x = this.r;
|
|
||||||
this.vel.x *= -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
update() {
|
|
||||||
this.vel.add(this.acc);
|
update() {
|
||||||
this.vel.limit(this.maxSpeed);
|
this.vel.add(this.acc);
|
||||||
this.pos.add(this.vel);
|
this.vel.limit(this.maxSpeed);
|
||||||
this.acc.set(0, 0);
|
this.pos.add(this.vel);
|
||||||
}
|
this.acc.set(0, 0);
|
||||||
|
}
|
||||||
show() {
|
|
||||||
noStroke();
|
show() {
|
||||||
fill(this.col);
|
noStroke();
|
||||||
ellipse(this.pos.x, this.pos.y, this.r * 2);
|
fill(this.col);
|
||||||
}
|
ellipse(this.pos.x, this.pos.y, this.r * 2);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
35
p5/sketch.js
35
p5/sketch.js
@ -9,27 +9,42 @@ let movers = [];
|
|||||||
let attractor;
|
let attractor;
|
||||||
var canvas;
|
var canvas;
|
||||||
|
|
||||||
function windowResized(){
|
function windowResized() {
|
||||||
resizeCanvas(windowWidth, windowHeight);
|
resizeCanvas(windowWidth, windowHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setup() {
|
function setup() {
|
||||||
canvas = createCanvas(windowWidth, windowHeight);
|
canvas = createCanvas(windowWidth, windowHeight);
|
||||||
canvas.position(0, 0);
|
canvas.position(0, 0);
|
||||||
canvas.style("z-index", "-1")
|
canvas.style("z-index", "-1");
|
||||||
|
|
||||||
movers[0] = new Mover(width/4, height, 5, -5, 55);
|
movers[0] = new Mover(10, 10, 5, -5, 50);
|
||||||
movers[0].col = color('red');
|
movers[0].col = color("red");
|
||||||
movers[1] = new Mover(3*width/4, height, -5, -5, 50);
|
movers[1] = new Mover(width / 2, height / 2, -5, -5, 60);
|
||||||
movers[1].col = color('white');
|
movers[1].col = color("white");
|
||||||
movers[2] = new Mover(2*width/4, height, -5, -5, 45);
|
movers[2] = new Mover(width - 10, height - 10, -5, -5, 40);
|
||||||
movers[2].col = color('blue');
|
movers[2].col = color("blue");
|
||||||
|
|
||||||
|
// Gay Mode
|
||||||
|
// movers[0] = new Mover(0, 0, 5, -5, 50);
|
||||||
|
// movers[0].col = color('#D12229');
|
||||||
|
// movers[1] = new Mover(width/5, height/5, -5, -5, 50);
|
||||||
|
// movers[1].col = color('#F68A1E');
|
||||||
|
// movers[2] = new Mover(2*width/5, 2*height/5, -5, -5, 50);
|
||||||
|
// movers[2].col = color('#FDE01A');
|
||||||
|
// movers[3] = new Mover(3*width/5, 3*height/5, -5, -5, 50);
|
||||||
|
// movers[3].col = color('#007940');
|
||||||
|
// movers[4] = new Mover(4*width/5, 4*height/5, -5, -5, 50);
|
||||||
|
// movers[4].col = color('#24408E');
|
||||||
|
// movers[5] = new Mover(width, height, -5, -5, 50);
|
||||||
|
// movers[5].col = color('#732982');
|
||||||
background(0);
|
background(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
function draw() {
|
function draw() {
|
||||||
background(0, 75);
|
background(0, 75);
|
||||||
for (let mover of movers) {
|
for (let mover of movers) {
|
||||||
|
mover.mouse_mass();
|
||||||
for (let other of movers) {
|
for (let other of movers) {
|
||||||
if (mover !== other) {
|
if (mover !== other) {
|
||||||
mover.attract(other);
|
mover.attract(other);
|
||||||
@ -41,4 +56,4 @@ function draw() {
|
|||||||
mover.edges();
|
mover.edges();
|
||||||
mover.show();
|
mover.show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user