Add rotating colored obstacles to scene1 and fix Body import

This commit is contained in:
Daddy32
2025-12-12 19:08:51 +01:00
parent fb56db605f
commit 3ea13ceaf9

26
main.js
View File

@@ -4,6 +4,7 @@
Render,
Runner,
World,
Body,
Bodies,
Constraint,
Events,
@@ -50,16 +51,27 @@
Bodies.rectangle(w / 2, h + 40, w, 80, {
isStatic: true,
restitution: 0.8,
render: { fillStyle: "#0ea5e9", strokeStyle: "#0ea5e9" },
}),
Bodies.rectangle(-40, h / 2, 80, h * 2, {
isStatic: true,
render: { fillStyle: "#f97316", strokeStyle: "#f97316" },
}),
Bodies.rectangle(w + 40, h / 2, 80, h * 2, {
isStatic: true,
render: { fillStyle: "#f97316", strokeStyle: "#f97316" },
}),
Bodies.rectangle(-40, h / 2, 80, h * 2, { isStatic: true }),
Bodies.rectangle(w + 40, h / 2, 80, h * 2, { isStatic: true }),
Bodies.rectangle(w * 0.25, h * 0.55, 160, 20, {
isStatic: true,
angle: -0.3,
render: { fillStyle: "#22c55e", strokeStyle: "#22c55e" },
plugin: { rotSpeed: 0.1 },
}),
Bodies.rectangle(w * 0.7, h * 0.4, 220, 24, {
isStatic: true,
angle: 0.26,
render: { fillStyle: "#a855f7", strokeStyle: "#a855f7" },
plugin: { rotSpeed: -0.08 },
}),
],
},
@@ -192,11 +204,13 @@
// Static boundaries and scene-specific obstacles.
let boundaries = [];
let rotators = [];
let currentScene = scenes[0];
const rebuildSceneBodies = () => {
boundaries.forEach((b) => World.remove(world, b));
boundaries = currentScene.createBodies(width, height);
rotators = boundaries.filter((b) => b.plugin && b.plugin.rotSpeed);
World.add(world, boundaries);
};
@@ -616,6 +630,14 @@
c.stiffness = c.plugin.baseStiffness ?? c.stiffness;
}
});
// Rotate any scene rotators slowly.
const dt = (engine.timing && engine.timing.delta) || 16;
rotators.forEach((b) => {
const speed = b.plugin.rotSpeed || 0;
if (speed !== 0) {
Body.rotate(b, speed * (dt / 1000));
}
});
});
Events.on(render, "afterRender", () => {