Renamed closest_points_ls3 to closest_points_3d, added comments, disable raycast aiming in FPV, get shortest between entity and terrain raycast

This commit is contained in:
Woeful_Wolf 2024-02-02 18:11:37 +02:00
parent 63394719a1
commit 7b843444be
3 changed files with 32 additions and 24 deletions

View File

@ -2201,7 +2201,7 @@ fn closest_points(n: LineSegment2<f32>, m: LineSegment2<f32>) -> (Vec2<f32>, Vec
}
// Get closest point between 2 3D line segments https://math.stackexchange.com/a/4289668
pub fn closest_points_ls3(n: LineSegment3<f32>, m: LineSegment3<f32>) -> (Vec3<f32>, Vec3<f32>) {
pub fn closest_points_3d(n: LineSegment3<f32>, m: LineSegment3<f32>) -> (Vec3<f32>, Vec3<f32>) {
let p1 = n.start;
let p2 = n.end;
let p3 = m.start;

View File

@ -1365,28 +1365,34 @@ impl PlayState for SessionState {
)
});
let dir = if is_aiming && holding_ranged {
// Shoot ray from camera forward direction and get the point it hits an
// entity or terrain
let ray_start = cam_pos + cam_dir * self.scene.camera().get_distance();
let entity_ray_end = ray_start + cam_dir * 500.0;
let dir = if is_aiming
&& holding_ranged
&& self.scene.camera().get_mode() == CameraMode::ThirdPerson
{
// Shoot ray from camera focus forwards and get the point it hits an
// entity or terrain. The ray starts from the camera focus point
// so that the player won't aim at things behind them, in front of the
// camera.
let ray_start = self.scene.camera().get_focus_pos();
let entity_ray_end = ray_start + cam_dir * 1000.0;
let terrain_ray_end = ray_start + cam_dir * 1000.0;
let aim_point =
match ray_entities(&client, ray_start, entity_ray_end, 500.0) {
Some((dist, _)) => ray_start + cam_dir * dist,
None => {
let terrain_ray_distance = client
.state()
.terrain()
.ray(ray_start, terrain_ray_end)
.max_iter(1000)
.until(Block::is_solid)
.cast()
.0;
ray_start + cam_dir * terrain_ray_distance
},
};
let aim_point = {
// Get the distance to nearest entity and terrain
let entity_dist =
ray_entities(&client, ray_start, entity_ray_end, 1000.0).0;
let terrain_ray_distance = client
.state()
.terrain()
.ray(ray_start, terrain_ray_end)
.max_iter(1000)
.until(Block::is_solid)
.cast()
.0;
// Return the hit point of whichever was smaller
ray_start + cam_dir * entity_dist.min(terrain_ray_distance)
};
// Get player orientation
let ori = client

View File

@ -13,7 +13,7 @@ use common::{
vol::ReadVol,
};
use common_base::span;
use common_systems::phys::closest_points_ls3;
use common_systems::phys::closest_points_3d;
#[derive(Clone, Copy, Debug)]
pub struct Target<T> {
@ -245,7 +245,7 @@ pub(super) fn ray_entities(
start: Vec3<f32>,
end: Vec3<f32>,
cast_dist: f32,
) -> Option<(f32, Entity)> {
) -> (f32, Option<Entity>) {
let player_entity = client.entity();
let ecs = client.state().ecs();
let positions = ecs.read_storage::<comp::Pos>();
@ -314,7 +314,7 @@ pub(super) fn ray_entities(
start: world_p0,
end: world_p1,
};
closest_points_ls3(seg_ray, seg_capsule)
closest_points_3d(seg_ray, seg_capsule)
} else {
let nearest = seg_ray.projected_point(world_p0);
(nearest, world_p0)
@ -344,4 +344,6 @@ pub(super) fn ray_entities(
};
});
entity
.map(|(dist, e)| (dist, Some(e)))
.unwrap_or((cast_dist, None))
}