It was supposed to be based on gravity equation, but it did not work out well, I changed square of distance with just distance, now it produces some nice patterns.
Parameters randomized each run, open browser console to check currently used parameters, if you want repeat pattern.
Log in to post a comment.
const g = 10 + 5 * Math.random(); const bx = 2 * Math.random(); const by = -10 - 130 * Math.random(); const dt = 0.0001 + 0.0009 * Math.random(); console.log('g', g, 'bx', bx, 'by', by, 'dt', dt); class Vector { constructor(x, y) { this.x = x; this.y = y; } mul(a) { return new Vector(this.x * a, this.y * a); } sub(v) { return new Vector(this.x - v.x, this.y - v.y); } add(v) { return new Vector(this.x + v.x, this.y + v.y); } len() { return Math.sqrt(this.x * this.x + this.y * this.y); } lenSqr() { return this.x * this.x + this.y * this.y; } norm() { return this.mul(1 / this.len()); } } class Body { constructor(position, speed, mass) { this.position = position; this.speed = speed; this.mass = mass; } } function simulate(dt, staticBody, body) { let bbV = staticBody.position.sub(body.position); let fAbs = g * body.mass * staticBody.mass / bbV.len(); let f = bbV.norm().mul(fAbs); let a = f.mul(1 / body.mass); let dv = a.mul(dt); let dr = body.speed.mul(dt); body.position = body.position.add(dr); body.speed = body.speed.add(dv); } let body = new Body(new Vector(50, 50), new Vector(bx, by), 10); let staticBody = new Body(new Vector(0, 0), new Vector(0, 0), 1000); Canvas.setpenopacity(0.8); const turtle = new Turtle(); turtle.penup(); turtle.goto(body.position.x, body.position.y); turtle.pendown(); function walk(i) { staticBody.position = new Vector(0, 0).add(new Vector(20*Math.cos(i/150000), 20*Math.sin(i/150000))); for (let j=0; j<10; j++) { simulate(dt, staticBody, body); } turtle.goto(body.position.x, body.position.y); return i < 4000000; }