# Calculation of perpendicular distance to camera plane explanation

I am following this tutorial for a Wolfenstein 3D engine. https://lodev.org/cgtutor/raycasting.html

Here is the relevant image from the tutorial:

I cannot figure out how dividing by rayDirX or rayDirY causes the distance calculation to work. The explanation given is about halfway down the page, right above this text if you search for it: "Calculate distance projected on camera direction".

• Assume this link will break some time in the future, but that a future reader would still like to understand your question and learn from your experience. So please include in the body of your question itself the relevant details needed to understand the calculation you're asking about. Including this content in your question also improves the search hits, bringing more folks with relevant experience to this page. – DMGregory Mar 22 '20 at 17:52
• I added an image, but I'll come back later and add more detail – Bluebomber357 Mar 22 '20 at 19:56
• @DMGregory I think I am in over my head with this question, I am not sure how to distill it down to a complete and concise question. Am I able to delete questions? – Bluebomber357 Mar 22 '20 at 21:41
• You can, but I don't recommend it. I'll take a read through the article a little later tonight and see if I can help you distill down a relevant excerpt. – DMGregory Mar 22 '20 at 21:42
• I read the section right underneath where I got stuck more carefully. Earlier I read the extended explanation wrong. I thought the tutorial digressed and derived in detail how to do the computationally more expensive but easier to understand method. The tutorial actually explains in the greater detail how it arrived at calculation that I am after. I fail, but I'll keep this question up. If you don't figure it out, I'll try to circle back later with an explanation. – Bluebomber357 Mar 22 '20 at 21:52

## 1 Answer

I believe I figured it out. This picture shows what I am after. How does perpWallDist = yDist/rayDirY? Using ratios you can deduce that yDist/rayDirY = euclidiean/rayDir. Then you can say that perpWallDist/dir = euclidean/rayDir. RayDir is equal to 1, that is just how the engine is setup, that vector is normalized. Therefore, perWallDist = yDist/rayDirY.

I would have never have figured this out on my own, but this makes sense and I can explain it. If there are other methods to solve this problem I would love to read them :)

• Nice work solving your problem, and thanks for posting your understanding. – DMGregory Mar 23 '20 at 2:36