I’m working on a rudimentary 3D, point-and-click game.
The cursor (hand) needs to know which object you are pointing at, and since it’s in 3D sometimes one object might be in front of another object, so we need to look at the distance from the camera to know which object we are pointing at.
public static float DistanceFromPlayer(Vector3 objectCenter)
{
return Vector3.DistanceSquared(objectCenter, References._3Dcamera.Position);
}
But this is not an adequate solution. In the image below you’ll see a screwdriver and another tilted screwdriver. The cursor hand might be the center where the camera position is. Now assume that the screwdrivers both have the same Z value, they just have different X and Y values - which is closest to the camera?
Well, the non-tilted scrwedriver that is closest to the center of the view would be closer to the camera position of course.
The problem is that this is not what the solution ought to be. I need an equation that where the result would find that in this example both screwdrivers are exactly the same distance from the camera.
Why? Well in this view, a third, hypothetical screwdriver that has just a slightly greater Z distance from the camera should be found to be “farther” away than either of these other screwdrivers. (If this is confusing maybe I’ll make another example image)
So instead of finding the distance from a Vector3 camera position in 3D space, I need to find the distance from the camera view as if the view were like a flat 2D plane. And the distance is found with a straight line ray from the object to the plane.
I’m awful with 3D and don’t understand how my own camera works very well.