Advertisement
Guest User

Untitled

a guest
May 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.11 KB | None | 0 0
  1. // here is how i call it
  2. if(c.isKeyJustPressed(KEY_RBUTTON))
  3. {
  4.     int state = 0;
  5.     vec3d hit_pos = vec3d();
  6.     vec3d pos_before_hit = vec3d();
  7.     uint block_offset;
  8.    
  9.     vec3d ray_pos = pos;
  10.     ray_pos.y += cam_height;
  11.     vec3d look_dir( Maths::Sin((dir_x)*Maths::Pi/180)*Maths::Cos(dir_y*Maths::Pi/180),
  12.                     Maths::Sin(dir_y*Maths::Pi/180),
  13.                     Maths::Cos((dir_x)*Maths::Pi/180)*Maths::Cos(dir_y*Maths::Pi/180));
  14.     Raycast_precise(@SM, ray_pos, look_dir, 16, state, hit_pos, pos_before_hit);
  15.    
  16.     if(state == 1)
  17.     {
  18.         SM.set_block(pos_before_hit.x, pos_before_hit.y, pos_before_hit.z, block_stone);
  19.        
  20.         pos_before_hit = pos_before_hit/16;
  21.         int offset = Maths::Min(int(pos_before_hit.z)+int(pos_before_hit.x)*z_size+x_size*z_size*int(pos_before_hit.y), SM.chunks.size()-1);
  22.         SM.chunks[offset].rebuildMesh();
  23.     }
  24. }
  25.  
  26.  
  27. // function itself
  28. void Raycast_precise(SuperMap@ SM, vec3d ray_pos, vec3d ray_dir, int max_dist, int &out state, vec3d &out hit_pos, vec3d &out pos_before_hit)
  29. {
  30.     vec3d ray_world_pos = vec3d(int(ray_pos.x), int(ray_pos.y), int(ray_pos.z));
  31.     vec3d delta_dist = vec3d(Maths::Abs(1/ray_dir.x), Maths::Abs(1/ray_dir.y), Maths::Abs(1/ray_dir.z));
  32.     vec3d side_dist;
  33.     vec3d step;
  34.     if(ray_dir.x < 0)
  35.     {
  36.         step.x = -1;
  37.         side_dist.x = (ray_pos.x - ray_world_pos.x) * delta_dist.x;
  38.     }
  39.     else
  40.     {
  41.         step.x = 1;
  42.         side_dist.x = (ray_world_pos.x + 1.0f - ray_pos.x) * delta_dist.x;
  43.     }
  44.     if(ray_dir.y < 0)
  45.     {
  46.         step.y = -1;
  47.         side_dist.y = (ray_pos.y - ray_world_pos.y) * delta_dist.y;
  48.     }
  49.     else
  50.     {
  51.         step.y = 1;
  52.         side_dist.y = (ray_world_pos.y + 1.0f - ray_pos.y) * delta_dist.y;
  53.     }
  54.     if(ray_dir.z < 0)
  55.     {
  56.         step.z = -1;
  57.         side_dist.z = (ray_pos.z - ray_world_pos.z) * delta_dist.z;
  58.     }
  59.     else
  60.     {
  61.         step.z = 1;
  62.         side_dist.z = (ray_world_pos.z + 1.0f - ray_pos.z) * delta_dist.z;
  63.     }
  64.     while(max_dist > 0)
  65.     {
  66.         pos_before_hit = ray_world_pos;
  67.         if(side_dist.x < side_dist.y)
  68.         {
  69.             if(side_dist.x < side_dist.z)
  70.             {
  71.                 side_dist.x += delta_dist.x;
  72.                 ray_world_pos.x += step.x;
  73.                 if(ray_world_pos.x > map_width || ray_world_pos.x < 0)
  74.                 {
  75.                     print("oob on x side");
  76.                     state = 3;
  77.                     return;
  78.                 }
  79.             }
  80.             else
  81.             {
  82.                 side_dist.z += delta_dist.z;
  83.                 ray_world_pos.z += step.z;
  84.                 if(ray_world_pos.z > map_depth || ray_world_pos.z < 0)
  85.                 {
  86.                     print("oob on z side");
  87.                     state = 3;
  88.                     return;
  89.                 }
  90.             }
  91.         }
  92.         else
  93.         {
  94.             if(side_dist.y < side_dist.z)
  95.             {
  96.                 side_dist.y += delta_dist.y;
  97.                 ray_world_pos.y += step.y;
  98.                 if(ray_world_pos.y > map_height || ray_world_pos.y < 0)
  99.                 {
  100.                     print("oob on y side");
  101.                     state = 3;
  102.                     return;
  103.                 }
  104.             }
  105.             else
  106.             {
  107.                 side_dist.z += delta_dist.z;
  108.                 ray_world_pos.z += step.z;
  109.                 if(ray_world_pos.z > map_depth || ray_world_pos.z < 0)
  110.                 {
  111.                     print("oob on z side");
  112.                     state = 3;
  113.                     return;
  114.                 }
  115.             }
  116.         }
  117.         u8 check = SM.get_block(ray_world_pos.x, ray_world_pos.y, ray_world_pos.z);
  118.         if(check != block_air)
  119.         {
  120.             print("hit something");
  121.             state = 1;
  122.             hit_pos = ray_world_pos;
  123.             //pos_before_hit = ray_world_pos;
  124.             return;
  125.         }
  126.         max_dist--;
  127.     }
  128.     print("too far");
  129.     state = 2;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement