The hexagon should be rotating counter clockwise with the ball interacting with the the…
- User
The hexagon should be rotating counter clockwise with the ball interacting with the the borders of the hexagon.
Assistant
I'll modify the code to make the hexagon rotate counter-clockwise while maintaining realistic physics interactions with the ball. Here's the updated version:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Ball Bouncing Inside Rotating Hexagon</title> <style> body { margin: 0; background: #222; display: flex; justify-content: center; align-items: center; height: 100vh; } canvas { background: #111; border: 2px solid #555; } </style> </head> <body> <canvas id="canvas" width="600" height="600"></canvas> <script> (() => { const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); const W = canvas.width; const H = canvas.height; // Hexagon parameters const hexRadius = 250; const hexCenter = { x: W / 2, y: H / 2 }; let rotationAngle = 0; const rotationSpeed = 0.5; // radians per second // Ball parameters const ballRadius = 15; let ballPos = { x: hexCenter.x, y: hexCenter.y - 100 }; let ballVel = { x: 200, y: 150 }; // pixels per second // Time tracking let lastTime = null; // Calculate hexagon vertices with rotation function getHexVertices(center, radius, rotation) { const vertices = []; for (let i = 0; i < 6; i++) { const angle = Math.PI / 3 * i - Math.PI / 2 + rotation; // include rotation vertices.push({ x: center.x + radius * Math.cos(angle), y: center.y + radius * Math.sin(angle), }); } return vertices; } // Draw hexagon function drawHexagon(vertices) { ctx.beginPath(); ctx.moveTo(vertices[0].x, vertices[0].y); for (let i = 1; i < vertices.length; i++) { ctx.lineTo(vertices[i].x, vertices[i].y); } ctx.closePath(); ctx.strokeStyle = '#0af'; ctx.lineWidth = 3; ctx.stroke(); } // Draw ball function drawBall(pos) { ctx.beginPath(); ctx.arc(pos.x, pos.y, ballRadius, 0, Math.PI * 2); ctx.fillStyle = '#f80'; ctx.fill(); ctx.strokeStyle = '#fff'; ctx.lineWidth = 2; ctx.stroke(); } // Vector helpers function dot(v1, v2) { return v1.x * v2.x + v1.y * v2.y; } function subtract(v1, v2) { return { x: v1.x - v2.x, y: v1.y - v2.y }; } function add(v1, v2) { return { x: v1.x + v2.x, y: v1.y + v2.y }; } function multiply(v, scalar) { return { x: v.x * scalar, y: v.y * scalar }; } function length(v) { return Math.sqrt(v.x * v.x + v.y * v.y); } function normalize(v) { const len = length(v); return len === 0 ? { x: 0, y: 0 } : { x: v.x / len, y: v.y / len }; } // Check collision with hex edges and reflect velocity function handleCollision(pos, vel, radius, vertices) { // For each edge of the hexagon for (let i = 0; i < vertices.length; i++) { const a = vertices[i]; const b = vertices[(i + 1) % vertices.length]; // Edge vector const edge = subtract(b, a); // Normal vector to edge (pointing inward for a CCW hexagon) const normal = normalize({ x: -edge.y, y: edge.x }); // Vector from a vertex to ball center const toBall = subtract(pos, a); // Distance from ball center to edge line const dist = dot(toBall, normal); // Project ball center onto edge line to check if within segment const edgeLen = length(edge); const projScalar = dot(toBall, normalize(edge)); if (dist < radius && dist > -radius) { // Check if projection is within edge segment length if (projScalar >= 0 && projScalar <= edgeLen) { // Collision detected, reflect velocity // Move ball out of collision const penetration = radius - dist; if (penetration > 0) { pos.x += normal.x * penetration; pos.y += normal.y * penetration; } // Reflect velocity vector const vDotN = dot(vel, normal); vel.x = vel.x - 2 * vDotN * normal.x; vel.y = vel.y - 2 * vDotN * normal.y; // Add some of the hexagon's rotational velocity to the ball // Calculate tangential velocity at collision point const tangent = normalize(edge); const rotVelMagnitude = rotationSpeed * hexRadius; const tangentialVel = { x: tangent.x * rotVelMagnitude, y: tangent.y * rotVelMagnitude }; // Add a portion of this to the ball's velocity vel.x += tangentialVel.x * 0.3; vel.y += tangentialVel.y * 0.3; // Dampen velocity slightly to simulate energy loss vel.x *= 0.98; vel.y *= 0.98; } } } } // Animation loop function animate(time) { if (!lastTime) lastTime = time; const dt = (time - lastTime) / 1000; // seconds lastTime = time; // Update rotation angle rotationAngle += rotationSpeed * dt; // Get current hexagon vertices const hexVertices = getHexVertices(hexCenter, hexRadius, rotationAngle); // Update ball position ballPos.x += ballVel.x * dt; ballPos.y += ballVel.y * dt; // Add gravity ballVel.y += 200 * dt; // Handle collisions with the rotating hexagon handleCollision(ballPos, ballVel, ballRadius, hexVertices); // Clear canvas ctx.clearRect(0, 0, W, H); // Draw hexagon and ball drawHexagon(hexVertices); drawBall(ballPos); requestAnimationFrame(animate); } // Start animation requestAnimationFrame(animate); })(); </script> </body> </html>Would you like me to explain or break down the code?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.