rotating planes in 3D

edited March 2016 in Questions about Code

Hi, trying some simple projects in 3D.

For my step 1 project I decided to rotate a plane. I created a plane, however, simple rotateY doesn't help me much, as I would like to rotate against some arbitrary point.

But then I run it this: - I first translate to the point against which I want to rotate - I rotate by some angle - I draw the plane (but as I have previously 'translated' my plane is no a bit off)

In the end, I want the plane to be rotated against any axis. For instance, against the middle of the plane.

This is my code:

int step  = 10;
int a = 1;
void setup() {
  size (800, 800, P3D);
}



void draw() {
  background (0, 0, 0);

  a = a + 1; 

  translate (width/2, 0);

  for (int y = 0; y < height; y += 10) {
    fill (255,0,0);
    ellipse (0, y, 5, 5);
  }
  rotateY (radians(a));




  for (int i = width/2 - 100; i < width/2 + 100; ) {
    i += step;
    for (int j = height/2 - 100; j < height/2 + 100; j += step) {
      pushMatrix();
      translate (i, j);
      fill (255, 255, 255);
      ellipse (0, 0, 5, 5);
      popMatrix();
    }
  }
}
Tagged:

Answers

  • edited March 2015 Answer ✓

    Hello !

    Your first "translate" should be inside an another pushMatrix/popMatrix. Then, in your loop, apply the rotation in the push/popMatrix corresponding to each element, but it's important to apply the rotation BEFORE the translation - the result will be very different if you put it after the translation -

    (if you define the rotation before the translation, it means that the origin of the mesh is at the center of the object ; when you apply a translation, you move the object from its center )

    Good luck !

  • edited March 2015

    Tried out, but missed something, I guess - so it didn't work in the end. And, if I enclose the translate operator in push/pop Matrix ... It won't play any role at all.

        int step  = 10;
        int a = 1;
        void setup() {
          size (800, 800, P3D);
        }
    
    
    
        void draw() {
          background (0, 0, 0);
    
          a = a + 1; 
    
    
    
    
          for (int y = 0; y < height; y += 10) {
            fill (255, 0, 0);
            ellipse (0, y, 5, 5);
          }
    
    
    
    
    
          for (int i = width/2 - 100; i < width/2 + 100; ) {
            i += step;
            for (int j = height/2 - 100; j < height/2 + 100; j += step) {
              pushMatrix();
    
    
              rotateY (radians(a));
    
    
              translate (i, j);
              fill (255, 255, 255);
              ellipse (0, 0, 5, 5);
              popMatrix();
            }
          }
        }
    
  • edited March 2015

    Decided there is no need to draw everything every frame - decided to make a shape... Unfortunately, doesn't work at the moment.

        int step  = 10;
        int a = 1;
    
        PShape plane;
        void setup() {
          size (800, 800, OPENGL);
          //  noLoop();
          plane = createShape();
          plane.beginShape(LINES);
          for (int i = 0; i < width; ) {
            i += step;
    
            for (int j = 0; j < height; j += step) {
              pushMatrix();
    
              translate (i, j, dist(i, j, width/2, height/2)*dist(i, j, width/2, height/2)/100);
              plane.fill (255, 255, 255);
    
              plane.vertex (0, 0, 10, 10);
              popMatrix();
            }
          }
          endShape(CLOSE);
        }
    
    
    
        void draw() {
          background (0, 0, 0);
    
          translate (0, 400);
          rotateX (radians(mouseX));
          translate (0, -400);
    
          shape(plane, 0, 0);
        }
    
  • you has made few mistakes

    int step  = 10;
    int a = 1;
    
    PShape plane;
    void setup() {
      size (800, 800, OPENGL);
      //  noLoop();
      plane = createShape();
      plane.beginShape(LINES);
      for (int i = 0; i < width; ) {
        i += step;
    
        for (int j = 0; j < height; j += step) {
          //  plane.pushMatrix();
    
          //plane.translate (i, j, dist(i, j, width/2, height/2)*dist(i, j, width/2, height/2)/100);
          //plane.translate ();
          plane.fill (255, 255, 255);
          plane.stroke (255, 255, 255);
          plane.vertex (i, j, -333);
          // plane.popMatrix();
        }
      }
      plane.endShape(CLOSE);
    }
    
    
    
    void draw() {
      background (0, 0, 0);
    
      translate (0, 400);
      rotateX (radians(map(mouseX, 0, width, 0, 360)));
      translate (0, -400);
    
      shape(plane, 0, 0);
    }
    
  • move mouse left and right please

  • here

    int step  = 30;
    int a = 1;
    
    // Make the parent shape
    PShape plane ; // see https: // www.processing.org/tutorials/pshape/
    
    void setup() {
      size (800, 800, OPENGL);
    
      plane = createShape(GROUP);
      PShape mySphere;
    
      for (int i = 0; i < width; ) {
        i += step;
    
        for (int j = 0; j < height; j += step) {
    
          // make sphere 
          mySphere = createShape(SPHERE, 15 );
          //  mySphere.translate(i, j, -.01*i*j);
          if (i>600&&j>600)
            mySphere.translate(i, j, -266);
          else 
            mySphere.translate(i, j, -222);
          mySphere.setStroke(false); // noStroke
          mySphere.setFill(color(255, 2, 2));
    
          // Add the "child" shape to the parent group
          plane.addChild(mySphere);
          mySphere=null;
        }
      }
    
      println ("here 2");
    }
    
    
    
    void draw() {
      background (0, 0, 0);
      lights();
    
      translate (0, 400, -400);
      rotateX (radians(map(mouseX, 0, width, 0, 360)));
      translate (0, -400);
    
      shape(plane, 0, 0);
    }
    
Sign In or Register to comment.