Turn the paddles to move the puppet.
Export to GIF for an animated version.
Log in to post a comment.
// LL 2021
const count = 1; // min=1 max=10 step=1
const gravity = 0.3; /// min=0 max=1 step=0.001
const seed = 35; /// min=0 max=100 step=1
const offset_y = 90;
const scale = 17; // min=1 max=50 step=1
const puppet_size = 5; // min=1 max=20 step=1
const style = 1; // min=0 max=1 step=1 (Polygons (fast),Polygons (slow))
const paddle_hands = 0.13 // min=-0.5 max=0.5 step=0.01
const paddle_knees = -0.08 // min=-0.5 max=0.5 step=0.01
const string_link_count = 40;
const thickness = 4; /// min=1 max=100 step=1
const iterations = 450; // min=0 max=500 step=1
const variation = 0.08; /// min=0 max=0.2 step=0.01
Canvas.setpenopacity(1);
const turtle = new Turtle();
var polygons = null;
var particles = [];
var things_to_draw = [];
var iterations_t = iterations;
console.clear();
class Particle {
constructor(x, y, px, py, radius, mass=1, floor=0, no_collision=false) {
this.x = x;
this.y = y;
this.px = px;
this.py = py;
this.radius = radius;
this.floor = 0;
this.no_collision = no_collision;
this.mass = mass;
}
dup() {
return new Particle(this.x, this.y, this.px, this.py, this.radius, this.mass, this.floor, this.no_collision);
}
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 += this.mass * 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;
}
}
function drawPoints(points) {
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 Limb {
constructor(index1, index2, shaded=false) {
this.index1 = index1;
this.index2 = index2;
this.shaded = shaded;
this.spring = new Spring(index1, index2);
}
draw() {
const astep = 1 / 50;
const tstep = .25 / scale;
if (this.index2 !== undefined) {
for (var layer=0; layer<thickness; layer++) {
const inset = (thickness-1-layer) * tstep;
const x1 = particles[this.index2].x;
const y1 = particles[this.index2].y;
const r1 = particles[this.index2].radius * 0.2 - inset;
if (r1 < tstep*0.2) continue;
var points = [];
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]);
}
drawPoints(points);
}
}
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]);
}
}
drawPoints(points);
}
}
update() {
this.spring.update();
}
}
class Spring {
constructor(index1, index2) {
this.index1 = index1;
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 = 0.5;
particles[this.index1].x += (x1 - x2) / l * diff * strength;
particles[this.index1].y += (y1 - y2) / l * diff * strength;
particles[this.index2].x += (x2 - x1) / l * diff * strength;
particles[this.index2].y += (y2 - y1) / l * diff * strength;
}
}
}
class String {
constructor(x, y, target_x, target_y, particle_index, attach_y) {
this.particle_index = particle_index;
this.x = x;
this.y = y;
this.target_x = target_x;
this.target_y = target_y;
this.attach_y = attach_y;
this.particles = [];
this.links = [];
var last_link_index = this.addParticle(this.x, this.y);
for (var l=1; l<=string_link_count; l++) {
x = this.x + (particles[particle_index].x - this.x) * l / string_link_count;
y = this.y + (particles[particle_index].y + this.attach_y - this.y) * l / string_link_count;
var current_link_index = this.addParticle(x, y);
this.links.push(new Spring(last_link_index, current_link_index));
last_link_index = current_link_index;
}
particles[this.links[0].index1].x = this.target_x;
particles[this.links[0].index1].y = this.target_y;
}
addParticle(x, y) {
var particle = new Particle(x, y, x, y, 0.01, 0.1, 0, true);
this.particles.push(particle);
particles.push(particle);
return particles.length - 1;
}
update() {
if (this.links.length < 1) return;
this.particles.forEach(p => p.update());
for (var i=0; i<10; i++) {
particles[this.links[0].index1].x = this.target_x;
particles[this.links[0].index1].y = this.target_y;
particles[this.links[this.links.length-1].index2].x = particles[this.particle_index].x;
particles[this.links[this.links.length-1].index2].y = particles[this.particle_index].y + this.attach_y;
this.links.forEach(l => l.update());
particles[this.particle_index].x = particles[this.links[this.links.length-1].index2].x;
particles[this.particle_index].y = particles[this.links[this.links.length-1].index2].y - this.attach_y;
}
}
draw() {
if (this.links.length < 1) return;
const points = [];
const inset = 0.3;
points.push([particles[this.links[0].index1].x * scale, particles[this.links[0].index1].y * scale + offset_y])
this.links.forEach(l => {
points.push( [particles[l.index2].x * scale + inset, particles[l.index2].y * scale + offset_y]);
points.unshift([particles[l.index2].x * scale - inset, particles[l.index2].y * scale + offset_y]);
});
drawPoints(points);
}
}
class Paddle {
constructor(height, attach_y, paddle_angle, index1, index2) {
this.strings = [];
if (index2 === undefined) {
var x = 0, y = -height;
this.strings.push(new String(x, y, x, y+1, index1, attach_y));
} else {
const len = 3
var cx = 0, cy = -height;
var x1 = cx + len * Math.cos(paddle_angle * Math.PI);
var y1 = cy + len * Math.sin(paddle_angle * Math.PI);
var x2 = cx - len * Math.cos(paddle_angle * Math.PI);
var y2 = cy - len * Math.sin(paddle_angle * Math.PI);
this.strings.push(new String(cx + len, cy, x1, y1, index1, attach_y));
this.strings.push(new String(cx - len, cy, x2, y2, index2, attach_y));
}
}
update() {
this.strings.forEach(s => s.update());
}
draw() {
this.strings.forEach(s => s.draw());
}
}
class Puppet {
constructor(x, y, size) {
const body_radius = 0.05;
const head_radius = 0.12;
y += -body_radius * size;
this.limbs = [];
this.particles = [];
const start_index = particles.length;
this.head_index = this.addParticle(x, y, size, 0, -1.0, head_radius, 1);
const neck_index = this.addParticle(x, y, size, 0, -0.8, body_radius, 0.1);
const belly_index = this.addParticle(x, y, size, 0, -0.6, body_radius, 1);
const bottom_index = this.addParticle(x, y, size, 0, -0.4, body_radius, 0.1);
const left_elbow_index = this.addParticle(x, y, size, -0.2, -0.8, body_radius, 0.1);
const left_hand_index = this.addParticle(x, y, size, -0.4, -0.8, body_radius, 1);
const right_elbow_index = this.addParticle(x, y, size, 0.2, -0.8, body_radius, 0.1);
const right_hand_index = this.addParticle(x, y, size, 0.4, -0.8, body_radius, 1);
const left_knee_index = this.addParticle(x, y, size, -0.1, -0.2, body_radius, 0.1);
const left_foot_index = this.addParticle(x, y, size, -0.2, 0, body_radius, 1);
const right_knee_index = this.addParticle(x, y, size, 0.1, -0.2, body_radius, 0.1);
const right_foot_index = this.addParticle(x, y, size, 0.2, 0, body_radius, 1);
// Create the limbs front to back in draw order
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(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(left_foot_index, left_knee_index, true));
this.limbs.push(new Limb(bottom_index, left_knee_index, true));
this.limbs.push(new Limb(left_hand_index, left_elbow_index, true));
this.limbs.push(new Limb(left_elbow_index, neck_index, true));
this.paddles = [];
const height = 4;
this.paddles.push(new Paddle(size*height, 0, -paddle_knees, right_knee_index, left_knee_index));
this.paddles.push(new Paddle(size*height, -particles[left_hand_index].radius, -paddle_hands, right_hand_index, left_hand_index));
this.paddles.push(new Paddle(size*height, -head_radius*size, 0, this.head_index));
}
addParticle(ox, oy, size, x, y, radius, mass) {
var particle = new Particle(ox + x*size, oy + y*size, ox + x*size, oy + y*size, radius*size, mass);
this.particles.push(particle);
particles.push(particle);
return particles.length - 1;
}
update() {
this.particles.forEach(p => p.update());
this.paddles.forEach(p => p.update());
this.limbs.forEach(l => l.update());
}
queueDraw() {
this.paddles.forEach(p => things_to_draw.push(p));
this.limbs.forEach(l => things_to_draw.push(l));
}
}
function resolveCollisions() {
for (var i1=0; i1<particles.length; i1++) {
if (particles[i1].no_collision) continue;
for (var i2=i1+1; i2<particles.length; i2++) {
if (particles[i2].no_collision) 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;
return Math.hypot(x1-x2, y1-y2);
}
function drawFloor() {
for (var layer=0; layer<thickness; layer++) {
const tstep = .25;
const inset = (thickness-1-layer) * tstep;
turtle.jump(-95, offset_y+inset); turtle.goto(95, offset_y+inset);
}
}
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 = puppet_size;
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.5;
var puppet = new Puppet(x, y, size);
puppets.push(puppet);
}
for (var it=0; it<iterations_t; it++) {
puppets.forEach(puppet => {
puppet.update();
resolveCollisions();
});
}
puppets.forEach(puppet => {
puppet.queueDraw();
});
drawFloor();
}
if (things_to_draw.length < 1) {
return false;
}
things_to_draw.shift().draw();
return true;
}
////
function sleep(milliseconds) { start=Date.now(); while (Date.now()-start<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);
}
};
}