I'll create a physics simulation that demonstrates multiple real-world physics concepts including gravity, collision detection, friction, and bouncing. This will be a bouncing balls simulation with realistic physics.
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>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>Real World Physics Simulation</h1>
<div class="controls">
<button id="addBall">Add Ball</button>
<button id="clearBalls">Clear All</button>
<label>
Gravity: <input type="range" id="gravitySlider" min="0" max="2" step="0.1" value="0.5">
<span id="gravityValue">0.5</span>
</label>
<label>
Bounce: <input type="range" id="bounceSlider" min="0" max="1" step="0.1" value="0.8">
<span id="bounceValue">0.8</span>
</label>
</div>
<canvas id="physicsCanvas"></canvas>
<div class="info">
<p>Click anywhere on the canvas to add balls!</p>
<p>Physics concepts: Gravity, Collision Detection, Energy Conservation, Friction</p>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS (style.css):
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.container {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-radius: 20px;
padding: 20px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
border: 1px solid rgba(255, 255, 255, 0.2);
}
h1 {
text-align: center;
color: white;
margin-bottom: 20px;
font-size: 2em;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}
.controls {
display: flex;
gap: 15px;
margin-bottom: 20px;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
button {
background: linear-gradient(45deg, #ff6b6b, #ee5a24);
color: white;
border: none;
padding: 10px 20px;
border-radius: 25px;
cursor: pointer;
font-weight: bold;
transition: transform 0.2s, box-shadow 0.2s;
}
button:hover {
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}
button:active {
transform: translateY(0);
}
label {
color: white;
display: flex;
align-items: center;
gap: 10px;
font-weight: bold;
}
input[type="range"] {
width: 100px;
}
#physicsCanvas {
border: 3px solid rgba(255, 255, 255, 0.3);
border-radius: 15px;
background: rgba(0, 0, 0, 0.1);
display: block;
margin: 0 auto;
cursor: crosshair;
}
.info {
margin-top: 15px;
text-align: center;
color: rgba(255, 255, 255, 0.8);
}
.info p {
margin: 5px 0;
font-size: 0.9em;
}
JavaScript (script.js):
class Ball {
constructor(x, y, radius, color) {
this.x = x;
this.y = y;
this.vx = (Math.random() - 0.5) * 10; // Random horizontal velocity
this.vy = (Math.random() - 0.5) * 10; // Random vertical velocity
this.radius = radius;
this.color = color;
this.mass = radius * 0.1; // Mass proportional to radius
this.friction = 0.99; // Air resistance
}
update(gravity, bounce) {
// Apply gravity
this.vy += gravity;
// Apply friction (air resistance)
this.vx *= this.friction;
this.vy *= this.friction;
// Update position
this.x += this.vx;
this.y += this.vy;
// Boundary collision detection
if (this.x + this.radius > canvas.width) {
this.x = canvas.width - this.radius;
this.vx *= -bounce;
}
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;
}
if (this.y - this.radius < 0) {
this.y = this.radius;
this.vy *= -bounce;
}
}
draw(ctx) {
// Create gradient for 3D effect
const gradient = ctx.createRadialGradient(
this.x - this.radius/3, this.y - this.radius/3, 0,
this.x, this.y, this.radius
);
gradient.addColorStop(0, this.color);
gradient.addColorStop(1, this.darkenColor(this.color, 0.3));
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = gradient;
ctx.fill();
// Add highlight
ctx.beginPath();
ctx.arc(this.x - this.radius/3, this.y - this.radius/3, this.radius/4, 0, Math.PI * 2);
ctx.fillStyle = 'rgba(255, 255, 255, 0.3)';
ctx.fill();
}
darkenColor(color, factor) {
// Simple color darkening function
const hex = color.replace('#', '');
const r = Math.max(0, parseInt(hex.substr(0, 2), 16) * (1 - factor));
const g = Math.max(0, parseInt(hex.substr(2, 2), 16) * (1 - factor));
const b = Math.max(0, parseInt(hex.substr(4, 2), 16) * (1 - factor));
return `rgb(${Math.floor(r)}, ${Math.floor(g)}, ${Math.floor(b)})`;
}
// Ball-to-ball collision detection and response
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) {
// Collision detected - implement elastic collision physics
const angle = Math.atan2(dy, dx);
const sin = Math.sin(angle);
const cos = Math.cos(angle);
// Rotate velocities
const vx1 = this.vx * cos + this.vy * sin;
const vy1 = this.vy * cos - this.vx * sin;
const vx2 = other.vx * cos + other.vy * sin;
const vy2 = other.vy * cos - other.vx * sin;
// Apply conservation of momentum
const finalVx1 = ((this.mass - other.mass) * vx1 + 2 * other.mass * vx2) / (this.mass + other.mass);
const finalVx2 = ((other.mass - this.mass) * vx2 + 2 * this.mass * vx1) / (this.mass + other.mass);
// Rotate back
this.vx = finalVx1 * cos - vy1 * sin;
this.vy = vy1 * cos + finalVx1 * sin;
other.vx = finalVx2 * cos - vy2 * sin;
other.vy = vy2 * cos + finalVx2 * sin;
// Separate balls to prevent overlap
const overlap = this.radius + other.radius - distance;
const separationX = (dx / distance) * overlap * 0.5;
const separationY = (dy / distance) * overlap * 0.5;
this.x -= separationX;
this.y -= separationY;
other.x += separationX;
other.y += separationY;
}
}
}
// Initialize canvas and context
const canvas = document.getElementById('physicsCanvas');
const ctx = canvas.getContext('2d');
canvas.width = 800;
canvas.height = 600;
// Physics simulation variables
let balls = [];
let gravity = 0.5;
let bounce = 0.8;
let animationId;
// Color palette for balls
const colors = ['#ff6b6b', '#4ecdc4', '#45b7d1', '#96ceb4', '#feca57', '#ff9ff3', '#54a0ff'];
// Control elements
const gravitySlider = document.getElementById('gravitySlider');
const bounceSlider = document.getElementById('bounceSlider');
const gravityValue = document.getElementById('gravityValue');
const bounceValue = document.getElementById('bounceValue');
const addBallBtn = document.getElementById('addBall');
const clearBallsBtn = document.getElementById('clearBalls');
// Event listeners
gravitySlider.addEventListener('input', (e) => {
gravity = parseFloat(e.target.value);
gravityValue.textContent = gravity;
});
bounceSlider.addEventListener('input', (e) => {
bounce = parseFloat(e.target.value);
bounceValue.textContent = bounce;
});
addBallBtn.addEventListener('click', () => {
addRandomBall();
});
clearBallsBtn.addEventListener('click', () => {
balls = [];
});
canvas.addEventListener('click', (e) => {
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
addBall(x, y);
});
function addBall(x, y) {
const radius = Math.random() * 20 + 10;
const color = colors[Math.floor(Math.random() * colors.length)];
balls.push(new Ball(x, y, radius, color));
}
function addRandomBall() {
const x = Math.random() * (canvas.width - 60) + 30;
const y = Math.random() * (canvas.height - 60) + 30;
addBall(x, y);
}
function animate() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update and draw all balls
for (let i = 0; i < balls.length; i++) {
balls[i].update(gravity, bounce);
// Check collisions with other balls
for (let j = i + 1; j < balls.length; j++) {
balls[i].collideWith(balls[j]);
}
balls[i].draw(ctx);
}
// Draw physics info
ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
ctx.font = '14px Arial';
ctx.fillText(`Balls: ${balls.length}`, 10, 25);
ctx.fillText(`Gravity: ${gravity}`, 10, 45);
ctx.fillText(`Bounce: ${bounce}`, 10, 65);
animationId = requestAnimationFrame(animate);
}
// Start with a few balls
for (let i = 0; i < 3; i++) {
addRandomBall();
}
// Start animation
animate();
This physics simulation demonstrates several real-world physics concepts:
Physics Concepts Implemented:
- Gravity: Constant downward acceleration affecting all objects
- Collision Detection: Both boundary and ball-to-ball collision detection
- Conservation of Momentum: Realistic elastic collisions between balls
- Energy Loss: Bounce coefficient simulates energy loss during collisions
- Air Resistance: Friction gradually slows down moving objects
- Mass Effects: Larger balls have more mass and affect collisions differently
Interactive Features:
- Click anywhere on the canvas to add new balls
- Adjust gravity and bounce coefficients with sliders
- Add random balls or clear all balls with buttons
- Real-time physics parameter display
The simulation creates a realistic environment where balls bounce around, collide with each other and the walls, and gradually lose energy due to friction and imperfect collisions, just like in the real world.