Here's a p5.js script demonstrating a bouncing ball inside a spinning tesseract with realistic physics:
let tesseract;
let ball;
let gravity = 0.2;
let friction = 0.99;
function setup() {
createCanvas(800, 600, WEBGL);
}
class Tesseract {
constructor() {
this.angle4D = 0;
this.rotationSpeed = 0.01;
}
project4Dto3D(point) {
let [x, y, z, w] = point;
let w1 = 2 / (2 + w);
return [
x * w1,
y * w1,
z * w1
];
}
draw() {
push();
rotateX(this.angle4D);
rotateY(this.angle4D * 1.3);
rotateZ(this.angle4D * 0.7);
stroke(200);
strokeWeight(2);
noFill();
// 4D vertices of a tesseract
let vertices = [
[-1, -1, -1, -1], [1, -1, -1, -1],
[1, 1, -1, -1], [-1, 1, -1, -1],
[-1, -1, 1, -1], [1, -1, 1, -1],
[1, 1, 1, -1], [-1, 1, 1, -1],
[-1, -1, -1, 1], [1, -1, -1, 1],
[1, 1, -1, 1], [-1, 1, -1, 1],
[-1, -1, 1, 1], [1, -1, 1, 1],
[1, 1, 1, 1], [-1, 1, 1, 1]
];
// Project 4D vertices to 3D
let projectedVertices = vertices.map(v => this.project4Dto3D(v));
// Draw edges
for (let i = 0; i < vertices.length; i++) {
for (let j = i + 1; j < vertices.length; j++) {
let diff = vertices[i].filter((val, idx) => val !== vertices[j][idx]).length;
if (diff <= 1) {
let p1 = projectedVertices[i];
let p2 = projectedVertices[j];
line(p1[0] * 200, p1[1] * 200, p1[2] * 200,
p2[0] * 200, p2[1] * 200, p2[2] * 200);
}
}
}
this.angle4D += this.rotationSpeed;
pop();
}
}
class Ball {
constructor() {
this.pos = createVector(0, 0, 0);
this.vel = createVector(random(-2, 2), random(-2, 2), random(-2, 2));
this.radius = 20;
this.boundingBox = 180; // Slightly smaller than tesseract projection
}
update() {
// Update position
this.pos.add(this.vel);
// Bounce off boundaries
if (Math.abs(this.pos.x) > this.boundingBox) {
this.pos.x = this.pos.x > 0 ? this.boundingBox : -this.boundingBox;
this.vel.x *= -1 * friction;
}
if (Math.abs(this.pos.y) > this.boundingBox) {
this.pos.y = this.pos.y > 0 ? this.boundingBox : -this.boundingBox;
this.vel.y *= -1 * friction;
}
if (Math.abs(this.pos.z) > this.boundingBox) {
this.pos.z = this.pos.z > 0 ? this.boundingBox : -this.boundingBox;
this.vel.z *= -1 * friction;
}
// Apply gravity
this.vel.y += gravity;
}
draw() {
push();
translate(this.pos.x, this.pos.y, this.pos.z);
noStroke();
fill(255, 0, 0, 200);
sphere(this.radius);
pop();
}
}
function setup() {
createCanvas(800, 600, WEBGL);
tesseract = new Tesseract();
ball = new Ball();
}
function draw() {
background(0);
// Lighting and perspective
ambientLight(50);
pointLight(255, 255, 255, 400, 400, 400);
// Draw tesseract and ball
tesseract.draw();
ball.update();
ball.draw();
}
This script creates a dynamic visualization with several interesting features:
- 4D Tesseract Projection: The tesseract rotates in 4D space, with its projection rendered in 3D.
- Bouncing Ball: A red sphere bounces realistically within the tesseract's boundaries.
- Physics Simulation:
- Gravity affects the ball's vertical movement
- Friction reduces bounce energy
- Boundary collisions with elastic response
The ball will bounce around inside the spinning tesseract, with its movement influenced by gravity and friction. The tesseract rotates smoothly, creating an mesmerizing 4D to 3D projection effect.
Note: This requires p5.js library to be included in your HTML file.