How to display Microbit's orientation as a 3D model in Processing?

Are you sure you receive data as degrees?

Because you use radians()?

in my version you can rotate with the mouse



import processing.serial.*; // import the serial library

Serial myPort; // create a serial object

float xRot = 0.9; // variables to store the rotation angles
float yRot = 0.3;
float zRot = 0.2;
String occ;

void setup() {
  size(400, 400, P3D); // set the size of the window and enable 3D rendering

  String portName = Serial.list()[0]; // get the name of the first serial port
  myPort = new Serial(this, portName, 115200); // open a connection to the serial port
  println((Object[]) Serial.list());
}

void draw() {
  background(255); // clear the screen
  lights(); 

  translate(width/2, height/2, 0); // center the cube on the screen

  rotateX(xRot); // apply the rotations
  rotateZ(yRot);
  rotateY(zRot);

  fill(200); // set the fill color
  box(100); // draw the cube

  // red nose 
  translate( 60, 0, 0 );
  fill(255, 0, 0); 
  box(17); 

  yRot = map(mouseX, 0, width, 0, TWO_PI);
  xRot = map(mouseY, 0, height, 0, TWO_PI);

  //serialEventMy();
}

void serialEventMy() {
  // this function is called whenever new data is available
  // read the incoming data from the serial port
  String data = myPort.readStringUntil('\n'); // read the data as a string

  // print the incoming data to the console if it is not an empty string
  if (data != null) {
    println(data);
  }

  delay(100);
  if (data != null) {
    // split the string into separate values
    String[] values = split(data, ',');
    // convert the values to floats and store them in the rotation variables
    xRot = radians(float(values[0]));
    yRot = radians(float(values[1]));
    zRot = radians(float(values[2]));
  }
}
//

the rotation might not be realistic, because the way
rotation is done in processing.

see 3D Rotations: Rotating around the screen axes - #26 by behreajj

1 Like