Find centre of pyramid and rotate from there, not the apex

here is my version with mouse rotation


PGraphics canvas;
PVector[] basePts = new PVector[3];

float angleMouseX, angleMouseY; 

void setup() {
  size(768, 768, P3D);  
  canvas = createGraphics(width, height, P3D);

  for (int i = 0; i < 3; i++ ) {
    float ang = TWO_PI * i / 3;
    basePts[i] = new PVector(cos(ang) * 400/2, 
      sin(ang) * 400/2, 
      -100  );
    println(basePts[i]);
  }
}

void draw() {
  angleMouseX = map(mouseX, 0, width, -TWO_PI, TWO_PI);
  angleMouseY = map(mouseY, 0, height, height+200, -200);

  float rad = 810; 
  float posX = (0) + cos(angleMouseX) * rad ;
  float posZ = 0 + sin(angleMouseX) * rad ;
  canvas.camera(posX, angleMouseY, posZ, 
    width/2.0-0, height/2.0, 0, 
    0, 1, 0);

  //  canvas.camera(); 

  canvas.beginDraw();
  canvas.background(0);
  canvas.lights(); 
  canvas.noStroke();

  canvas.pushMatrix();
  canvas.translate(width/2, height/2, 0);
  //  canvas.rotateZ(PI/2);
  //  canvas.rotateY(radians(frameCount/2 % 360));

  canvas.fill(255);
  canvas.strokeWeight(3);
  canvas.stroke(123);

  //sides
  canvas.beginShape(TRIANGLES);
  for (int i = 0; i < 3; i++ ) {
    int i2 = (i+1) % 3;
    canvas.vertex(basePts[i].x, basePts[i].y, basePts[i].z);
    canvas.vertex(basePts[i2].x, basePts[i2].y, basePts[i2].z);
    canvas.vertex(0, 0, 100);
  }
  canvas.endShape();//sides

  //base
  canvas.fill(255, 0, 9); 
  canvas.beginShape(); //base
  for (int i = 0; i < 3; i++ ) {
    canvas.vertex(basePts[i].x, basePts[i].y, basePts[i].z);
  }
  canvas.endShape(CLOSE);

  canvas.popMatrix();
  canvas.endDraw();

  blendMode(BLEND);
  image(canvas, 0, 0);
}

2 Likes