How to get my 2D position on the screen from a 3D model

edited August 2017 in How To...

Hi,

Like what I said in the title. I create a 3D box in a 3D coordinate system, then I change the camera setting to see from different angels. On my screen (absolutely it's 2D), the box appears at different positions. I'd like to get it's center pointer position on the screen displayed by x and y just like a 2D drawing. Any ideas?

Thank you

Answers

  • you want to look ar screenX and screenY

    or modelX and modelY

  • Hi Chrisir,

    Thank you for your new instructions, I tried screenX and screenY. The issue here is it looks like the 3D pointer will always be map to a constant 2D plane. So no matter what I change the position of the camera, it always map to a fixed 2D position.

    When I disable the camera and use translate to make the box move, that is really what I want, the correct position of the box on my 2D window. So my question is do we have any method to map the 3D pointer on my camera view 2D plane. That should already be done somewhere. That's how the shape been drawn on my screen, right?

    Max

  • No really, screenX and screenY are what you want. Example:

    void setup(){
        size(600,400,P3D);
    }
    void draw(){
      background(0);
      pushMatrix();
      translate(width/2,height/2);
      scale(90);
      rotateY(map(mouseX,0,width,-PI,PI));
      rotateX(map(mouseY,0,height,-PI,PI));
      noFill();
      stroke(0,255,0);
      box(1);
      float px = screenX(.5,.5,.5);
      float py = screenY(.5,.5,.5);
      popMatrix();
      stroke(255);
      line(0,0,px,py);
    }
    
  • I checked your code and plus the camera, It looks good and I don't know why it doesn't work the last time. Maybe I messed up with the 3D position of the point I want to detect. Thank you so much for your help and example codes :)

Sign In or Register to comment.