How to Rotate a 3D "Line" like a 2D "Line"

Hello I am trying to get a 3D "Line" (the "line" is really a box() but you get the idea) I am drawing to rotate around one end the same way a 2D line does. In the example below you can see that the red line which is your standard line() object rotates around one of its points. I would like to get my box() object which is in green to rotate the same way as the red line. Any advice would be greatly appreciated.

void setup()
{
  size(640,360,P3D);

}
float n=0;
void draw(){
background(255);
//GREEN LINE
pushMatrix();
stroke(0,255,0);
translate(width/2, height/2);
rotate(n);
box(90,1,1);
popMatrix();

//RED LINE
pushMatrix();
stroke(255,0,0);
translate(width/2, height/2);
rotate(n);
line(0,0,-95,100);
popMatrix();
n=n+0.01;
}

Answers

  • Answer ✓
    float n=0;
    
    void setup() {
      size(640, 360, P3D);
    }
    
    void draw() {
      background(255);
    
      //GREEN BOX
      pushMatrix();
      stroke(0, 255, 0);
      translate(width/2, height/2);
      rotate(n);
      translate(45, 0, 0);
      box(90, 1, 1);
      popMatrix();
    
      //RED LINE
      pushMatrix();
      stroke(255, 0, 0);
      translate(width/2, height/2);
      rotate(n);
      line(0, 0, -95, 100);
      popMatrix();
    
      n=n+0.01;
    }
    
  • Thanks!! TfGuy44 that really helped. So if I understand this correctly the second call of translate moves the center of rotation of the line 45 units in the x direction? so I must first translate the line to the center of the screen and then translate it again in order to rotate it?

  • The first translate moves the (0,0,0) point so that it is in the middle of the screen. When you call rotate(), you're actually calling rotateZ(), which rotates space around the Z-axis. The second call to translate() then moves the (0,0,0) point along the (now) ROTATED X-axis. It moves the origin 45 units away from where we rotated, which makes it look like a 90-units long box is now spinning about the center of the screen.

    The important thing to realize is that your call to rotate() is changing the direction that the X-axis points!

  • edited March 2017

    There is also a version of line with 6 parameters

    It's a 3d line

    void setup() {
      size(640, 360, P3D);
    }
    
    void draw() {
      background(0);
      lights();
      depth(260);
    }
    
    void depth(float z1) {
    
      pushMatrix();
    
      // A vector that points to the mouse location
      PVector mouse = new PVector(mouseX, mouseY, 0);
      stroke(255);
      line(220, 220, -220, 
        mouse.x, mouse.y, mouse.z );
    
      popMatrix();
    }
    // 
    

    Also look at my turtle code, there is a function drawLine iirc

    Which gives you a thin rect as a line

    Use it with lights();

    void setup() {
      size(640, 360, P3D);
    }
    
    void draw() {
      background(0);
      lights();
      depth(260);
    }
    
    void depth(float z1) {
    
      pushMatrix();
    
      // A vector that points to the mouse location
      PVector mouse = new PVector(mouseX, mouseY, 0);
      stroke(255);
      drawLine(220, 220, -220, 
        mouse.x, mouse.y, mouse.z, 
        22, 
        color(255, 0, 0));
    
      popMatrix();
    }
    // 
    
    // -----------------------------------------------
    
    void drawLine(float x1, float y1, float z1, 
      float x2, float y2, float z2, 
      float weight, 
      color strokeColour)
      // was called drawLine; programmed by James Carruthers
      // see http : // 
      // processing.org/discourse/yabb2/YaBB.pl?num=1262458611/0#9
    {
      PVector p1 = new PVector(x1, y1, z1);
      PVector p2 = new PVector(x2, y2, z2);
      PVector v1 = new PVector(x2-x1, y2-y1, z2-z1);
      float rho = sqrt(pow(v1.x, 2)+pow(v1.y, 2)+pow(v1.z, 2));
      float phi = acos(v1.z/rho);
      float the = atan2(v1.y, v1.x);
      v1.mult(0.5);
    
      pushMatrix();
      translate(x1, y1, z1);
      translate(v1.x, v1.y, v1.z);
      rotateZ(the);
      rotateY(phi);
      noStroke();
      fill(strokeColour);
      // box(weight,weight,p1.dist(p2)*1.2);
      box(weight, weight, p1.dist(p2)*1.0);
      popMatrix();
    }
    
    //
    
  • I posted some additional code above

Sign In or Register to comment.