<< back to Examples page

Example 5: Translating between coordinate systems; objectX()/ Y/Z


setAxis(100,100) - will move the (0,0) registration point of our BSpace to location (100,100) on the stage (WORLD) coordinates. This creates two distinct coordinate systems (c.s.): OBJECT c.s. and WORLD c.s.

In our example, the LOCAL point (0,0) is also a WORLD point (100,100). This is because we moved the BSpace Axis to (100,100) with the setAxis command.

Furthermore, any point in 3D is displayed on a 2D screen. Therefore, it is handy to have a tool that translates 3D coordinates to 2D coordinates. This is what ObjectX/Y/Z does.

This example demonstrate how to translate between a 3D LOCAL coordinate system, and a 2D WORLD coordinate system.

 

// Translating between coordinate systems; objectX()/ Y/Z

BSpace s1;
float angle; void setup(){ size(200,200); background(200); noFill(); smooth(); s1 = new BSpace(this); s1.setAxis(100,100); }
void loop(){


  angle += 0.04;
  s1.rotateX(angle); 
  s1.rotateY(angle); 
  s1.rotateZ(angle); 
  s1.box(40);
 // The box dimensions are 40*40*40, so one of its boxCorner is at 20,20,20.
 // Using the BSpace ObjectXYZ commands, we will translate between the s1 rotating coordinate system
 // and the stage (GLOBAL).
  float boxCornerX = s1.objectX(20,20,20);
  float boxCornerY = s1.objectY(20,20,20);
  float boxCornerZ = s1.objectZ(20,20,20);
           
           
  // Now that we have the translated boxCorner, we can use the mouseX/Y which also return values for the
  // 2D GLOBAL stage coordinates.
  stroke(255,0,0);
  strokeWidth(5); 
  point(boxCornerX, boxCornerY); // Draw a point on the stage, which show            
  strokeWidth(0); 
  line(boxCornerX,boxCornerY,mouseX,mouseY); // Draw a line from the boxCorner to the mouse cursor.
  stroke(0);
}