Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // here is how i call it
- if(c.isKeyJustPressed(KEY_RBUTTON))
- {
- int state = 0;
- vec3d hit_pos = vec3d();
- vec3d pos_before_hit = vec3d();
- uint block_offset;
- vec3d ray_pos = pos;
- ray_pos.y += cam_height;
- vec3d look_dir( Maths::Sin((dir_x)*Maths::Pi/180)*Maths::Cos(dir_y*Maths::Pi/180),
- Maths::Sin(dir_y*Maths::Pi/180),
- Maths::Cos((dir_x)*Maths::Pi/180)*Maths::Cos(dir_y*Maths::Pi/180));
- Raycast_precise(@SM, ray_pos, look_dir, 16, state, hit_pos, pos_before_hit);
- if(state == 1)
- {
- SM.set_block(pos_before_hit.x, pos_before_hit.y, pos_before_hit.z, block_stone);
- pos_before_hit = pos_before_hit/16;
- 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);
- SM.chunks[offset].rebuildMesh();
- }
- }
- // function itself
- 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)
- {
- vec3d ray_world_pos = vec3d(int(ray_pos.x), int(ray_pos.y), int(ray_pos.z));
- vec3d delta_dist = vec3d(Maths::Abs(1/ray_dir.x), Maths::Abs(1/ray_dir.y), Maths::Abs(1/ray_dir.z));
- vec3d side_dist;
- vec3d step;
- if(ray_dir.x < 0)
- {
- step.x = -1;
- side_dist.x = (ray_pos.x - ray_world_pos.x) * delta_dist.x;
- }
- else
- {
- step.x = 1;
- side_dist.x = (ray_world_pos.x + 1.0f - ray_pos.x) * delta_dist.x;
- }
- if(ray_dir.y < 0)
- {
- step.y = -1;
- side_dist.y = (ray_pos.y - ray_world_pos.y) * delta_dist.y;
- }
- else
- {
- step.y = 1;
- side_dist.y = (ray_world_pos.y + 1.0f - ray_pos.y) * delta_dist.y;
- }
- if(ray_dir.z < 0)
- {
- step.z = -1;
- side_dist.z = (ray_pos.z - ray_world_pos.z) * delta_dist.z;
- }
- else
- {
- step.z = 1;
- side_dist.z = (ray_world_pos.z + 1.0f - ray_pos.z) * delta_dist.z;
- }
- while(max_dist > 0)
- {
- pos_before_hit = ray_world_pos;
- if(side_dist.x < side_dist.y)
- {
- if(side_dist.x < side_dist.z)
- {
- side_dist.x += delta_dist.x;
- ray_world_pos.x += step.x;
- if(ray_world_pos.x > map_width || ray_world_pos.x < 0)
- {
- print("oob on x side");
- state = 3;
- return;
- }
- }
- else
- {
- side_dist.z += delta_dist.z;
- ray_world_pos.z += step.z;
- if(ray_world_pos.z > map_depth || ray_world_pos.z < 0)
- {
- print("oob on z side");
- state = 3;
- return;
- }
- }
- }
- else
- {
- if(side_dist.y < side_dist.z)
- {
- side_dist.y += delta_dist.y;
- ray_world_pos.y += step.y;
- if(ray_world_pos.y > map_height || ray_world_pos.y < 0)
- {
- print("oob on y side");
- state = 3;
- return;
- }
- }
- else
- {
- side_dist.z += delta_dist.z;
- ray_world_pos.z += step.z;
- if(ray_world_pos.z > map_depth || ray_world_pos.z < 0)
- {
- print("oob on z side");
- state = 3;
- return;
- }
- }
- }
- u8 check = SM.get_block(ray_world_pos.x, ray_world_pos.y, ray_world_pos.z);
- if(check != block_air)
- {
- print("hit something");
- state = 1;
- hit_pos = ray_world_pos;
- //pos_before_hit = ray_world_pos;
- return;
- }
- max_dist--;
- }
- print("too far");
- state = 2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement