Line between a 2D and 3D point

edited June 2017 in Library Questions

Dear all,

I as a part of a 3D interface that I'm working on, I would like to be able to create a line between a 2D point and 3D point. In order to understand my question better, here is a simplified code:

import peasy.*;
PeasyCam cam;

void setup() {
  size(1600, 900, P3D);
  smooth();
  frameRate(60);
  lights();
  cam = new PeasyCam(this, 100);
}

void draw() {
  background(255);
  cam.beginHUD();
  rect(0, 0, 50, 50);
  cam.endHUD();

  translate(mouseX, mouseY);  
  box(10,10,10);
}

I would like that a line would be connecting a 2D point (for example the lower right corner of the 2D square) with one of the corners (3D point) of the cube. So even if the cube would be rotating, the line would be connecting both points by constantly updating itself.

Has anyone an idea how this could be done? Maybe by defining 2 PVectors, one as a 2D and the other as 3D?

Any help is highly appreciated. Regards,

L

Tagged:

Answers

  • Answer ✓

    Have a look at https://processing.org/reference/screenX_.html

    First attempt, not using the mouse but controlling the x and y position with the arrow keys.

    Kf

    import peasy.*;
    
    final int len=5;
    int px, py;
    
    PeasyCam cam;
    
    void setup() {
      size(1600, 900, P3D);
      smooth();
      frameRate(60);
      lights();
      //cam = new PeasyCam(this, width/2, height/2, 0, 100);
      cam = new PeasyCam(this, 100);
      px=0;//width/2;
      py=0;//height/2;
    }
    
    void draw() {
      background(255);
    
    
      pushMatrix();
      translate(-width/2,0,-height/2); 
      drawGrid(width/50, 50, 50, 1);
      popMatrix();
    
      pushMatrix();
      translate(px, py);  
      box(len);
      popMatrix();
      float posx=screenX(px-len/2, py-len/2, +len/2);
      float posy=screenY(px-len/2, py-len/2, +len/2);
    
    
      cam.beginHUD();
      rect(0, 0, 50, 50);
      line(50, 50, posx, posy);
      cam.endHUD();
    }
    
    void keyPressed() {
    
      if (key==CODED) {
        if (keyCode==UP)
          py++;
        else if (keyCode==DOWN)
          py--;
        else if (keyCode==LEFT)
          px--;
        else if (keyCode==RIGHT)
          px++;
      }
    }
    
    
    // -------------------------------------------------------
    void drawAxis(float len1, float len2, float len3) {
    
      pushStyle();
      strokeWeight(3);
    
      stroke(255, 0, 0);  //RED
      line(0, 0, 0, len1, 0, 0);
    
      stroke(0, 255, 0);  //GREEN
      line(0, 0, 0, 0, len2, 0);
    
      stroke(0, 0, 255);  //BLUE
      line(0, 0, 0, 0, 0, len3);
    
      popStyle();
    }
    
    // -------------------------------------------------------
    // @Args:  Plane: 0=YZ  1=XZ  2=XY 
    void drawGrid(int size, int w, int h, int plane) {
      pushStyle();
      noFill();
      if (plane == 0) stroke(255, 0, 0);
      if (plane == 1) stroke(0, 255, 0);
      if (plane == 2) stroke(0, 0, 255);
      int total = w * h;
      int tw = w * size;
      int th = h * size;
      beginShape(LINES);
      for (int i = 0; i < total; i++) {
        int x = (i % w) * size;
        int y = (i / w) * size;
        if (plane == 0) {
          vertex(0, x, 0);
          vertex(0, x, th);
          vertex(0, 0, y);
          vertex(0, tw, y);
        }
        if (plane == 1) {
          vertex(x, 0, 0);
          vertex(x, 0, th);
          vertex(0, 0, y);
          vertex(tw, 0, y);
        }
        if (plane == 2) {
          vertex(x, 0, 0);
          vertex(x, th, 0);
          vertex(0, y, 0);
          vertex(tw, y, 0);
        }
      }
      endShape();
      popStyle();
    }
    
  • Thank you kfrajer, thank you a lot really!

Sign In or Register to comment.