Jump to content
  • Advertisement
Sign in to follow this  
erickmeister

Raycasting math problem

This topic is 4956 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, I am having a problem getting my math right. I know this because my walls look like a funhouse :) If it's ok, I will post my raycasting function. My source basically contains 3 functions, one for calculating the ray which works great. 2 raycasting which is not great and drawing which works great. By the way I am programming in C++ using the Allegro game library. GRID_SIZE is a constant int equal to 64; angle[] is pre-calculated with all the angles to cast based on the FOV
void castRays(Player & ref)
{
  int ray_x;
  int ray_y;
  int map_x;
  int map_y;
  
  // end point variables will equal to ray_x/y when the rays end 
  int end_point_x;
  int end_point_y;

  // use these to calculate grid intersections before casting
  int opposite;
  int adjacent;

  // cast rays
  for(int i=0;i<SCREEN_WIDTH;i++)
  {
    ray_x = ref.posX;
    map_x = ref.posX / GRID_SIZE;
    map_y = ref.posY / GRID_SIZE;

    //try to find the V/H intersections
    //if ray is going right
    if((angle >= 0.0f) && (angle < 180.0f))
    {
      ray_x += (map_x + 1) * GRID_SIZE;
      adjacent = ref.posX - ray_x;
      opposite = int(tan((angle - 90.0f) * M_PI / 180.0f) * adjacent);
      ray_y = ref.posY + adjacent;
    }
    
    // if ray is going left
    else if((angle >= 180.0f) && (angle <= 360.0f))
    {
      ray_x += map_x * GRID_SIZE - 1;
      adjacent = ref.posX - ray_x;
      opposite = int(tan((angle - 270.0f) * M_PI / 180.0f) * adjacent);
      ray_y = ref.posY - adjacent;
    }

    while(map[map_x][map_y] != WALL)
    {
      ray_x += int(GRID_SIZE * cos(angle * M_PI / 180.0f));
      ray_y += int(GRID_SIZE * sin(angle * M_PI / 180.0f)); 

      map_x = ray_x / GRID_SIZE;
      map_y = ray_y / GRID_SIZE;
    }
    // end point variables equal to where ray_x, ray_y end
    end_point_x = ray_x;
    end_point_y = ray_y;

    // calculate the wall distance
    wall_distance = abs((end_point_y - ref.posY) / sin(ref.facing_direction));
  }
}
END_OF_FUNCTION(castRays(Player & ref));
Sorry for posting that ugly chunk of code and laughing is ok :) I will explain what I am trying to do, I'm searhing for walls by skipping 64 units at a time. But before I cast i'm trying to get my ray_x and ray_y variables to the grid intersections and I am sure this is incorrect which would explain my funhouse. Can someone correct my code or tell me what i'm doing wrong? I will very much appreciate it. Erick

Share this post


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

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!