Here's a simple physics simulation that demonstrates gravity and collision using HTML, CSS, and JavaScript:
<!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>
body {
margin: 0;
overflow: hidden;
background-color: #f0f0f0;
}
#canvas {
display: block;
background-color: white;
}
.controls {
position: absolute;
top: 10px;
left: 10px;
background: rgba(255,255,255,0.7);
padding: 10px;
border-radius: 5px;
}
button {
margin: 5px;
padding: 5px 10px;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div class="controls">
<button id="addBall">Add Ball</button>
<button id="reset">Reset</button>
<div>Gravity: <input type="range" id="gravity" min="0" max="2" step="0.1" value="0.5"></div>
<div>Bounce: <input type="range" id="bounce" min="0" max="1" step="0.1" value="0.7"></div>
</div>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const addBallBtn = document.getElementById('addBall');
const resetBtn = document.getElementById('reset');
const gravitySlider = document.getElementById('gravity');
const bounceSlider = document.getElementById('bounce');
// Set canvas size
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
// Physics parameters
let gravity = 0.5;
let bounce = 0.7;
let friction = 0.99;
// Array to store balls
let balls = [];
// Ball class
class Ball {
constructor(x, y, radius) {
this.x = x;
this.y = y;
this.radius = radius;
this.vx = Math.random() * 10 - 5; // Random initial velocity
this.vy = Math.random() * 5 - 10;
this.color = `hsl(${Math.random() * 360}, 70%, 50%)`;
this.mass = radius * radius * Math.PI;
}
update() {
// Apply gravity
this.vy += gravity;
// Apply friction
this.vx *= friction;
this.vy *= friction;
// Update position
this.x += this.vx;
this.y += this.vy;
// Handle wall collisions
if (this.x + this.radius > canvas.width) {
this.x = canvas.width - this.radius;
this.vx *= -bounce;
} else if (this.x - this.radius < 0) {
this.x = this.radius;
this.vx *= -bounce;
}
if (this.y + this.radius > canvas.height) {
this.y = canvas.height - this.radius;
this.vy *= -bounce;
} else if (this.y - this.radius < 0) {
this.y = this.radius;
this.vy *= -bounce;
}
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
ctx.closePath();
}
collideWith(other) {
const dx = other.x - this.x;
const dy = other.y - this.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < this.radius + other.radius) {
// Calculate collision normal
const nx = dx / distance;
const ny = dy / distance;
// Calculate relative velocity
const relVelX = other.vx - this.vx;
const relVelY = other.vy - this.vy;
// Calculate impulse
const impulse = 2 * (nx * relVelX + ny * relVelY) /
(1/this.mass + 1/other.mass);
// Apply impulse
this.vx += impulse * nx / this.mass * bounce;
this.vy += impulse * ny / this.mass * bounce;
other.vx -= impulse * nx / other.mass * bounce;
other.vy -= impulse * ny / other.mass * bounce;
// Prevent overlap
const overlap = (this.radius + other.radius - distance) / 2;
this.x -= overlap * nx;
this.y -= overlap * ny;
other.x += overlap * nx;
other.y += overlap * ny;
}
}
}
// Add a new ball
function addBall() {
const radius = 15 + Math.random() * 25;
const x = Math.random() * (canvas.width - 2 * radius) + radius;
const y = Math.random() * (canvas.height / 2) + radius;
balls.push(new Ball(x, y, radius));
}
// Reset simulation
function reset() {
balls = [];
}
// Update physics parameters
function updateParams() {
gravity = parseFloat(gravitySlider.value);
bounce = parseFloat(bounceSlider.value);
}
// Animation loop
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
updateParams();
// Update and draw balls
for (let i = 0; i < balls.length; i++) {
balls[i].update();
balls[i].draw();
// Check collisions with other balls
for (let j = i + 1; j < balls.length; j++) {
balls[i].collideWith(balls[j]);
}
}
requestAnimationFrame(animate);
}
// Event listeners
addBallBtn.addEventListener('click', addBall);
resetBtn.addEventListener('click', reset);
// Add some initial balls
for (let i = 0; i < 5; i++) {
addBall();
}
// Start animation
animate();
</script>
</body>
</html>
Would you like me to explain or break down this code?