Physics Simulation in HTML Canvas and JavaScript
Below is a basic implementation of the physics simulation you described. This simulation uses the HTML canvas element and JavaScript to create a rotating box with four balls inside. The balls are affected by gravity and collide with each other and the walls of the box.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Physics Simulation</title>
<style>
#canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="canvas" width="800" height="600"></canvas>
<script src="script.js"></script>
</body>
</html>
JavaScript
// Get the canvas element
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// Set the canvas dimensions
canvas.width = 800;
canvas.height = 600;
// Define the box properties
const box = {
x: canvas.width / 2,
y: canvas.height / 2,
width: 200,
height: 200,
angle: 0,
angularVelocity: 0.01
};
// Define the ball properties
const balls = [
{ x: box.x - 50, y: box.y - 50, vx: 0, vy: 0, radius: 10, mass: 1 },
{ x: box.x + 50, y: box.y - 50, vx: 0, vy: 0, radius: 10, mass: 1 },
{ x: box.x - 50, y: box.y + 50, vx: 0, vy: 0, radius: 10, mass: 1 },
{ x: box.x + 50, y: box.y + 50, vx: 0, vy: 0, radius: 10, mass: 1 }
];
// Define the gravity
const gravity = 0.1;
// Main loop
function update() {
// Clear the canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update the box angle
box.angle += box.angularVelocity;
// Update the balls
for (let i = 0; i < balls.length; i++) {
// Update the ball position
balls[i].x += balls[i].vx;
balls[i].y += balls[i].vy;
// Apply gravity
balls[i].vy += gravity;
// Check for collision with the floor
if (balls[i].y + balls[i].radius > canvas.height) {
balls[i].y = canvas.height - balls[i].radius;
balls[i].vy = -balls[i].vy * 0.9; // Dampen the bounce
}
// Check for collision with the box walls
const boxX = box.x + Math.cos(box.angle) * (balls[i].x - box.x) - Math.sin(box.angle) * (balls[i].y - box.y);
const boxY = box.y + Math.sin(box.angle) * (balls[i].x - box.x) + Math.cos(box.angle) * (balls[i].y - box.y);
if (boxX - box.width / 2 > balls[i].x - balls[i].radius || boxX + box.width / 2 < balls[i].x + balls[i].radius) {
balls[i].vx = -balls[i].vx;
}
if (boxY - box.height / 2 > balls[i].y - balls[i].radius || boxY + box.height / 2 < balls[i].y + balls[i].radius) {
balls[i].vy = -balls[i].vy;
}
// Check for collision with other balls
for (let j = 0; j < balls.length; j++) {
if (i !== j) {
const distance = Math.sqrt((balls[i].x - balls[j].x) ** 2 + (balls[i].y - balls[j].y) ** 2);
if (distance < balls[i].radius + balls[j].radius) {
// Swap velocities
const tempVx = balls[i].vx;
const tempVy = balls[i].vy;
balls[i].vx = balls[j].vx;
balls[i].vy = balls[j].vy;
balls[j].vx = tempVx;
balls[j].vy = tempVy;
}
}
}
}
// Draw the box
ctx.save();
ctx.translate(box.x, box.y);
ctx.rotate(box.angle);
ctx.strokeStyle = 'black';
ctx.strokeRect(-box.width / 2, -box.height / 2, box.width, box.height);
ctx.restore();
// Draw the balls
for (let i = 0; i < balls.length; i++) {
ctx.beginPath();
ctx.arc(balls[i].x, balls[i].y, balls[i].radius, 0, 2 * Math.PI);
ctx.fillStyle = 'blue';
ctx.fill();
}
// Request the next frame
requestAnimationFrame(update);
}
// Start the simulation
update();
This code creates a rotating box with four balls inside. The balls are affected by gravity and collide with each other and the walls of the box. The simulation uses the HTML canvas element and JavaScript to render the scene.
Note that this is a basic implementation and may not be perfect. You may need to adjust the parameters and the collision detection algorithm to get the desired behavior.
Example Use Cases
- You can adjust the
box.angularVelocity variable to change the speed of the box rotation.
- You can adjust the
gravity variable to change the strength of the gravity.
- You can add more balls to the simulation by adding more objects to the
balls array.
- You can change the size and mass of the balls by adjusting the
radius and mass properties of the ball objects.
Advice
- Make sure to adjust the parameters and the collision detection algorithm to get the desired behavior.
- You can use this code as a starting point and add more features to the simulation, such as user input or more complex physics.
- You can also use this code to create a game or a interactive simulation.