Getting Plane Coordinate from 3D Collision Coordinates

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By stormreaver

Within a 3D environment, I have a 3D mesh with a flat, square texture applied that is constructed of Labels on a Node2D, and a CollisionShape around only the part of the mesh that contains the flat texture.

I also have an in-game 3D laser pointer that uses a RayCast to determine the global collision point between the laser pointer and the CollisionShape.

When the laser points at the texture, I want to determine which of the Labels is under the point of collision. Is there a way to do this within Godot?

:bust_in_silhouette: Reply From: stormreaver

Godot doesn’t seem to have a built-in function, so I created a solution:

  1. Get the global collision point from the laser’s raycast.
  2. Convert to the mesh’s local coordinates.
  3. Get the 2D X,Y offset within the collision rectangle (separately since the coordinate system will have to change the origin to the upper left corner).
  4. Convert the X,Y coordinate to a percentage (0 to 1) relative the the collision rectangle’s size.
  5. Iterate through the viewport container’s list of Node2D’s, converting the percentage to X,Y coordinates based on the viewport’s size and relative to the start of the viewport.
  6. Check to see which 2D label falls within the calculated X,Y coordinates.