Jump to content
  • Advertisement
Sign in to follow this  

Advanced Raycaster Mathematics?

This topic is 4257 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello GameDev, great to be here. I read the tutorial Advanced Raycasting Tutorial and found some curiosities with the math. Now, I'm no math major, but I'm pretty confident in my math abilities. I went over the calulations on graph paper and could not for the life of me get them to work out. Now, this could have been something with me but just to be sure I tried to hack together a little raycasting loop. It was something to the effect of:
for(int xScanline = 0, float ray = heading - 45; xScanline < screen.width, ray < heading + 45; xScanline++, ray+=Increase90Fov)
   float raySlope = tan(ray * (PI / 180));
   float wallSlope = (wallY2 - wallY1) / (wallX2 - wallX1);
   float b1 = (PlayerX - wallX1) * wallSlope + (PlayerY - wallY1);
   float wallHitX = b1 / (raySlope - wallSlope);
   float wallHitY = wallY1 + (wallHitX - wallX1) * wallSlope;

   int xDiff = (playerX - wallHitX);
   int yDiff = (playerY - wallHitY);

   if(abs(xDiff) > abs(yDiff)) //Find the Closer of the Wall hits
     dist = x_diff / cos(ray); 
     dist = y_diff / sin(ray);

   if(dist == 0)
      dist = 1;
   float scaledDist = eyeDist / dist;

   correctIntersection(wallHitX, wallHitY, ray); //Did we hit in the correct quadrant?

   int colTop; //Where to start drawing the column slice
   int colBottom //Where to stop drawing the column slice

   if(colTop < 0)
      colTop = 0;

   if(colBottom > screenHeight)
      colBottom = screenHeight - 1;

   drawLine(screen, xScanline, cotTop, xScanline, cotBottom, colour, colour, colour);

Assuming we're simplifying things by just having one completely enclosed sector so we don't have to get all complex. I think that's a good general overview of the code. Any input would be appreciated, it is quite an interesting topic. ;) Thanks.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!