xxxxxxxxxx
54
// Friction Force
// The Nature of Code
// The Coding Train / Daniel Shiffman
// https://youtu.be/9IQEM_Ijklg
// https://thecodingtrain.com/learning/nature-of-code/2.3-friction.html
// https://editor.p5js.org/codingtrain/sketches/zcTpMCpA
let movers = [];
let mu = 0.1;
function setup() {
createCanvas(600, 400);
for (let i = 0; i < 10; i++) {
movers[i] = new Mover(random(width), random(0,100), random(2, 4));
}
}
function draw() {
background(0);
for (let mover of movers) {
if (mouseIsPressed) {
let wind = createVector(0.1, 0);
drawVector(wind, mover.pos.x, mover.pos.y, color(0, 0, 255), 100);
mover.applyForce(wind, color(0, 0, 255));
}
let gravity = createVector(0, 0.2);
let weight = p5.Vector.mult(gravity, mover.mass);
drawVector(weight, mover.pos.x, mover.pos.y, color(255, 100, 150), 100);
mover.applyForce(weight, color(0, 255, 0));
mover.friction();
mover.update();
mover.edges();
mover.show();
}
}
function drawVector(v, x, y, col, factor) {
push();
let arrowsize = 4;
// Translate to position to render vector
translate(x, y);
stroke(col);
// Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate
rotate(v.heading());
// Calculate length of vector & scale it to be bigger or smaller if necessary
let len = v.mag() * factor;
// Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction)
line(0, 0, len, 0);
line(len, 0, len - arrowsize, +arrowsize / 2);
line(len, 0, len - arrowsize, -arrowsize / 2);
pop();
}