Here's a basic implementation of your request using HTML and JavaScript with the <canvas> element to create a visual simulation where:
- A black triangle rotates.
- A pink ball moves around and bounces off the triangle's rotating sides.
- The background is white.
This example uses 2D vector math to handle ball movement and basic collision detection with triangle sides.
✅ Demo Code (HTML + JS)
You can copy and paste this into an .html file and open it in a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Rotating Triangle and Bouncing Ball</title>
<style>
body {
margin: 0;
background: white;
overflow: hidden;
}
canvas {
display: block;
background: white;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
// Ball properties
let ball = {
x: centerX,
y: centerY - 100,
radius: 10,
color: 'pink',
vx: 2,
vy: 2
};
// Triangle properties
let triangle = {
radius: 150,
rotation: 0,
speed: 0.01 // rotation speed
};
// Vector utility
function getTriangleVertices(cx, cy, radius, angle) {
const vertices = [];
for (let i = 0; i < 3; i++) {
let theta = angle + i * (2 * Math.PI / 3);
let x = cx + radius * Math.cos(theta);
let y = cy + radius * Math.sin(theta);
vertices.push({ x, y });
}
return vertices;
}
function reflect(ball, normal) {
// Normalize the normal
const mag = Math.sqrt(normal.x * normal.x + normal.y * normal.y);
let nx = normal.x / mag;
let ny = normal.y / mag;
// Dot product
const dot = ball.vx * nx + ball.vy * ny;
// Reflect
ball.vx = ball.vx - 2 * dot * nx;
ball.vy = ball.vy - 2 * dot * ny;
}
// Line/ball collision detection
function ballLineCollision(ball, p1, p2) {
// Get vector from p1 to ball
const dx = ball.x - p1.x;
const dy = ball.y - p1.y;
// Get edge vector
const ex = p2.x - p1.x;
const ey = p2.y - p1.y;
// Get edge normal
const edgeLength = Math.hypot(ex, ey);
const nx = -ey / edgeLength;
const ny = ex / edgeLength;
const dist = dx * nx + dy * ny; // signed distance to edge plane
if (Math.abs(dist) <= ball.radius) {
// Check if projection falls within the line segment
const proj = (dx * ex + dy * ey) / (edgeLength * edgeLength);
if (proj >= 0 && proj <= 1) {
// Ball is intersecting edge
return { hit: true, normal: { x: nx, y: ny } };
}
}
return { hit: false };
}
function drawTriangle(vertices) {
ctx.beginPath();
ctx.moveTo(vertices[0].x, vertices[0].y);
ctx.lineTo(vertices[1].x, vertices[1].y);
ctx.lineTo(vertices[2].x, vertices[2].y);
ctx.closePath();
ctx.strokeStyle = "black";
ctx.lineWidth = 3;
ctx.stroke();
}
function drawBall(ball) {
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2);
ctx.fillStyle = ball.color;
ctx.fill();
}
function update() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
triangle.rotation += triangle.speed;
const vertices = getTriangleVertices(centerX, centerY, triangle.radius, triangle.rotation);
drawTriangle(vertices);
// Move the ball
ball.x += ball.vx;
ball.y += ball.vy;
// Bounce off triangle edges
for (let i = 0; i < 3; i++) {
const p1 = vertices[i];
const p2 = vertices[(i + 1) % 3];
const collision = ballLineCollision(ball, p1, p2);
if (collision.hit) {
reflect(ball, collision.normal);
break;
}
}
// Bounce off canvas edges (optional)
if (ball.x <= ball.radius || ball.x >= canvas.width - ball.radius) {
ball.vx *= -1;
}
if (ball.y <= ball.radius || ball.y >= canvas.height - ball.radius) {
ball.vy *= -1;
}
drawBall(ball);
requestAnimationFrame(update);
}
update();
</script>
</body>
</html>
🔧 Features:
- Rotating Equilateral Triangle centered in canvas.
- A ball bounces perfectly off the edges as they rotate.
- All math is done in 2D space using vectors and simple physics.
If you want enhancements like gravity, sound, advanced edge detection, or non-perfect bouncing (with friction or energy loss), let me know.