1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Raycasting engine - help needed

Discussion in 'Programming' started by ras maxim, Apr 16, 2017.

  1. ras maxim

    ras maxim Member

    Joined:
    Apr 14, 2017
    Posts:
    32
    Hello. I am trying to implement a simple raycasting algorythm in Game Maker Studio 1.4 following the tutorials by Permadi and Lode Vandevenne and I need some help.

    Project file: Download
    Compiled executable: Download

    Things I want to improve:

    1. Division by zero bug. When checking for intersections with horizontal walls, we use the following formula (taken from here):
      Code:
      first wall x coordinate = wall size / tangent of ray angle
      It works except the case when angle is 0 or 180 degrees. Because the tangent is zero, and division by zero returns infinity (-1.J, which is being rounded to zero by GMS, I guess) the x coordinate is always zero. It results in checking the wrong wall and displaying a pixel column that is totally out of place (see the screenshot, it is in the center).

      raycasting.png

      Is there another way to find the first coordinate? Please help me to fix this.

    2. Correct floor drawing. I can't seem to find the right way to get the exact floor intersection coordinates. Lode Vandevenne gives the following code depending what wall is being cheked, east, west, south or north:
      Code:
            //4 different wall directions possible
            if(side == 0 && rayDirX > 0)
           {
             floorXWall = mapX;
             floorYWall = mapY + wallX;
           }
           else if(side == 0 && rayDirX < 0)
           {
             floorXWall = mapX + 1.0;
             floorYWall = mapY + wallX;
           }
           else if(side == 1 && rayDirY > 0)
           {
             floorXWall = mapX + wallX;
             floorYWall = mapY;
           }
           else
           {
             floorXWall = mapX + wallX;
             floorYWall = mapY + 1.0;
           }
      
      Where wallX and wallY are the exact coordinates of wall and ray intersection, mapX and mapY are the coorinates of the square where the wall is (coordinates divided by wall size and rounded) and floorXWall and floorYWall are exact coordinates of the ray intersection with the floor (I guess so).

      It doesn't work for me, probably because I use pixel coordinates and lodev's tutorial uses grid coordinates (e.g. the first wall in the row is 0.0, 0.0 and the next one is 1.0, 0.0 and not 64.0, 0.0). For the current moment I just use wall coordinates for this, and it gives "scrolling floors" effect. This is not the way to go.

      I can't figure out how to adapt these formulas for my pixel coordinates. If you know how to do it, please tell me.

    3. Performance. It runs about 8 fps and 22 fps with YoYo Complier. I tried using surface buffers instead of draw_sprite, it frees another 3-4 frames. But the main time is taken by the floor drawing loop calculations. Even if I comment out the actual drawing part it is 14 fps and 36 fps with YoYo Complier.

      If you have suggestions how to optimize the code, please post them.
    Once again, you can find the link to the source code and the compiled executable at the top of the post. Thanks for reading.
     
    slojanko likes this.
  2. ras maxim

    ras maxim Member

    Joined:
    Apr 14, 2017
    Posts:
    32
    Haven't found a solution yet, so I think I can bump this thread.
     
  3. ras maxim

    ras maxim Member

    Joined:
    Apr 14, 2017
    Posts:
    32
    [​IMG]

    Image from http://permadi.com.

    Code:
    Xa = 64/Tan(alpha)
    
    It is easy to find the lenth of the Xa segment if the angle alpha is between 0 and 180 degrees. But when it is exactly 0 or 180, the tangent is 0 and the formula above is not applicable.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice