Skip to content
The Chat Archive
command-a-03-2025· Cohere LMArena Human Preference 140K

p5.js demo script showing a bouncing ball inside a spinning pyramid. It should have…

  1. User

    p5.js demo script showing a bouncing ball inside a spinning pyramid. It should have realistic physics. No HTML

  2. Assistant

    Below is a p5.js script that demonstrates a bouncing ball inside a spinning pyramid with realistic physics. The ball will bounce off the pyramid's walls, and both the pyramid and the ball will have realistic physical behaviors.

    let pyramid;
    let ball;
    let gravity = 0.3;
    let friction = 0.95;
    let spinSpeed = 0.02;
    
    function setup() {
      createCanvas(800, 600, WEBGL);
      pyramid = new Pyramid();
      ball = new Ball();
    }
    
    class Pyramid {
      constructor() {
        this.angle = 0;
        this.vertices = [
          [-100, -100, 100],
          [100, -100, 100],
          [0, 100, 100],
          [-100, -100, -100],
          [100, -100, -100],
          [0, 100, -100]
        ];
        this.faces = [
          [0, 1, 2],
          [3, 4, 5],
          [0, 1, 4, 3],
          [1, 2, 5, 4],
          [2, 0, 3, 5],
          [0, 2, 5, 3]
        ];
      }
    
      draw() {
        push();
        rotateX(this.angle);
        rotateY(this.angle * 1.5);
        rotateZ(this.angle * 0.7);
    
        stroke(255);
        strokeWeight(2);
        noFill();
    
        for (let face of this.faces) {
          beginShape();
          for (let v of face) {
            vertex(this.vertices[v][0], this.vertices[v][1], this.vertices[v][2]);
          }
          endShape(CLOSE);
        }
    
        this.angle += spinSpeed;
        pop();
      }
    
      checkCollision(ball) {
        for (let face of this.faces) {
          let [v1, v2, v3] = face.map(i => createVector(...this.vertices[i]));
          let normal = p5.Vector.cross(p5.Vector.sub(v2, v1), p5.Vector.sub(v3, v1)).normalize();
          let planeD = normal.dot(v1);
          let dist = normal.dot(ball.pos) - planeD;
    
          if (dist > -ball.radius && dist < ball.radius) {
            let toPlane = p5.Vector.mult(normal, -dist);
            ball.pos.add(toPlane);
            ball.vel.add(normal.mult(-1 * (1 + friction) * ball.vel.dot(normal)));
            return true;
          }
        }
        return false;
      }
    }
    
    class Ball {
      constructor() {
        this.pos = createVector(0, -50, 0);
        this.vel = createVector(random(-2, 2), random(2, 4), random(-2, 2));
        this.radius = 10;
        this.mass = 1;
      }
    
      update() {
        this.vel.y -= gravity;
        this.pos.add(this.vel);
    
        if (!pyramid.checkCollision(this)) {
          // Floor collision
          if (this.pos.y - this.radius < -100) {
            this.pos.y = -100 + this.radius;
            this.vel.y *= -friction;
          }
        }
      }
    
      draw() {
        push();
        translate(this.pos.x, this.pos.y, this.pos.z);
        noStroke();
        fill(255, 0, 0);
        sphere(this.radius * 2);
        pop();
      }
    }
    
    function draw() {
      background(30);
      ambientLight(50);
      pointLight(255, 255, 255, 0, 0, 200);
    
      pyramid.draw();
      ball.update();
      ball.draw();
    }
    

    Explanation:

    1. Pyramid Class:

      • The pyramid is defined by its vertices and faces.
      • It rotates smoothly in 3D space.
      • The checkCollision method detects if the ball collides with any of the pyramid's faces and handles the collision response.
    2. Ball Class:

      • The ball has a position, velocity, radius, and mass.
      • It is affected by gravity and bounces off the pyramid's walls and the floor.
      • The ball's movement is updated in the update method, and it is drawn as a red sphere.
    3. Physics:

      • Gravity is applied to the ball's vertical velocity.
      • Collisions with the pyramid's faces are detected using plane equations, and the ball's velocity is adjusted accordingly.
      • Friction is applied to reduce the ball's velocity after each bounce.

    This script creates a visually engaging scene where a ball bounces realistically inside a spinning pyramid, showcasing basic 3D physics in p5.js.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.