Here's a p5.js script demonstrating a bouncing ball inside a spinning pyramid with realistic physics:
let pyramid;
let ball;
let gravity = 0.2;
let friction = 0.98;
let restitution = 0.7; // Coefficient of restitution for energy loss on collision
function setup() {
createCanvas(800, 600, WEBGL);
pyramid = new Pyramid();
ball = new Ball();
}
class Pyramid {
constructor() {
this.angleX = 0;
this.angleY = 0;
this.rotationSpeedX = 0.005;
this.rotationSpeedY = 0.008;
this.size = 200;
}
draw() {
push();
rotateX(this.angleX);
rotateY(this.angleY);
// Base
noStroke();
fill(100, 100, 255, 100);
beginShape(QUADS);
vertex(-this.size, this.size, this.size);
vertex(this.size, this.size, this.size);
vertex(this.size, this.size, -this.size);
vertex(-this.size, this.size, -this.size);
endShape();
// Sides
fill(200, 100, 100, 150);
beginShape(TRIANGLES);
vertex(0, -this.size, 0);
vertex(-this.size, this.size, this.size);
vertex(this.size, this.size, this.size);
vertex(0, -this.size, 0);
vertex(this.size, this.size, this.size);
vertex(this.size, this.size, -this.size);
vertex(0, -this.size, 0);
vertex(this.size, this.size, -this.size);
vertex(-this.size, this.size, -this.size);
vertex(0, -this.size, 0);
vertex(-this.size, this.size, -this.size);
vertex(-this.size, this.size, this.size);
endShape();
stroke(255);
strokeWeight(2);
noFill();
beginShape(LINES);
vertex(-this.size, this.size, this.size);
vertex(this.size, this.size, this.size);
vertex(this.size, this.size, this.size);
vertex(this.size, this.size, -this.size);
vertex(this.size, this.size, -this.size);
vertex(-this.size, this.size, -this.size);
vertex(-this.size, this.size, -this.size);
vertex(-this.size, this.size, this.size);
endShape();
this.angleX += this.rotationSpeedX;
this.angleY += this.rotationSpeedY;
pop();
}
// Check collision with pyramid faces
checkCollision(pos, vel, radius) {
let halfSize = this.size - radius;
let apex = -this.size;
let base = this.size;
// Base collision (y = base plane)
if (pos.y > halfSize) {
let normal = createVector(0, -1, 0);
let dot = vel.dot(normal);
if (dot > 0) { // Only bounce if moving towards the plane
vel.sub(normal.mult(2 * dot * restitution));
pos.y = halfSize; // Prevent penetration
}
}
// Side collisions (planes defined by pyramid edges)
// Front face (normal = (1,1,0)/sqrt(2))
if (pos.x - pos.z > halfSize * 1.414) {
let normal = createVector(1, 0, -1).normalize();
let dot = vel.dot(normal);
if (dot > 0) {
vel.sub(normal.mult(2 * dot * restitution));
pos.x = halfSize + pos.z; // Correct position
}
}
// Right face (normal = (0,1,1)/sqrt(2))
if (pos.x + pos.z > halfSize * 1.414) {
let normal = createVector(1, 0, 1).normalize();
let dot = vel.dot(normal);
if (dot > 0) {
vel.sub(normal.mult(2 * dot * restitution));
pos.x = -pos.z + halfSize; // Correct position
}
}
// Back face (normal = (-1,1,0)/sqrt(2))
if (-pos.x - pos.z > halfSize * 1.414) {
let normal = createVector(-1, 0, -1).normalize();
let dot = vel.dot(normal);
if (dot > 0) {
vel.sub(normal.mult(2 * dot * restitution));
pos.x = -halfSize + pos.z; // Correct position
}
}
// Left face (normal = (0,1,-1)/sqrt(2))
if (-pos.x + pos.z > halfSize * 1.414) {
let normal = createVector(-1, 0, 1).normalize();
let dot = vel.dot(normal);
if (dot > 0) {
vel.sub(normal.mult(2 * dot * restitution));
pos.x = pos.z + halfSize; // Correct position
}
}
// Apex collision (cone approximation)
let distanceToApex = dist(pos.x, pos.y, pos.z, 0, apex, 0);
if (distanceToApex < radius) {
// Calculate reflection direction
let apexVec = createVector(0, apex, 0).sub(pos);
let normal = apexVec.normalize();
let dot = vel.dot(normal);
if (dot < 0) { // Only bounce if moving towards apex
vel.sub(normal.mult(2 * dot * restitution));
// Move ball out of apex
pos.add(apexVec.setMag(radius - distanceToApex));
}
}
}
}
class Ball {
constructor() {
this.pos = createVector(0, -50, 0);
this.vel = createVector(random(-2, 2), random(-4, 0), random(-2, 2));
this.radius = 15;
}
update() {
this.vel.y += gravity;
this.pos.add(this.vel);
pyramid.checkCollision(this.pos, this.vel, this.radius);
this.vel.mult(friction);
}
draw() {
push();
translate(this.pos.x, this.pos.y, this.pos.z);
noStroke();
fill(255, 0, 0, 200);
sphere(this.radius);
pop();
}
}
function draw() {
background(0);
// Basic lighting
ambientLight(30);
directionalLight(255, 255, 255, 0, 0, -1);
pyramid.draw();
ball.update();
ball.draw();
}
This script creates:
- Spinning Pyramid: Rotates around both X and Y axes
- Bouncing Ball:
- Affected by gravity
- Collides with all pyramid faces (base and sides)
- Energy loss on each bounce (realistic restitution)
- Constrained within pyramid boundaries
The physics implementation includes:
- Gravity: Constant downward acceleration
- Friction: Gradual energy loss over time
- Restitution: Energy loss on collisions (bounciness < 1)
- Complex Collision Detection:
- Plane collisions for base and sides
- Cone approximation for apex collision
- Proper reflection calculations using surface normals
The ball will bounce naturally within the rotating pyramid, demonstrating:
- Different bounce angles on various surfaces
- Gradual settling due to energy loss
- Realistic shadow effects from basic lighting
Run this code in the p5.js environment to see the interactive visualization. Adjust variables like gravity, friction, and restitution to experiment with different physical behaviors!