Log In  

Cart #ray-0 | 2024-05-02 | Embed ▽ | License: CC4-BY-NC-SA
9

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

P#147765 2024-05-02 12:28

Very nice! I will try to make one too, but in OG Pico

P#147773 2024-05-02 13:23

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.

P#147801 2024-05-03 01:36 ( Edited 2024-05-03 01:36)

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
P#147805 2024-05-03 02:30 ( Edited 2024-05-03 02:31)

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.

P#147877 2024-05-04 00:59

If you know the distance of the ray d and the relative angle of the cast a, then to get a distance D as though it were projected onto a 2D line instead of a sphere, you want D=d/abs(sin(a)).

P#147915 2024-05-04 17:05

Thanks alot everyone :)

P#147923 2024-05-04 19:44

Very cool!

P#147925 2024-05-04 20:16

[Please log in to post a comment]