Jump to content

Nearest point on polygon to another point?


Recommended Posts

I'm trying to build a sort of "tunnel explorer" to show points of interest within a tunnel modeled after something in the real world. Within the tunnel are points of interest.

Here's a mockup with a tunnel and some points of interest- http://www.babylonjs-playground.com/#1VFLZF#4

By default I expect users to free-roam with a FreeCamera, but I'd also like to be able to "jump to" any given defect by selecting it via UI not shown in the playground. I'm not sure how to do that though... my best theory is this:

  1. Add an invisible object in the approximate center-line of the tunnel (CL)
  2. Upon selecting a point of interest to jump to, locate the point on CL closest to the centeroid (mesh.getAbsolutePosition()) of the point of interest and set the camera's position to that point
  3. Rotate the camera to look at the point of interest

Can anyone help me implement step 2 (jumpNearestOnCenterline in playground)? Note that my actual tunnel not straight and constructed via a point cloud, so there is no good/simple way to determine the center of the tunnel that I'm aware of

I've never worked with cameras or automatic camera positioning systems before so my solution is inspired by GIS. If you have a more straight-forward idea I'm all ears.

Thank you for your time,


Link to comment
Share on other sites

Hello and welcome!

you can use camera.setTarget(box.position) to rotate your camera towards a POI

In you case, you can align the tunnel on z axis. So then clicking on a red POI will give you the POI.getAbsolutePosition(). Using this position you can keep the z and move your camera.position to (0, 0, z) and then use camera.setTarget

Link to comment
Share on other sites

Just realized I had a typo - PG updated at http://www.babylonjs-playground.com/#1VFLZF#5

@Deltakosh I'm confused by both your suggestions

  1. How can I align on the Z axis? As stated above, my tunnel is not straight. I'm somewhat familiar with rotations but unclear as to how I would determine the orientation of the localized part of the tunnel a point of interest appears in. Without that, I couldn't rotate correctly
  2. camera.setTarget doesn't alter the X axis rotation correctly. See this PG update - http://www.babylonjs-playground.com/#1VFLZF#7 (method is updated to use setTarget; runs automatically after 2 seconds. Drag/pan down to see the box). Am I using the method incorrectly?

Thank you again,


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...