Multiple 3D Objects With Positions and Rotations

edited October 2017 in How To...

Hello all, I'm using P3D to try and draw a group of objects each with their own unique position in space and facing direction. For now, they're just boxes. I'm messing with transpose and rotateX/Y/Z, along with push and pullMatrix, but I can't wrap my head around it to place the boxes in the set positions. Does anyone have a good tutorial or example I can look at? Everything I can find just seems to use objects to show lighting examples. Thank you.

Answers

  • edited October 2017

    Can you say more about what you are trying to do, or share a short demo sketch?

    By transpose, do you mean translate()? By pullMatrix, do you mean popMatrix()?

    My guess without seeing any code is that you want to use this approach:

    1. origin -- translate / rotate the cursor to some shared starting reference point
    2. for each object:
      1. use pushMatrix()
      2. translate() that object to the right relative point
      3. then rotateX() / Y / Z it to the correct facing.
      4. use popMatrix() to return to the origin
        ...then repeat for the next object.

    Although, depending on your system of relative reference and how you imagine the space, you might want to do rotating before translating etc.

    Have you read the reference pages for the commands listed above, and have you looked at:

    ...these cover the basics of using the relative positioning commands and the matrix stack in 2D, then 3D contexts.

  • edited October 2017 Answer ✓

    I agree: Do the tutorials.

    Here is a simple example.

    Basically, normally you draw on a canvas (x,y). In 3D you draw in a room with a depth (x,y,z). Now imagine a table. You place something (an apple) on the surface. Move it right, you increase x. Move it towards you you increase z. Move it up above the table you decrease y. You use translate (x,y,z);.

    Remember:

    • 0,0 is upper left corner of the screen
    • x is towards right
    • y is towards down
    • z positive is towards you, z negative is away from you into the screen

    Code with functions and rotation.

    To have different rotations, make float a1,a2,a3; etc. and pass each of them as a parameter to the box function.

    Chrisir ;-)

    float a1; 
    
    void setup() {
      size(1200, 600, P3D);
      background(0);
    }
    
    void draw() {
    
      background(0);
    
      // everything looks better with lights
      lights(); 
    
      fill(255, 0, 0); // red 
      displaySphere(width/2, height/2, 0, 19);
    
      fill(0, 255, 0); // green 
      displaySphere(44, 44, 0, 19);
    
      fill(0, 255, 0); // green 
      displayBox(width/2, 99, 0, 39);
    
      fill(0, 0, 255); // blue 
      displayBox(width/2, 99, -439, 39); // blue box is same x, same, y, same size 
      // but farer away than the green box (-439 instead of 0)
    
      fill(0, 255, 0); // green 
      displayText(30, height-30, 0);
    
      fill(255, 235, 10); // yellow  
      displayText(width-118, height-30, 0);
    
      fill(255, 0, 255); // blue  
      displayText(width-118, height-30, -60); // same x and y, but more far away
    }
    
    //---------------------------------------------------------- 
    
    void displaySphere(float x, float y, float z, 
      float r) {
    
      // sphere
    
      noStroke();
      pushMatrix();
      translate(x, y, z);
      sphere(r);
      popMatrix();
    }
    
    void displayBox(float x, float y, float z, 
      float r) {
    
      // box 
    
      noStroke();
      pushMatrix();
      translate(x, y, z); // translate and rotate 
      rotateY(a1); 
      box(r);
      popMatrix();
    
      // rotation 
      a1+=.012;
    }
    
    void displayText(float x, float y, float z) {
    
      // Text of the coordinates at coordinates:
    
      // This function shows the coordinates x,y,z as text at the screen position 
      // of the coordinates.  
    
      pushMatrix();
      translate(x, y, z); // translate and rotate 
      text(x+","+y+","+z, 
        0, 0);
      popMatrix();
    }
    
Sign In or Register to comment.