← Volver

Newton estaría orgulloso

NEWTON WOULD BE PROUD

El código

let walker; function setup() { createCanvas(680, 440); background(30); walker = new Walker(); smooth(); } function draw() { background(0, 3); fill(255, 255, 0); circle(width/2,height/2,15); walker.show(); walker.update(); walker.checkEdges(); } class Walker { constructor() { this.position = createVector(width/2, height/2); this.velocity = createVector(0, 0); this.acceleration = createVector(0, 0); } show() { noStroke(); fill(50,120,255); circle(this.position.x, this.position.y, 7); } update() { let mouse = createVector(mouseX, mouseY); let dir = p5.Vector.sub(mouse, this.position); let distM = dir.mag(); dir.setMag(1/distM); this.acceleration = dir; this.velocity.add(this.acceleration); this.velocity.limit(5); this.position.add(this.velocity); } checkEdges() { if (this.position.x > width) { this.position.x = 0; } else if (this.position.x < 0) { this.position.x = width; } if (this.position.y > height) { this.position.y = 0; } else if (this.position.y < 0) { this.position.y = height; } } }

NEWTON WOULD BE FASCINATED

El código

let bodies = []; let attractor; function setup() { createCanvas(800, 600); for (let i = 0; i < 32; i++) { bodies[i] = new Body(width/2, (10 + (i * 4)), 2, (4 + (i/32)), 0); } attractor = new Attractor(width/2, height/2, 256); } function draw() { background(32, 64); for (let i = 0; i < bodies.length; i++) { for (let j = 0; j < bodies.length; j++) { if (i !== j) { let force = bodies[j].attract(bodies[i]); bodies[i].applyForce(force); } } let force = attractor.attract(bodies[i]); bodies[i].applyForce(force); bodies[i].update(); bodies[i].show(); } } class Attractor { constructor(x, y, m) { this.position = createVector(x, y); this.mass = m; } attract(mover) { let force = p5.Vector.sub(this.position, mover.position); let distance = force.mag(); distance = constrain(distance, 5, 25); let strength = (this.mass * mover.mass) / (distance * distance); force.setMag(strength); return force; } } class Body { constructor(x, y, mass, accX, accY) { this.mass = mass; this.position = createVector(x, y); this.velocity = createVector(0, 0); this.acceleration = createVector(accX, accY); this.rC = random(100, 255); this.gC = random(100, 255); this.bC = random(100, 255); } applyForce(force) { let f = p5.Vector.div(force, this.mass); this.acceleration.add(f); } update() { this.velocity.add(this.acceleration); this.position.add(this.velocity); this.acceleration.mult(0); } attract(body) { let force = p5.Vector.sub(this.position, body.position); let d = constrain(force.mag(), 32, 256); let strength = ((this.mass * body.mass)) / (d * d); force.setMag(strength); return force; } show() { noStroke(); fill(this.rC, this.gC, this.bC); circle(this.position.x, this.position.y, this.mass * 6); } }

¿Qué estamos viendo?

Esto es una pequeña muestra de lo que te permite crear el libro The Nature of Code: Simulating Natural Systems with JavaScript con solo haber leído los capítulos 1 y 2.

Aunque de una manera artificial y fantasiosa, pero visualmente interesante, los programas anteriores intentan simular un poco cómo actúa la fuerza gravitatoria sobre los cuerpos celestes.

El desafío es que hagas tu propia versión, ya sea más realista, o más ficticia. Vos fijate.