0

I'm doing project using raycasting (just remaking old good wolf3d game). And I am led by one tutorial. I am stuck in understanding the math which he is using in it. I tried to get into it and lost a lot of hours googling, drawing and solving it, but I can't get that result. Asking here, because math and gamedev communities looks like much less populated. And also gamedev community has the same question as mine, and it is still unanswered. So, maybe someone with good math could explain it.

Here is my ray Here what a tutorial says:

When deriving deltaDistX geometrically you get, with Pythagoras, the formula "sqrt(1 + (rayDirY * rayDirY) / (rayDirX * rayDirX))", but this can be simplified to abs(1 / rayDirX). Similarly, deltaDistY is "sqrt(1 + (rayDirX * rayDirX) / (rayDirY * rayDirY))" which can be simplified to abs(1 / rayDirY).

rayDirX and rayDirY is our ray's X and Y coordinates. Each side of the grid is a 1. I guess it is connected with rescaling the Pythagorean theorem here it goes. But how is this simplified to this?

abs(1 / rayDirX) and abs(1 / rayDirY)

Any help and info is appreciated.

Fyodor Iz
  • 67
  • 6
  • see [Ray Casting with different height size](https://stackoverflow.com/a/47251071/2521214) ... IIRC the simplification is simple `tan` equation and as one axis is `1` the other one converts into `tan(a) = dy/1` or `tan(a) = 1/dx` ... where `a` is angle of casted ray ... so you should end up with something like `dy = tan(a)` and `dx = 1/tan(a)` ... and both `tan(a)` and `1/tan(a)` can be precomputed in some LUT for speed (usually in fixed point to avoid FPU and divisions)... The simplificated equations of yours could be scaled already hence the missing `tan` ... – Spektre Mar 12 '19 at 11:56
  • @Spektre Thank you for your feedback! It could be thing with tan, but anyway I can't mind it into right place to get that simplification working. There is nothing in code associated with tan. But I saw guides which was using tan in calculations to get this distance. Anyway, I am glad that you answered this :) – Fyodor Iz Mar 12 '19 at 19:52

1 Answers1

2

I had the same question when I was following lodev guide. Asked himself. This is his response:

Let's call rayDirX and rayDirY x and y for brevity. Then:

sqrt(1 + y * y / (x * x)) = sqrt((x * x + y * y) / (x * x))

But we know that the length of rayDir is always 1, so (x * x + y * y) is 1 so that gives sqrt(1 / (x * x)) which equals abs(1 / x).

aantropov
  • 88
  • 5