# canculate distance from camera to view plane ?

This topic is 4898 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

My intention is to map a 2D(screen)coordinate(x,y) to 3D cooridnate(x,y,z)to draw in 3D screen by using Perspective projection. my solution is to calculate these values base on the Distance from camera to view plane(D) as i read from a 3D book : x(3D) = (1 - z(3D)/D)*x(2D) y(3D) = (1 - z(3D)/D) *y(2D) my question is if i had to calcute D value or Direct3D have already support function to get this value? are there any better solution in this case?

##### Share on other sites
Well, that's the problem, isn't it? you know the X and Y coordinates, but you don't know the Z coordinates. You could be indicating any one of an infinite number of points, which radiate out in a straight line from the eyepoint.

To do picking, you need more information about the particular point you're interested in. Sometimes, everything you care about will be in the same plane. Sometimes, all you'll know is that the point you want is on a given mesh... or on one of a number of meshes. In this latter case, you can read back from the Z-buffer, but it's generally not recommended, due to low precison and the efficiency hit from doing a readback. More commonly, you'll do an intersection test to figure out whether that line of possible points intersects a given object, and if so, where on that object.

##### Share on other sites
no, my problem is not the z value, i can manual add and tweak this value.
what i mean is the D distance, how to calculate it or if Direct3D have already support function to get this value?

##### Share on other sites
What exactly do you mean by view plane? Your near or far plane? If that is what you mean, I don´t see a problem, because you´ve used those distance values to generate your projection matrix.

If you want the exact z-position of a given point S on any plane p (given by something like a*x + b*y + c*z + d = 0), you´ll need to create a ray representing the possible 3D coordinates behind S. When you have that ray you do a ray-plane-intersection to calculate the 3D coordinates of a point that lies behind your original point S on the plane p.
I guess informations on how to generate the ray would be found most easily in any tutorial / article about "picking", so try to google for that. Ray-plane-intersection should also be found with google.

If you want the distance of your camera C from the plane p (as above) you should simply insert the (x,y,z) coordinates of C into p´s equation, the result is the distance of C to p, like that: a* x(C) + b* y(C) + c* z(C) + d.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634135
• Total Posts
3015752
×