p5.js demo script showing a bouncing ball inside a spinning tesseract. It should have…
- User
p5.js demo script showing a bouncing ball inside a spinning tesseract. It should have realistic physics. No HTML
Assistant
// Tesseract and ball simulation in p5.js (WEBGL) // Bouncing ball with gravity inside a spinning tesseract projection let tesseractVertices = []; let projectedVertices = []; let tesseractEdges = []; let rotationAngleYZ = 0; let rotationAngleXW = 0; // Add another rotation plane for complexity let projectionDistance = 2; // Distance for 4D to 3D projection let ball; let gravity; let restitution = 0.7; // Bounciness factor (0 = no bounce, 1 = perfect bounce) // Tesseract parameters let tesseractSize = 1; // Half-side length in 4D let projectedMinX, projectedMaxX; let projectedMinY, projectedMaxY; let projectedMinZ, projectedMaxZ; function setup() { createCanvas(600, 600, WEBGL); ortho(); // Use orthographic projection for a less distorted tesseract view // Initialize tesseract vertices (16 vertices, 4D coordinates) // Each coordinate is either -size or +size for (let i = 0; i < pow(2, 4); i++) { let vertex = []; for (let j = 0; j < 4; j++) { // Get the j-th bit of i (0 or 1) and map it to -size or +size vertex[j] = ((i >> j) & 1) ? tesseractSize : -tesseractSize; } tesseractVertices.push(vertex); } // Define tesseract edges (32 edges) // Edges connect vertices that differ in exactly one coordinate for (let i = 0; i < tesseractVertices.length; i++) { for (let j = i + 1; j < tesseractVertices.length; j++) { let diff = 0; for (let k = 0; k < 4; k++) { if (tesseractVertices[i][k] !== tesseractVertices[j][k]) { diff++; } } if (diff === 1) { tesseractEdges.push([i, j]); } } } // Initialize ball let ballRadius = 0.1; // Relative to tesseract size ball = { position: createVector(0, 0, 0), velocity: createVector(0.05, 0, 0.05), // Initial velocity radius: ballRadius }; // Initialize gravity gravity = createVector(0, 0.01, 0); // Downward gravity along Y axis } function draw() { background(50); // Dark background lights(); // Add some lighting // Rotation in YZ plane let rotationYZ = [ [1, 0, 0, 0], [0, cos(rotationAngleYZ), -sin(rotationAngleYZ), 0], [0, sin(rotationAngleYZ), cos(rotationAngleYZ), 0], [0, 0, 0, 1] ]; // Rotation in XW plane let rotationXW = [ [cos(rotationAngleXW), 0, 0, -sin(rotationAngleXW)], [0, 1, 0, 0], [0, 0, 1, 0], [sin(rotationAngleXW), 0, 0, cos(rotationAngleXW)] ]; // Project and rotate vertices projectedVertices = []; projectedMinX = Infinity; projectedMaxX = -Infinity; projectedMinY = Infinity; projectedMaxY = -Infinity; projectedMinZ = Infinity; projectedMaxZ = -Infinity; for (let i = 0; i < tesseractVertices.length; i++) { let v = tesseractVertices[i]; // Apply 4D rotations let rotated = multiplyMatrixVector(rotationYZ, v); rotated = multiplyMatrixVector(rotationXW, rotated); // Apply second rotation // 4D to 3D projection (perspective-like) // We project (x, y, z, w) onto a 3D space. // The projection point is on the W axis, at distance 'projectionDistance'. // Avoid division by zero or negative values by ensuring w < projectionDistance. // This is naturally handled by starting vertices at +/- size < projectionDistance // and small rotations. let w = rotated[3]; let perspectiveFactor = 1 / (projectionDistance - w); // Scale inversely with distance in W let projected = createVector( rotated[0] * perspectiveFactor, rotated[1] * perspectiveFactor, rotated[2] * perspectiveFactor ); projectedVertices.push(projected); // Update bounding box of the projected tesseract projectedMinX = min(projectedMinX, projected.x); projectedMaxX = max(projectedMaxX, projected.x); projectedMinY = min(projectedMinY, projected.y); projectedMaxY = max(projectedMaxY, projected.y); projectedMinZ = min(projectedMinZ, projected.z); projectedMaxZ = max(projectedMaxZ, projected.z); } // Draw the tesseract edges stroke(255); // White color for edges strokeWeight(1); noFill(); for (let edge of tesseractEdges) { let v1 = projectedVertices[edge[0]]; let v2 = projectedVertices[edge[1]]; line(v1.x * width / 4, v1.y * height / 4, v1.z * width / 4, v2.x * width / 4, v2.y * height / 4, v2.z * width / 4); } // --- Ball Physics --- // Apply gravity ball.velocity.add(gravity); // Update position ball.position.add(ball.velocity); // Collision detection and response with the projected tesseract bounding box // The bounding box is dynamic as the tesseract rotates let px = ball.position.x; let py = ball.position.y; let pz = ball.position.z; let r = ball.radius; // Scale the projected boundaries to screen coordinates for comparison let scaledMinX = projectedMinX * width / 4; let scaledMaxX = projectedMaxX * width / 4; let scaledMinY = projectedMinY * height / 4; let scaledMaxY = projectedMaxY * height / 4; let scaledMinZ = projectedMinZ * width / 4; let scaledMaxZ = projectedMaxZ * width / 4; // X-axis collision if (px - r < scaledMinX) { ball.position.x = scaledMinX + r; ball.velocity.x *= -restitution; // Add a small random perturbation to avoid getting stuck ball.velocity.x += random(-0.001, 0.001); } else if (px + r > scaledMaxX) { ball.position.x = scaledMaxX - r; ball.velocity.x *= -restitution; ball.velocity.x += random(-0.001, 0.001); } // Y-axis collision (Floor and Ceiling) if (py + r > scaledMaxY) { // Collision with bottom wall (max Y) ball.position.y = scaledMaxY - r; ball.velocity.y *= -restitution; ball.velocity.y += random(-0.001, 0.001); // Apply friction/dampening when bouncing off floor ball.velocity.x *= 0.99; ball.velocity.z *= 0.99; } else if (py - r < scaledMinY) { // Collision with top wall (min Y) ball.position.y = scaledMinY + r; ball.velocity.y *= -restitution; ball.velocity.y += random(-0.001, 0.001); } // Z-axis collision if (pz - r < scaledMinZ) { ball.position.z = scaledMinZ + r; ball.velocity.z *= -restitution; ball.velocity.z += random(-0.001, 0.001); } else if (pz + r > scaledMaxZ) { ball.position.z = scaledMaxZ - r; ball.velocity.z *= -restitution; ball.velocity.z += random(-0.001, 0.001); } // Draw the ball push(); translate(ball.position.x, ball.position.y, ball.position.z); noStroke(); fill(255, 0, 0); // Red color for the ball sphere(ball.radius * width / 4); // Scale ball radius to screen size pop(); // Increment rotation angles for animation rotationAngleYZ += 0.005; rotationAngleXW += 0.007; } // Helper function to multiply a 4x4 matrix by a 4D vector function multiplyMatrixVector(matrix, vector) { let result = [0, 0, 0, 0]; for (let i = 0; i < 4; i++) { for (let j = 0; j < 4; j++) { result[i] += matrix[i][j] * vector[j]; } } return result; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.