I’m trying to create an animated pop-up card. I can figure out how to animate folding planes when the folds are parallel, but am having trouble figuring out angled folds that increase the angular velocity of the moving parts. Are there any existing techniques to implement this? or are there additional libraries that are compatible with P5.js that would help? Here is my code thus far:
let angle = 0; // Angle for the folding motion
function setup() {
createCanvas(600, 600, WEBGL);
}
function draw() {
angleMode(DEGREES);
background(200);
stroke('#03A9F4')
orbitControl();
lights();
// Smooth folding motion: From closed (0 degrees) to open (90 degrees)
angle = map(sin(frameCount*1.5), 0, 1, 45, 0);
angle2 = map(sin(frameCount* 1.5), 0, 1, 45, 0);
// First plane (rotating around the vertical hinge edge)
push();
fill(150, 100, 200);
rotateY(angle); // Rotate toward the hinge (opposite direction)
translate(-100, 0, 0); // Position plane to the left of the hinge
makeShape(200, 200);
push();
fill(100,150,200);
translate(50, 0, 0);
rotate(angle2 * 2,[1,-1,0]);
beginShape();
vertex(50, -50, 0); // Top-left corner
// vertex(100 / 2, -100 / 2, 0); // Top-right corner
vertex(50, 50, 0); // Bottom-right corner
vertex(-50, 50, 0); // Bottom-left corner
endShape(CLOSE);
pop();
pop();
// Second plane (rotating around the vertical hinge edge)
push();
fill(100, 150, 200);
rotateY(-angle); // Rotate toward the hinge (opposite direction)
translate(100, 0, 0); // Position plane to the right of the hinge
makeShape(200, 200);
push();
fill(150,100,200);
translate(-50,0,0);
rotate(-angle2 * 2,[-1,-1,0]);
beginShape();
vertex(-50, -50, 0); // Top-left corner
// vertex(100 / 2, -100 / 2, 0); // Top-right corner
vertex(50, 50, 0); // Bottom-right corner
vertex(-50, 50, 0); // Bottom-left corner
endShape(CLOSE);
pop();
pop();
}
function makeShape(w, h) {
beginShape();
vertex(-w / 2, -h / 2, 0); // Top-left corner
vertex(w / 2, -h / 2, 0); // Top-right corner
vertex(w / 2, h / 2, 0); // Bottom-right corner
vertex(-w / 2, h / 2, 0); // Bottom-left corner
endShape(CLOSE);
}
Any help would be greatly appreciated!