How to keep the camera stable while rotating a 3D model?

I have a 3D model (of a skateboard) in processing which is rotating based on the movement of an external device. I have also placed 3 arrows in my Processing scene as reference axes in the X,Y,Z directions. My problem is that when the device rotates, my entire Processing scene (axes and model) all rotates.

I’m trying to keep the ‘camera’ stable, as well as the axes, so that they don’t move, and instead the skateboard moves within the axes. So instead of the whole view rotating with the skateboard, it stays in one position, facing the X axis. But the skateboard model is still free to rotate as it needs.

Can anyone help me create this effect?

please show your code

in general use pushMatrix(); and popMatrix(); to isolate the skateboard rotation from the rest (axes in the X,Y,Z directions):

pseudo-code:

pushMatrix(); 
translate(x,y,z); 
rotate(a); 
shape(skate, 0,0); 
popMatrix(); 
a+=0.0412; 

// draw axes in the X,Y,Z directions

Example with full mouse rotation


void setup() {
  size(1640, 860, P3D);
}

void draw() {
  avoidClipping(); 
  background(152);
  lights(); 

  pushMatrix(); 
  translate (width/2, height/2, 400);

  rotateX(map(mouseY, 0, height, -TWO_PI, TWO_PI));
  rotateY(map(mouseX, 0, width, -TWO_PI, TWO_PI));

  textMode(SHAPE);
  textAlign(CENTER, CENTER);
  fill(255); 
  text("Hello", 0, 0);

  translate(60, 0);
  box(18);

  translate(-120, 0);
  box(18);
  popMatrix();

  drawFloor();
}

// ---------------------------------------------------------------------------
// Tools

void avoidClipping() {
  // avoid clipping (at camera): 
  // https : // 
  // forum.processing.org/two/discussion/4128/quick-q-how-close-is-too-close-why-when-do-3d-objects-disappear
  perspective(PI/3.0, (float) width/height, 1, 1000000);
}//func 

void drawFloor() {

  pushMatrix();

  //stroke(111);  // gray 
  noStroke(); 
  fill(0, 2, 255); // blue
  float factor1=80.0; // dist between boxes

  translate(width/2, height/2, 0); 

  for (int x=-55; x<55; x++) {
    for (int z=-55; z<55; z++) {

      pushMatrix(); 
      translate(x*factor1 - (4*factor1/2), 
        height-111, 
        z*factor1 - (4*factor1/2) );
      fill(map(z, -55, 55, 100, 255), 0, 0);
      box(40);  // size of 40 (width, height and depth) 
      popMatrix();
    }
  }
  popMatrix();
}
//
1 Like

Thanks that worked great! Apologies for not showing my code in the original post, I just wasn’t sure about sharing it since its for my dissertation

2 Likes