Sure! Here's a basic particle collision algorithm implemented in JavaScript using the HTML Canvas:
<!DOCTYPE html>
<html>
<head>
<title>Particle Collision</title>
<style>
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="myCanvas" width="500" height="500"></canvas>
<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
const particles = [];
// define particle class
class Particle {
constructor(x, y, radius, color, dx, dy) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
this.dx = dx;
this.dy = dy;
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
ctx.closePath();
}
update() {
this.x += this.dx;
this.y += this.dy;
// handle collision with walls
if (this.x - this.radius < 0 || this.x + this.radius > canvas.width) {
this.dx = -this.dx;
}
if (this.y - this.radius < 0 || this.y + this.radius > canvas.height) {
this.dy = -this.dy;
}
// handle collision with other particles
for (let i = 0; i < particles.length; i++) {
const otherParticle = particles[i];
if (this === otherParticle) {
continue;
}
const distance = Math.sqrt(
Math.pow(this.x - otherParticle.x, 2) +
Math.pow(this.y - otherParticle.y, 2)
);
if (distance < this.radius + otherParticle.radius) {
const angle = Math.atan2(
otherParticle.y - this.y,
otherParticle.x - this.x
);
this.dx = Math.cos(angle) * Math.abs(this.dx);
this.dy = Math.sin(angle) * Math.abs(this.dy);
otherParticle.dx = -Math.cos(angle) * Math.abs(otherParticle.dx);
otherParticle.dy = -Math.sin(angle) * Math.abs(otherParticle.dy);
}
}
}
}
// create particles
for (let i = 0; i < 50; i++) {
const radius = Math.random() * 20 + 10;
const x = Math.random() * (canvas.width - radius * 2) + radius;
const y = Math.random() * (canvas.height - radius * 2) + radius;
const dx = Math.random() * 2 - 1;
const dy = Math.random() * 2 - 1;
const color = `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${Math.random() * 255})`;
particles.push(new Particle(x, y, radius, color, dx, dy));
}
// animate particles
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < particles.length; i++) {
const particle = particles[i];
particle.draw();
particle.update();
}
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
This code creates an HTML page with a canvas element that displays a set of particles that bounce off the walls and collide with each other. The Particle class defines the properties and behaviors of each particle, and the particles array contains all the particles that are currently on the screen.
The animate() function is called recursively using requestAnimationFrame() to update and redraw the particles on the canvas every frame. In the update() method of the Particle class, the particles' positions are updated based on their velocity, and collision detection logic is implemented to handle collisions between particles and the walls, as well as between particles themselves.
Overall, this code demonstrates a simple but effective algorithm for simulating particle collisions using JavaScript and the HTML Canvas.