Write a JavaScript program using an HTML Canvas to simulate four balls inside a rotating…
- User
Write a JavaScript program using an HTML Canvas to simulate four balls inside a rotating rectangular container.
Container:
A rectangle (width W, height H) centered at (W/2, H/2) on the canvas.
Rotates continuously around its center at a fixed angular velocity ω (e.g. 0.5 rad/s).
Gravity:
A uniform acceleration g = 9.8 m/s² directed downward toward the bottom edge of the canvas (independent of the container’s rotation).
Balls:
Four circles of equal radius and mass.
Initial positions and velocities are up to you (just keep them inside the container at start).
Physics requirements:
Implement elastic collisions between balls and between each ball and the container walls.
When a ball collides with a wall, account for the wall’s instantaneous velocity due to rotation when computing the collision response.
Apply gravity each frame to the balls’ vertical velocity.
Implementation constraints:
Use plain ES6 JavaScript and the Canvas API only (no external physics libraries).
Target 60 fps with requestAnimationFrame.
Structure your code so it’s easy to adjust parameters (g, ω, restitution coefficient).
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.