DementedPurple wrote:
I don't know anything about Trigonometry
Then I simply can't explain how raycasting works in detail, since I'd have to teach you trigonometry as well. You need to at least understand what sines, cosines and tangents are, how they relate to the
unit circle and their
basic formulas.
I can still give you a basic overview of how things work, though. Like I said, raycasting is all about triangles:
Code:
+----------------+----------------+----------------+----------------+
| | | |WWWWWWWWWWWWWWWW|
| | | |WWWWWWWWWWWWWWWW|
| | | |WWWWWWWWWWWWWWWW|
| | | X WWWWWWWWWWWWWWW|
| | | / |WWWWWWWWWWWWWWWW|
| | | / |WWWWWWWWWWWWWWWW|
| | | / |WWWWWWWWWWWWWWWW|
| | | / |WWWWWWWWWWWWWWWW|
+----------------+----------------+------/---------+----------------+
| | | / |WWWWWWWWWWWWWWWW|
| | | / |WWWWWWWWWWWWWWWW|
| | |/ |WWWWWWWWWWWWWWWW|
| | HYP /| |WWWWWWWWWWWWWWWW|
| | / | |WWWWWWWWWWWWWWWW|
| | / | OPP |WWWWWWWWWWWWWWWW|
| | / | |WWWWWWWWWWWWWWWW|
| | / | |WWWWWWWWWWWWWWWW|
+----------------+-----/----------+----------------+----------------+
| | / | |WWWWWWWWWWWWWWWW|
| | / | |WWWWWWWWWWWWWWWW|
| / | |WWWWWWWWWWWWWWWW|
| +---+/ | A | |WWWWWWWWWWWWWWWW|
| | P |--+----------------+----------------+WWWWWWWWWWWWWWWW|
| +---+ | | ADJ |WWWWWWWWWWWWWWWW|
| | | |WWWWWWWWWWWWWWWW|
| | | |WWWWWWWWWWWWWWWW|
+----------------+----------------+----------------+----------------+
To draw a scene, you have to cast several rays away from the player at successive angles. When you cast a ray from player P at angle A, it hits a wall W at point X, forming a triangle. You need to apply trigonometric formulas to find the distance from the player to the wall (HYP), and use that information to calculate how tall that wall slice will appear on screen (greater distances result in smaller walls). Then you also need to find the opposite side (OPP), so you know the exact coordinate that was hit by the ray, in order to know what texture slice to use. Then you just need to do the actual scaling of the texture.
This is a very basic overview of the process, since each of these processes has to be broken down into smaller tasks. Casting the ray for example involves the use of smaller triangles, aligned to the grid:
Code:
+----------------+----------------+----------------+----------------+
| | | |WWWWWWWWWWWWWWWW|
| | | |WWWWWWWWWWWWWWWW|
| | | |WWWWWWWWWWWWWWWW|
| | | X WWWWWWWWWWWWWWW|
| | | / |WWWWWWWWWWWWWWWW|
| | | / |WWWWWWWWWWWWWWWW|
| | | / |WWWWWWWWWWWWWWWW|
| | | / |WWWWWWWWWWWWWWWW|
+----------------+----------------+------/---------+----------------+
| | | / |WWWWWWWWWWWWWWWW|
| | | / |WWWWWWWWWWWWWWWW|
| | |/---------------+WWWWWWWWWWWWWWWW|
| | /| |WWWWWWWWWWWWWWWW|
| | / | |WWWWWWWWWWWWWWWW|
| | / | |WWWWWWWWWWWWWWWW|
| | / | |WWWWWWWWWWWWWWWW|
| | / | |WWWWWWWWWWWWWWWW|
+----------------+-----/----------+----------------+----------------+
| | / | |WWWWWWWWWWWWWWWW|
| | / | |WWWWWWWWWWWWWWWW|
| /----------------+ |WWWWWWWWWWWWWWWW|
| +---+/ | | |WWWWWWWWWWWWWWWW|
| | P |--+ | |WWWWWWWWWWWWWWWW|
| +---+ | | |WWWWWWWWWWWWWWWW|
| | | |WWWWWWWWWWWWWWWW|
| | | |WWWWWWWWWWWWWWWW|
+----------------+----------------+----------------+----------------+
The formulas necessary to do this have a lot of divisions and multiplications, things the 6502 is just not good at, so you'll need to be clever and find ways to optimize those as much as possible. In my engine I was able to turn all divisions into multiplications or table look-ups, and I significantly reduced the number of multiplications by combining multiple operations into a single step.