Raycasting?
Controls:
- arrow keys to move
- mouse to look around it will eventually reach the border, you have to center the mouse again
- x for viewing map
- d for more detailed stats
about
After trying to make raycasting the wrong way, I came up with this method.
I really don't know if thats the correct way of doing it. My idea was to have rays being shot from the player, when they hit a wall the stop and draw a line in the center at the screen + index of the ray, also the further away the ray went, the smaller the line will be. So that make a pretty good illusion of 3d in a 2d space. You can poke around with the values like the field of vision, amount of samples taken (rays shot), the stretch of the screen and many other.
But I am having this fisheye issue. I guess you noticed that the prespectiv isn't always correct. sometimes it gets this weird round edges. I dont really know how to fix that. Also I dont know how to have walls with diffrent colors, or have a wall which is red on the left side a dark red on the right.
thanks for your interest
The fisheye issue is caused by the camera lens (the screen) being a flat surface. Rays further to the edges of the screen are a longer distance from the eye. To compensate for this, you will have to apply cos()
to each ray using the ray's angle to be able to correct this.
I also noticed when playing this test that you are calculating the distance of each ray incorrectly. This is a common problem when it comes to rendering things with perspective. The formula should be something like:
-- fov: a constant like the width of the screen -- z: the distance from the SCREEN "LENS" -- (i.e. the screen itself) to the collided object. fov / (z + fov) -- the object is at the camera "eye" when it is -- "behind" the lens at the distance "fov", meaning -- it is at the center when it tries to divide by 0. -- We handle this case by not rendering the ray at all. |
Instead I think maybe you have it flipped like:
(z + fov) / fov |
To fix the fish-eye effect, calculate rays in a way that ends of all rays form a straight line instead of an arc. I did this by adding camera_direction_vector and a fraction of a camera_perpendicular_vector. I know it's hard to describe, so I put a few illustrations and description here.
[Please log in to post a comment]