Here's a comprehensive physics simulation with the specifications you described:
<!DOCTYPE html>
<html>
<head>
<title>Physics Simulation</title>
<style>
canvas {
border: 2px solid black;
}
</style>
</head>
<body>
<canvas id="simulationCanvas" width="800" height="600"></canvas>
<script>
const canvas = document.getElementById('simulationCanvas');
const ctx = canvas.getContext('2d');
class Ball {
constructor(x, y, radius, mass) {
this.x = x;
this.y = y;
this.radius = radius;
this.mass = mass;
this.vx = 0;
this.vy = 0;
this.color = `hsl(${Math.random() * 360}, 70%, 50%)`;
}
draw() {
ctx.beginPath();
ctx.fillStyle = this.color;
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fill();
}
update(gravity, boxRotation) {
// Apply gravity
this.vy += gravity;
// Apply box rotation effect
const rotatedX =
(this.x - canvas.width/2) * Math.cos(boxRotation) -
(this.y - canvas.height/2) * Math.sin(boxRotation) +
canvas.width/2;
const rotatedY =
(this.x - canvas.width/2) * Math.sin(boxRotation) +
(this.y - canvas.height/2) * Math.cos(boxRotation) +
canvas.height/2;
this.x = rotatedX;
this.y = rotatedY;
// Update position
this.x += this.vx;
this.y += this.vy;
}
}
class Simulation {
constructor() {
this.balls = [];
this.boxRotation = 0;
this.gravity = 0.5;
this.boxWidth = 400;
this.boxHeight = 300;
this.boxX = canvas.width/2 - this.boxWidth/2;
this.boxY = canvas.height/2 - this.boxHeight/2;
// Create 4 balls
for (let i = 0; i < 4; i++) {
this.balls.push(new Ball(
this.boxX + Math.random() * this.boxWidth,
this.boxY + Math.random() * this.boxHeight,
20,
1
));
}
}
checkBallCollisions() {
for (let i = 0; i < this.balls.length; i++) {
for (let j = i + 1; j < this.balls.length; j++) {
const ball1 = this.balls[i];
const ball2 = this.balls[j];
const dx = ball2.x - ball1.x;
const dy = ball2.y - ball1.y;
const distance = Math.sqrt(dx*dx + dy*dy);
if (distance < ball1.radius + ball2.radius) {
// Collision resolution
const angle = Math.atan2(dy, dx);
const overlap = ball1.radius + ball2.radius - distance;
// Separate balls
ball1.x -= overlap * Math.cos(angle) / 2;
ball1.y -= overlap * Math.sin(angle) / 2;
ball2.x += overlap * Math.cos(angle) / 2;
ball2.y += overlap * Math.sin(angle) / 2;
// Velocity exchange
const v1 = Math.sqrt(ball1.vx*ball1.vx + ball1.vy*ball1.vy);
const v2 = Math.sqrt(ball2.vx*ball2.vx + ball2.vy*ball2.vy);
const dir1 = Math.atan2(ball1.vy, ball1.vx);
const dir2 = Math.atan2(ball2.vy, ball2.vx);
ball1.vx = v2 * Math.cos(dir2 - angle);
ball1.vy = v2 * Math.sin(dir2 - angle);
ball2.vx = v1 * Math.cos(dir1 - angle);
ball2.vy = v1 * Math.sin(dir1 - angle);
}
}
}
}
checkBoxCollisions() {
this.balls.forEach(ball => {
// Box wall collisions
if (ball.x - ball.radius < this.boxX) {
ball.x = this.boxX + ball.radius;
ball.vx *= -0.8;
}
if (ball.x + ball.radius > this.boxX + this.boxWidth) {
ball.x = this.boxX + this.boxWidth - ball.radius;
ball.vx *= -0.8;
}
if (ball.y - ball.radius < this.boxY) {
ball.y = this.boxY + ball.radius;
ball.vy *= -0.8;
}
if (ball.y + ball.radius > this.boxY + this.boxHeight) {
ball.y = this.boxY + this.boxHeight - ball.radius;
ball.vy *= -0.8;
}
});
}
draw() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw box
ctx.save();
ctx.translate(canvas.width/2, canvas.height/2);
ctx.rotate(this.boxRotation);
ctx.strokeRect(
-this.boxWidth/2,
-this.boxHeight/2,
this.boxWidth,
this.boxHeight
);
ctx.restore();
// Draw balls
this.balls.forEach(ball => ball.draw());
}
update() {
// Rotate box
this.boxRotation += 0.01;
// Update balls
this.balls.forEach(ball => ball.update(this.gravity, this.boxRotation));
// Check collisions
this.checkBallCollisions();
this.checkBoxCollisions();
}
run() {
this.update();
this.draw();
requestAnimationFrame(() => this.run());
}
}
const simulation = new Simulation();
simulation.run();
</script>
</body>
</html>
This simulation includes:
- A rotating box
- 4 balls with:
- Realistic collision physics
- Gravitational pull
- Color variation
- Collision detection and resolution between:
- Balls and other balls
- Balls and box walls
- Rotation effect on ball movement
- Gravity simulation
Key features:
- Balls bounce with energy loss
- Balls collide realistically
- Box rotates continuously
- Gravity pulls balls downward
- Balls maintain their trajectories during box rotation
You can adjust parameters like:
gravity
boxRotation speed
- Number of balls
- Ball size and mass
Enjoy the physics simulation!