

let center, mouse;
function setup() {
createCanvas(800, 400);
}
function draw() {
background(25);
center = createVector(width / 2, height / 2);
mouse = createVector(mouseX, mouseY);
let dir = p5.Vector.sub(mouse, center);
// ๋ฒกํฐ ๊ทธ๋ฆฌ๊ธฐ
stroke(10, 180, 170);
fill(0);
translate(center.x, center.y);
line(0, 0, dir.x, dir.y);
// ๋์ ํ์ดํ
push();
translate(dir.x, dir.y);
rotate(dir.heading());
let arrowSize = 20;
translate(-arrowSize, 0);
triangle(0, 2, 0, -2, arrowSize, 0);
pop();
noStroke();
fill(255, 255, 255);
text("๋ฒกํฐ ๊ธธ์ด: " + nf(dir.mag(), 1, 2), -50, -20);
}
๐ ๊ฐ๋ : ๋ ์ ๊ฐ์ ๋ฒกํฐ, ๋ฐฉํฅ(heading()), ํฌ๊ธฐ(mag()).

let pos, vel;
function setup() {
createCanvas(800, 200);
pos = createVector(100, 200);
vel = createVector(2, -1);
}
function draw() {
background(25);
pos.add(vel);
// ํ๋ฉด ๋ฐ์ผ๋ก ๋๊ฐ๋ฉด ๋ฐ์
if (pos.x > width || pos.x < 0) vel.x *= -1;
if (pos.y > height || pos.y < 0) vel.y *= -1;
fill(10, 190, 180, 255);
ellipse(pos.x, pos.y, 20, 20);
// ์๋ ๋ฒกํฐ ๊ทธ๋ฆฌ๊ธฐ
stroke(10, 190, 180, 255);
strokeWeight(3);
line(pos.x, pos.y, pos.x + vel.x * 10, pos.y + vel.y * 10);
}
๐ ๊ฐ๋ : p5.Vector.add(), ์๋ ๋ฒกํฐ, ํ๋ฉด ๋ฐ์ฌ.

let mover;
function setup() {
createCanvas(800, 400);
mover = new Mover();
}
function draw() {
background(25);
let gravity = createVector(0, 0.1);
mover.applyForce(gravity);
// ๋ฐ๋ฅ ๊ทผ์ฒ์ผ ๋ ๋ง์ฐฐ๋ ฅ
if (mover.pos.y > height - 20) {
let friction = mover.vel.copy();
friction.normalize();
friction.mult(-0.1);
mover.applyForce(friction);
}
mover.update();
mover.edges();
mover.display();
}
class Mover {
constructor() {
this.pos = createVector(width / 2, 0);
this.vel = createVector(0, 0);
this.acc = createVector(0, 0);
this.mass = 1;
}
applyForce(force) {
let f = p5.Vector.div(force, this.mass);
this.acc.add(f);
}
update() {
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.mult(0);
}
edges() {
if (this.pos.y > height - 25) {
this.pos.y = height - 25;
this.vel.y *= -0.7; // ๋ฐ๋
}
}
display() {
fill(10, 190, 180);
ellipse(this.pos.x, this.pos.y, 50, 50);
}
}
๐ ๊ฐ๋ : ๋ดํด์ ์ 2๋ฒ์น F = ma, ์ค๋ ฅ๊ณผ ๋ง์ฐฐ๋ ฅ์ ํฉ์ฐ, ๋ฐ๋ ์๋ฎฌ๋ ์ด์ .

let movers = [];
function setup() {
createCanvas(800, 400);
for (let i = 0; i < 40; i++) {
movers.push(new Mover(random(width), random(height)));
}
}
function draw() {
background(25, 25, 25, 50);
let wind = createVector(0.02, 0.02);
let gravity = createVector(0, 0.01);
for (let m of movers) {
if (mouseIsPressed) m.applyForce(wind);
m.applyForce(gravity);
m.update();
m.edges();
m.display();
}
fill(255);
noStroke();
text("Click to blow the wind โ", 10, 20);
}
class Mover {
constructor(x, y) {
this.pos = createVector(x, y);
this.vel = createVector(0, 0);
this.acc = createVector(0, 0);
this.mass = random(0.5, 2);
}
applyForce(force) {
let f = p5.Vector.div(force, this.mass);
this.acc.add(f);
}
update() {
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.mult(0);
}
edges() {
if (this.pos.y > height - 10) {
this.pos.y = height - 10;
this.vel.y *= -0.8;
}
}
display() {
fill(10, 190, 180, 200);
ellipse(this.pos.x, this.pos.y, this.mass * 16, this.mass * 16);
}
}
๐ ๊ฐ๋ : ์ฌ๋ฌ ๊ฐ์ฒด์ ํ ์ ์ฉ, ์ง๋์ ๋ฐ๋ฅธ ๊ฐ์๋ ์ฐจ์ด, ๋ฐ๋๊ณผ ์ค๋ ฅ์ ์ํธ์์ฉ.
๋ง์ฐ์ค๊ฐ ์บ๋ฒ์ค ์์ญ์ ๋ฒ์ด๋๋ฉด ํํฐํด๋ค์ด ์์ ๋กญ๊ฒ ์์ง์ด๊ณ , ๋ค์ ๋ค์ด์ค๋ฉด ๋ง์ฐ์ค ์์น๋ก ๋๋ ค๊ฐ๋๋ค. ๋ง์ฐ์ค ํด๋ฆญ ์์ด ์์น๋ง์ผ๋ก ์๋ํฉ๋๋ค.

let movers = [];
const numMovers = 20;
function setup() {
createCanvas(800, 400);
for (let i = 0; i < numMovers; i++) {
movers.push(new Mover(random(width), random(height)));
}
}
function draw() {
background(25, 20);
let isMouseInside = mouseX >= 0 && mouseX <= width && mouseY >= 0 && mouseY <= height;
for (let mover of movers) {
if (isMouseInside) {
let mouse = createVector(mouseX, mouseY);
let dir = p5.Vector.sub(mouse, mover.pos);
let d = constrain(dir.mag(), 5, 25);
dir.normalize();
let strength = (200 * mover.mass) / (d * d);
dir.mult(strength);
mover.applyForce(dir);
}
mover.update();
mover.edges();
mover.display();
}
noStroke();
fill(255);
text("Move mouse to attract particles", 10, 20);
}
class Mover {
constructor(x, y) {
this.pos = createVector(x, y);
this.vel = createVector(0, 0);
this.acc = createVector(0, 0);
this.size = random(5, 15);
this.mass = this.size / 10;
}
applyForce(f) {
let force = p5.Vector.div(f, this.mass);
this.acc.add(force);
}
update() {
this.vel.add(this.acc);
this.vel.mult(0.85);
this.pos.add(this.vel);
this.acc.mult(0);
}
edges() {
if (this.pos.x > width) this.pos.x = 0;
if (this.pos.x < 0) this.pos.x = width;
if (this.pos.y > height) this.pos.y = 0;
if (this.pos.y < 0) this.pos.y = height;
}
display() {
fill(10, 180, 170);
ellipse(this.pos.x, this.pos.y, this.size, this.size);
}
}
๐ ๊ฐ๋ : ๊ฑฐ๋ฆฌ ๊ธฐ๋ฐ ํ (Gravitational Attraction), 1 / dยฒ ๋ฒ์น, ๋ง์ฐ์ค ์ธํฐ๋์ .