Topic was automatically imported from the old Question2Answer platform.
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?
Godot doesn’t seem to have a built-in function, so I created a solution:
Get the global collision point from the laser’s raycast.
Convert to the mesh’s local coordinates.
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).
Convert the X,Y coordinate to a percentage (0 to 1) relative the the collision rectangle’s size.
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.
Check to see which 2D label falls within the calculated X,Y coordinates.