freshfork / p5.EasyCam

A p5.js library for easy 3D camera control.
https://freshfork.github.io/p5.EasyCam
MIT License
136 stars 33 forks source link

world point to screen coordinates #15

Closed stephanschulz closed 2 years ago

stephanschulz commented 2 years ago

Thanks for making this library. It works very well.

Would anyone have advice on how to implement point picking with easycam?

In openframeworks i can use worldToScreen and then check the distance to the mouse to find the nearest 3d object. But i have no idea how to do this conversion from 3d to 2d here?

Thanks a bunch.

jwdunn1 commented 2 years ago

This sketch was implemented by easyCam's original author. I'm not familiar with the inner mechanics, but there is a worldToScreen defined on line 351. Maybe this will help.

stephanschulz commented 2 years ago

Thank you for taking the time to respond.

I solved it by using this screen position code: https://github.com/bohnacker/p5js-screenPosition

I collect the 3d points in function draw():

 screenPositions = [];
  for (let i = 0; i < layoutPoints.length; i++) {
    // get the screen positions of the ends of the line:
    screenPositions.push(
      screenPosition(layoutPoints[i].x, layoutPoints[i].y, layoutPoints[i].z)
    );
  }

Then draw within function displayHUD() :

push();
  translate(width / 2, height / 2);
  fill(255, 255, 255);
  stroke(255);
  for (let i = 0; i < screenPositions.length; i++) {
    circle(screenPositions[i].x, screenPositions[i].y, 10);
  }
  pop();

The circles on top of the 3D boxes are drawing in 2D.

Screen Shot 2022-02-26 at 10 21 57 AM
jwdunn1 commented 2 years ago

Excellent! Thanks for sharing the snippets. I'm going to do some experimenting with this [when I find some time].

stephanschulz commented 2 years ago

Just because, here an animation drawing a line between mouse and the closest 3D object. Feb-26-2022 10-52-08

Bobingstern commented 1 year ago

Can you share the code to this demo? For some reason i cant get my own to work

jwdunn1 commented 1 year ago

I did something similar here