When I turn the 3D space, I want an object remains in place: how to do ?

allall
edited March 2016 in Library Questions

Hello,

I wrote a small P5 code to explain my problem. In P3D, there are 2 cubes : white cube and red cube. I want to rotate the white cube in the 3 dimensions without rotate the red cube. I use a very simple Max standalone control (rotateX, rotateY, rotateZ) to rotate the white cube (OSC). You can download this standalone here : https://dl.dropboxusercontent.com/u/83095197/Rotation.zip

This work fine if I rotate only one variable (rotateX with rotateY = 0 et rotateZ = 0; rotateY with rotateX = 0 et rotateZ = 0; rotateZ with rotateX = 0 et rotateY = 0). On the other side, it doesn't work if I rotate 2, 3 variables in the same time : the red cube move. How to maintain the red cube in place by modifying several parameters at once ?

Here, my P5 code :

                        // OSC
                        import oscP5.*;
                        import netP5.*;
                        OscP5 oscP5;
                        NetAddress myRemoteLocation;

                        // VARIABLES
                        int DIMX, DIMY, DIMZ;
                        float translateX, translateY, translateZ;
                        float rotateX, rotateY, rotateZ;


                        void setup() {
                          size(600, 600, P3D); 
                          DIMX = width/2; 
                          DIMY = height/2;
                          DIMZ = 300;

                          // OSC
                          oscP5 = new OscP5(this, 12000);
                          myRemoteLocation = new NetAddress("127.0.0.1", 12000);

                          // VARIABLES
                          translateX = width/2;
                          translateY = height/2;
                          translateZ = -height*2;
                          rotateX = 0;
                          rotateY = 0;
                          rotateZ = 0;
                        }


                        void draw() {
                          background(0);

                          // WHITE CUBE
                          translate(translateX, translateY, translateZ);
                          rotateX(radians(rotateX));
                          rotateY(radians(rotateY));
                          rotateZ(radians(rotateZ));
                          noFill();
                          stroke(255);
                          box(DIMX*2, DIMY*2, DIMZ*2);

                          // RED CUBE
                          pushMatrix();
                          rotateX(radians(rotateX * -1));
                          rotateY(radians(rotateY * -1));
                          rotateZ(radians(rotateZ * -1));
                          noFill();
                          stroke(255, 0, 0);
                          box(DIMX, DIMY, DIMZ);
                          popMatrix();
                        }


                        void oscEvent(OscMessage theOscMessage) {

                          // ROTATE X
                          if (theOscMessage.checkAddrPattern("/max2P5/rotateX")==true) {
                            rotateX=theOscMessage.get(0).floatValue();
                          }

                          // ROTATE Y
                          if (theOscMessage.checkAddrPattern("/max2P5/rotateY")==true) {
                            rotateY=theOscMessage.get(0).floatValue();
                          }

                          // ROTATE Z
                          if (theOscMessage.checkAddrPattern("/max2P5/rotateZ")==true) {
                            rotateZ=theOscMessage.get(0).floatValue();
                          }
                        }

Thank you :) Best, Alex

Answers

  • Look at pushMatrix and popMatrix

  • Of course !!! I feel stupid :) Thank you...

  • I'm still in difficulty...

    I'm going to be more precise with this new small P5 code. I use P3D. I draw a white cube to see the space and its transformation (translation and rotation in the 3 dimensions). You can transform the space with the new Max standalone controls available here : https://dl.dropboxusercontent.com/u/83095197/Rotation.zip

    Then, I use a picture file (.png) placed inside this space. When I transform the space in the 3 dimensions, I would like that the picture file move with the white cube to keep its place in the cube but in the same time, I would like that the file doesn't rotate with the cube. I need that the file be still in front of us.

    This work fine if I rotate only one variable (rotateX with rotateY = 0 et rotateZ = 0; rotateY with rotateX = 0 et rotateZ = 0; rotateZ with rotateX = 0 et rotateY = 0), but it doesn't work if I rotate 2, 3 variables in the same time : the picture file move. How to maintain the picture file in front of us by modifying several parameters at once ?

                    // OSC
                    import oscP5.*;
                    import netP5.*;
                    OscP5 oscP5;
                    NetAddress myRemoteLocation;
    
                    // VARIABLES
                    int DIMX, DIMY, DIMZ;
                    float translateX, translateY, translateZ;
                    float rotateX, rotateY, rotateZ;
    
                    // IMAGE
                    PImage ellipse;
    
    
                    void setup() {
                      size(600, 600, P3D); 
                      DIMX = width/2; 
                      DIMY = height/2;
                      DIMZ = 300;
    
                      // OSC
                      oscP5 = new OscP5(this, 12000);
                      myRemoteLocation = new NetAddress("127.0.0.1", 12000);
    
                      // VARIABLES
                      translateX = width/2;
                      translateY = height/2;
                      translateZ = -height*2;
                      rotateX = 0;
                      rotateY = 0;
                      rotateZ = 0;
    
                      // IMAGES
                      ellipse = loadImage("Ellipse_512_blurG_step5_0.png");
                    }
    
    
                    void draw() {
                      background(0);
    
                      // GLOBAL TRANSFORMATION OF THE SAPCE
                      translate(translateX, translateY, translateZ);
                      rotateX(radians(rotateX));
                      rotateY(radians(rotateY));
                      rotateZ(radians(rotateZ));
    
                      // WHITE CUBE (REPRESENT THE SPACE)
                      noFill();
                      stroke(255);
                      box(DIMX*2, DIMY*2, DIMZ*2);
    
                      // ELLIPSE
                      translate(DIMX/4, DIMY/4, DIMZ/4);
                      rotateX(radians(rotateX * -1));
                      rotateY(radians(rotateY * -1));
                      rotateZ(radians(rotateZ * -1));
    
                      imageMode(CENTER);
                      tint(255, 0, 0);
                      image(ellipse, 0, 0, 200, 200);
                    }
    
    
                    void oscEvent(OscMessage theOscMessage) {
    
                      // TRANSLATE X
                      if (theOscMessage.checkAddrPattern("/max2P5/translateX")==true) {
                        translateX=theOscMessage.get(0).floatValue();
                      }
    
                      // TRANSLATE Y
                      if (theOscMessage.checkAddrPattern("/max2P5/translateY")==true) {
                        translateY=theOscMessage.get(0).floatValue();
                      }
    
                      // TRANSLATE Z
                      if (theOscMessage.checkAddrPattern("/max2P5/translateZ")==true) {
                        translateZ=theOscMessage.get(0).floatValue();
                      }
    
                      // ROTATE X
                      if (theOscMessage.checkAddrPattern("/max2P5/rotateX")==true) {
                        rotateX=theOscMessage.get(0).floatValue();
                      }
    
                      // ROTATE Y
                      if (theOscMessage.checkAddrPattern("/max2P5/rotateY")==true) {
                        rotateY=theOscMessage.get(0).floatValue();
                      }
    
                      // ROTATE Z
                      if (theOscMessage.checkAddrPattern("/max2P5/rotateZ")==true) {
                        rotateZ=theOscMessage.get(0).floatValue();
                      }
                    }
    

    Thank you !!! Best, Alex

  • ??

    where did you use pushMatrix & popMatrix?

    Use them around 41 and 52 as a pair and likewise around

    52 & 61

    I am not sure about that osc event though

  • I didn't add pushMatrix() & popMatrix() in the previous code because I wanted to explain my problem with a different configuration : a picture file (red ellipse) not placed in the center of the white cube. I checked many configuration of pushMatrix() & popMatrix() but I don't know how to resolve my problem = I want that the red ellipse keep its position inside the cube (when I translate, rotate the cube in the 3 dimensions) but I don't want that this one rotate : I want to keep the picture always in front of us. I tried your proposition of course but it doesn't work... OSC work fine. Best,

  • You need a hud

    A head up display?

    Osc has this?

Sign In or Register to comment.