Ragdolls frolicking on the street.
Log in to post a comment.
// LL 2021 const count = 4; // min=1 max=10 step=1 const thickness = 4; // min=1 max=100 step=1 const iterations = 0; // min=0 max=1000 step=1 const gravity = 0.5; // min=0 max=1 step=0.001 const impulse = 0; //min=0 max=10 step=0.1 const variation = 0.08; // min=0 max=0.2 step=0.01 const seed = 35; // min=0 max=100 step=1 const offset_y = 70; const scale = 12; // min=1 max=50 step=1 const style = 1; // min=0 max=1 step=1 (Polygons (fast),Polygons (slow)) const draw_crossing = 1; // min=0 max=1 step=1 (No, Yes) Canvas.setpenopacity(1); const turtle = new Turtle(); var polygons = null; var particles = []; var limbs_to_draw = []; var iterations_t = iterations; console.clear(); class Particle { constructor(x, y, px, py, radius, floor=0, heavy=false) { this.x = x; this.y = y; this.px = px; this.py = py; this.radius = radius; this.floor = 0; this.heavy = heavy; } dup() { return new Particle(this.x, this.y, this.px, this.py, this.radius, this.floor, this.heavy); } update() { var x = this.x, y = this.y, px = this.px, py = this.py; this.px = this.x; this.py = this.y; // Verlet const friction = 0.99; x += (x - px) * friction; y += (y - py) * friction; y += gravity / 100; const l = getDistance(x, y); const rx = rotX(x/l, y/l, Math.PI / 2); const ry = rotY(x/l, y/l, Math.PI / 2); var dx = x - this.x; var dy = y - this.y; const l2 = getDistance(dx, dy); const max_distance = 0.03; if (l2 > max_distance) { dx = dx / l2 * max_distance; dy = dy / l2 * max_distance; } this.x += dx; this.y += dy; if (this.y > this.floor - this.radius) this.y = this.floor - this.radius; } } class Limb { constructor(index1, index2, shaded=false) { this.index1 = index1; this.index2 = index2; this.shaded = shaded; } draw() { const astep = 1 / 50; const tstep = .25 / scale; for (var layer=0; layer<thickness; layer++) { const inset = (thickness-1-layer) * tstep; const points = []; const x1 = particles[this.index1].x; const y1 = particles[this.index1].y; const r1 = particles[this.index1].radius - inset; if (r1 < tstep*0.2) continue; if (this.index2 === undefined) { for (var a = -1; a <= 1; a += astep) { const dx = rotX(1, 0, a * Math.PI); const dy = rotY(1, 0, a * Math.PI); var px = (x1 + r1 * dx) * scale ; var py = (y1 + r1 * dy) * scale + offset_y; points.push([px, py]); } } else { const x2 = particles[this.index2].x; const y2 = particles[this.index2].y; const r2 = particles[this.index2].radius - inset; if (r2 < tstep*0.2) continue; const l = getDistance(x1, y1, x2, y2); for (var a = -0.5; a <= 0.5; a += astep) { const dx = rotX(x1 - x2, y1 - y2, a * Math.PI) / l; const dy = rotY(x1 - x2, y1 - y2, a * Math.PI) / l; var px = (x1 + r1 * dx) * scale ; var py = (y1 + r1 * dy) * scale + offset_y; points.push([px, py]); } for (var a = -0.5; a <= 0.5; a += astep) { const dx = rotX(x2 - x1, y2 - y1, a * Math.PI) / l; const dy = rotY(x2 - x1, y2 - y1, a * Math.PI) / l; var px = (x2 + r2 * dx) * scale ; var py = (y2 + r2 * dy) * scale + offset_y; points.push([px, py]); } } if (style == 0) { turtle.jump(points[points.length-1]); points.forEach(p=>turtle.goto(p)); } else { const p1 = polygons.create(); p1.addPoints(...points); if (this.shaded) p1.addHatching(-Math.PI/4, 2); p1.addOutline(); polygons.draw(turtle, p1, true); } } } } class Spring { constructor(index1, index2, is_weak) { this.index1 = index1; this.is_weak = is_weak; if (index2 !== undefined) { this.index2 = index2; this.length = getDistance(particles[index1].x, particles[index1].y, particles[index2].x, particles[index2].y); } } reset() { if (this.index2 !== undefined) { this.length = getDistance(particles[this.index1].x, particles[this.index1].y, particles[this.index2].x, particles[this.index2].y); } } update() { if (this.shaded === undefined && this.index2 !== undefined) { var x1 = particles[this.index1].x; var y1 = particles[this.index1].y; var x2 = particles[this.index2].x; var y2 = particles[this.index2].y; const l = Math.max(0.01, getDistance(x1, y1, x2, y2)); const diff = this.length - l; const strength = this.is_weak ? 0.005 : 0.2; // if (!particles[this.index1].heavy) { particles[this.index1].x += (x1 - x2) / l * diff * strength; particles[this.index1].y += (y1 - y2) / l * diff * strength; // } // if (!particles[this.index2].heavy) { particles[this.index2].x += (x2 - x1) / l * diff * strength; particles[this.index2].y += (y2 - y1) / l * diff * strength; // } } } } class Puppet { constructor(x, y, size) { const body_radius = 0.06; const head_radius = 0.14; y += -body_radius * size; this.limbs = []; this.springs = []; this.particles = []; const start_index = particles.length; this.head_index = this.addParticle(x, y, size, 0, -1.0, head_radius); const neck_index = this.addParticle(x, y, size, 0, -0.8, body_radius); const belly_index = this.addParticle(x, y, size, 0, -0.6, body_radius); const bottom_index = this.addParticle(x, y, size, 0, -0.4, body_radius); const left_elbow_index = this.addParticle(x, y, size, -0.175, -0.75, body_radius); const left_hand_index = this.addParticle(x, y, size, -0.35, -0.6, body_radius); const right_elbow_index = this.addParticle(x, y, size, 0.175, -0.65, body_radius); const right_hand_index = this.addParticle(x, y, size, 0.35, -0.8, body_radius); const left_knee_index = this.addParticle(x, y, size, -0.09, -0.2, body_radius); const left_foot_index = this.addParticle(x, y, size, -0.25, 0, body_radius); const right_knee_index = this.addParticle(x, y, size, 0.2, -0.25, body_radius); const right_foot_index = this.addParticle(x, y, size, 0.2, 0, body_radius); // Create the limbs front to back in draw order this.limbs.push(new Limb(right_hand_index, right_elbow_index)); this.limbs.push(new Limb(right_elbow_index, neck_index)); this.limbs.push(new Limb(right_foot_index, right_knee_index)); this.limbs.push(new Limb(right_knee_index, bottom_index)); this.limbs.push(new Limb(this.head_index)); // Just to draw the head this.limbs.push(new Limb(this.head_index, neck_index)); this.limbs.push(new Limb(neck_index, belly_index)); this.limbs.push(new Limb(belly_index, bottom_index)); this.limbs.push(new Limb(left_knee_index, bottom_index, true)); this.limbs.push(new Limb(left_foot_index, left_knee_index, true)); this.limbs.push(new Limb(left_elbow_index, neck_index, true)); this.limbs.push(new Limb(left_hand_index, left_elbow_index, true)); for (var i1=0; i1<this.particles.length; i1++) { for (var i2=i1+1; i2<this.particles.length; i2++) { const is_weak = !this.isLimb(start_index+i1, start_index+i2); this.springs.push(new Spring(start_index+i1, start_index+i2, is_weak)); } } particles[this.head_index].heavy = true; particles[this.head_index].floor = -size * 0.9; } pose() { this.particles.forEach(p => { p.x += variation * (rng.nextFloat() - 0.5) * 10; p.y += variation * (rng.nextFloat() - 0.5) * 10; }); for (var i=0; i<10; i++) { this.springs.forEach(s => { if (s.is_weak) { s.reset(); } else s.update(); }); } } impulse() { this.particles.forEach(p => { p.px += impulse * (rng.nextFloat() - 0.5) * 10; p.py += impulse * (rng.nextFloat() - 0.5) * 10; }); } isLimb(index1, index2) { var is_limb = false; this.limbs.forEach(l => { is_limb |= (l.index1==index1 && l.index2==index2); is_limb |= (l.index1==index2 && l.index2==index1); }); return is_limb; } addParticle(ox, oy, size, x, y, radius) { var particle = new Particle(ox + x*size, oy + y*size, ox + x*size, oy + y*size, radius*size); this.particles.push(particle); particles.push(particle); return particles.length - 1; } update() { this.particles.forEach(p => p.update()); this.springs.forEach(s => s.update()); } queueDraw() { this.limbs.forEach(l => { limbs_to_draw.push(l); }); } } function resolveCollisions() { for (var i1=0; i1<particles.length; i1++) { //if (!particles[i1].heavy) continue; for (var i2=i1+1; i2<particles.length; i2++) { //if (!particles[i2].heavy) continue; var x1 = particles[i1].x; var y1 = particles[i1].y; var r1 = particles[i1].radius; var x2 = particles[i2].x; var y2 = particles[i2].y; var r2 = particles[i2].radius; const l = Math.max(0.01, getDistance(x1, y1, x2, y2)); if (l < (r1 + r2)) { const diff = r1 + r2 - l; const strength = 0.5; particles[i1].x += (x1 - x2) / l * diff * strength; particles[i1].y += (y1 - y2) / l * diff * strength; particles[i2].x += (x2 - x1) / l * diff * strength; particles[i2].y += (y2 - y1) / l * diff * strength; } } } } function rotX(x, y, a) { return Math.cos(a) * x - Math.sin(a) * y; } function rotY(x, y, a) { return Math.sin(a) * x + Math.cos(a) * y; } function getDistance(x1, y1, x2, y2) { if (x2 === undefined) x2 = 0; if (y2 === undefined) y2 = 0; const dx = x1 - x2, dy = y1 - y2; return Math.sqrt(dx*dx + dy*dy); } function drawRoad(size) { const points = []; const near = 2, far = -10, perspective = 5; points.push( [-size * scale, near * scale + offset_y] ); points.push( [ size * scale, near * scale + offset_y] ); points.push( [ size/perspective * scale, far * scale + offset_y] ); points.push( [-size/perspective * scale, far * scale + offset_y] ); const p1 = polygons.create(); p1.addPoints(...points); p1.addHatching(-Math.PI/4, 1); p1.addOutline(); polygons.draw(turtle, p1, true); } function drawCrossing(size) { const markings = 6; const near = 1, far = -4, perspective = 1.75; for (var i=0; i<markings; i++) { const points = []; points.push( [(-size + size * 1.8 * (i+0.0) / (markings-1)) * scale, near * scale + offset_y] ); points.push( [(-size + size * 1.8 * (i+0.5) / (markings-1)) * scale, near * scale + offset_y] ); points.push( [(-size + size * 1.8 * (i+0.5) / (markings-1))/perspective * scale, far * scale + offset_y] ); points.push( [(-size + size * 1.8 * (i+0.0) / (markings-1))/perspective * scale, far * scale + offset_y] ); const p1 = polygons.create(); p1.addPoints(...points); p1.addOutline(); polygons.draw(turtle, p1, true); } drawRoad(size * 1.2); } function walk(i, t) { if (i==0) { iterations_t = iterations * t; rng = new RNG(seed); particles = []; strings_to_draw = []; polygons = new Polygons(); const puppets = []; for (var c=0; c<count; c++) { const size = 5; const x = (c - (count-1) * 0.5) * size * 0.8; //const x = (c / Math.max(0.5, count-1) - 0.5) * size; const y = 0; var puppet = new Puppet(x, 0, 5); puppet.pose(); puppets.push(puppet); } for (var it=0; it<iterations_t; it++) { puppets.forEach(puppet => { if ((it%10) == 0) puppet.impulse(); puppet.update(); resolveCollisions(); }); } puppets.forEach(puppet => { puppet.queueDraw(); // puppet.springs.forEach(s => { // if (s.is_weak) { // turtle.jump(particles[s.index1].x * scale, particles[s.index1].y * scale + offset_y); // turtle.goto(particles[s.index2].x * scale, particles[s.index2].y * scale + offset_y); // } // }); }); } if (limbs_to_draw.length < 1) { if (style == 1 && draw_crossing) drawCrossing(20); return false; } limbs_to_draw.shift().draw(); return true; } //// function sleep(milliseconds) { const date = Date.now(); let currentDate = null; do { currentDate = Date.now(); } while (currentDate - date < milliseconds); } //// Random with seed function RNG(_seed) { // LCG using GCC's constants this.m = 0x80000000; // 2**31; this.a = 1103515245; this.c = 12345; this.state = _seed ? _seed : Math.floor(Math.random() * (this.m - 1)); } RNG.prototype.nextFloat = function() { // returns in range [0,1] this.state = (this.a * this.state + this.c) % this.m; return this.state / (this.m - 1); } var rng = new RNG(seed); //////////////////////////////////////////////////////////////// // Polygon Clipping utility code - Created by Reinder Nijhoff 2019 // https://turtletoy.net/turtle/a5befa1f8d //////////////////////////////////////////////////////////////// function Polygons() { const polygonList = []; const Polygon = class { constructor() { this.cp = []; // clip path: array of [x,y] pairs this.dp = []; // 2d lines [x0,y0],[x1,y1] to draw this.aabb = []; // AABB bounding box } addPoints(...points) { // add point to clip path and update bounding box let xmin = 1e5, xmax = -1e5, ymin = 1e5, ymax = -1e5; (this.cp = [...this.cp, ...points]).forEach( p => { xmin = Math.min(xmin, p[0]), xmax = Math.max(xmax, p[0]); ymin = Math.min(ymin, p[1]), ymax = Math.max(ymax, p[1]); }); this.aabb = [(xmin+xmax)/2, (ymin+ymax)/2, (xmax-xmin)/2, (ymax-ymin)/2]; } addSegments(...points) { // add segments (each a pair of points) points.forEach(p => this.dp.push(p)); } addOutline() { for (let i = 0, l = this.cp.length; i < l; i++) { this.dp.push(this.cp[i], this.cp[(i + 1) % l]); } } draw(t) { for (let i = 0, l = this.dp.length; i < l; i+=2) { t.jump(this.dp[i]), t.goto(this.dp[i + 1]); } } addHatching(a, d) { const tp = new Polygon(); tp.cp.push([-1e5,-1e5],[1e5,-1e5],[1e5,1e5],[-1e5,1e5]); const dx = Math.sin(a) * d, dy = Math.cos(a) * d; const cx = Math.sin(a) * 200, cy = Math.cos(a) * 200; for (let i = 0.5; i < 150 / d; i++) { tp.dp.push([dx * i + cy, dy * i - cx], [dx * i - cy, dy * i + cx]); tp.dp.push([-dx * i + cy, -dy * i - cx], [-dx * i - cy, -dy * i + cx]); } tp.boolean(this, false); this.dp = [...this.dp, ...tp.dp]; } inside(p) { let int = 0; // find number of i ntersection points from p to far away for (let i = 0, l = this.cp.length; i < l; i++) { if (this.segment_intersect(p, [0.1, -1000], this.cp[i], this.cp[(i + 1) % l])) { int++; } } return int & 1; // if even your outside } boolean(p, diff = true) { // bouding box optimization by ge1doot. if (Math.abs(this.aabb[0] - p.aabb[0]) - (p.aabb[2] + this.aabb[2]) >= 0 && Math.abs(this.aabb[1] - p.aabb[1]) - (p.aabb[3] + this.aabb[3]) >= 0) return this.dp.length > 0; // polygon diff algorithm (narrow phase) const ndp = []; for (let i = 0, l = this.dp.length; i < l; i+=2) { const ls0 = this.dp[i]; const ls1 = this.dp[i + 1]; // find all intersections with clip path const int = []; for (let j = 0, cl = p.cp.length; j < cl; j++) { const pint = this.segment_intersect(ls0, ls1, p.cp[j], p.cp[(j + 1) % cl]); if (pint !== false) { int.push(pint); } } if (int.length === 0) { // 0 intersections, inside or outside? if (diff === !p.inside(ls0)) { ndp.push(ls0, ls1); } } else { int.push(ls0, ls1); // order intersection points on line ls.p1 to ls.p2 const cmpx = ls1[0] - ls0[0]; const cmpy = ls1[1] - ls0[1]; int.sort( (a,b) => (a[0] - ls0[0]) * cmpx + (a[1] - ls0[1]) * cmpy - (b[0] - ls0[0]) * cmpx - (b[1] - ls0[1]) * cmpy); for (let j = 0; j < int.length - 1; j++) { if ((int[j][0] - int[j+1][0])**2 + (int[j][1] - int[j+1][1])**2 >= 0.001) { if (diff === !p.inside([(int[j][0]+int[j+1][0])/2,(int[j][1]+int[j+1][1])/2])) { ndp.push(int[j], int[j+1]); } } } } } return (this.dp = ndp).length > 0; } //port of http://paulbourke.net/geometry/pointlineplane/Helpers.cs segment_intersect(l1p1, l1p2, l2p1, l2p2) { const d = (l2p2[1] - l2p1[1]) * (l1p2[0] - l1p1[0]) - (l2p2[0] - l2p1[0]) * (l1p2[1] - l1p1[1]); if (d === 0) return false; const n_a = (l2p2[0] - l2p1[0]) * (l1p1[1] - l2p1[1]) - (l2p2[1] - l2p1[1]) * (l1p1[0] - l2p1[0]); const n_b = (l1p2[0] - l1p1[0]) * (l1p1[1] - l2p1[1]) - (l1p2[1] - l1p1[1]) * (l1p1[0] - l2p1[0]); const ua = n_a / d; const ub = n_b / d; if (ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1) { return [l1p1[0] + ua * (l1p2[0] - l1p1[0]), l1p1[1] + ua * (l1p2[1] - l1p1[1])]; } return false; } }; return { list: () => polygonList, create: () => new Polygon(), draw: (turtle, p, addToVisList=true) => { for (let j = 0; j < polygonList.length && p.boolean(polygonList[j]); j++); p.draw(turtle); if (addToVisList) polygonList.push(p); } }; }