Distance to CameraView

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.

I think I have the solution.

		public static float DistanceFromPlayer(Vector3 objectCenter)
		{
			return Vector3.Transform(objectCenter, References._3Dcamera.View).Z;
		}

if i understand you correctly (I’m not sure and a bit confused) then you could essentially ignore x and y. Therefore, if the camera is a plane and you need the distance between to object and the camera plane the equation would be.

Math.Abs(objectCenter.Z - cameraPosition.Z)

Math.Abs can be removed if you need signed numbers.

That solution would only work in my example where Z is the distinguishing factor. But at other angles it wouldn’t be accurate.