Here's an HTML5 canvas script with JavaScript that creates 10 bouncing balls inside a spinning square, with realistic physics including friction and collision energy loss:
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bouncing Balls in Spinning Square</title>
<style>
canvas {
border: 1px solid black;
background-color: #f0f0f0;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
class Ball {
constructor(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
this.dx = Math.random() * 4 - 2;
this.dy = Math.random() * 4 - 2;
this.mass = radius;
this.friction = 0.99;
}
draw() {
ctx.beginPath();
ctx.fillStyle = this.color;
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fill();
ctx.closePath();
}
update(squareRotation) {
// Apply friction
this.dx *= this.friction;
this.dy *= this.friction;
// Rotate velocity based on square rotation
const rotatedDx =
this.dx * Math.cos(squareRotation) -
this.dy * Math.sin(squareRotation);
const rotatedDy =
this.dx * Math.sin(squareRotation) +
this.dy * Math.cos(squareRotation);
this.x += rotatedDx;
this.y += rotatedDy;
}
checkCollision(otherBall) {
const dx = otherBall.x - this.x;
const dy = otherBall.y - this.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < this.radius + otherBall.radius) {
// Collision detected
const angle = Math.atan2(dy, dx);
const targetX = this.x + Math.cos(angle) * (this.radius + otherBall.radius);
const targetY = this.y + Math.sin(angle) * (this.radius + otherBall.radius);
const ax = (targetX - otherBall.x) * 0.05;
const ay = (targetY - otherBall.y) * 0.05;
this.dx -= ax;
this.dy -= ay;
otherBall.dx += ax;
otherBall.dy += ay;
}
}
}
// Create balls
const balls = [];
const colors = ['red', 'blue', 'green', 'purple', 'orange',
'cyan', 'magenta', 'yellow', 'brown', 'pink'];
for (let i = 0; i < 10; i++) {
balls.push(new Ball(
Math.random() * canvas.width,
Math.random() * canvas.height,
20,
colors[i]
));
}
let squareRotation = 0;
const squareSize = 600;
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Rotate square
squareRotation += 0.01;
// Draw rotating square
ctx.save();
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.rotate(squareRotation);
ctx.strokeStyle = 'black';
ctx.strokeRect(-squareSize/2, -squareSize/2, squareSize, squareSize);
ctx.restore();
// Update and draw balls
for (let i = 0; i < balls.length; i++) {
balls[i].update(squareRotation);
// Check collisions with other balls
for (let j = i + 1; j < balls.length; j++) {
balls[i].checkCollision(balls[j]);
}
// Boundary collision
if (balls[i].x - balls[i].radius < 0 ||
balls[i].x + balls[i].radius > canvas.width) {
balls[i].dx *= -0.9;
}
if (balls[i].y - balls[i].radius < 0 ||
balls[i].y + balls[i].radius > canvas.height) {
balls[i].dy *= -0.9;
}
balls[i].draw();
}
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
This script includes several key features:
10 balls with different colors
Realistic physics with:
Friction
Velocity rotation based on square's rotation
Ball-to-ball collision detection
Energy loss during collisions
A spinning square container
Smooth animation using requestAnimationFrame()
The physics simulation includes:
Friction reduction of velocity
Collision detection between balls
Boundary collision with energy loss
Rotation of velocities based on the square's rotation
You can adjust parameters like friction, ball size, rotation speed, etc., to customize the simulation.