From 8113b3c5678a48ad7fefbe30f1a746effb49e44b Mon Sep 17 00:00:00 2001 From: spatialfree Date: Mon, 16 Dec 2024 07:15:31 -0500 Subject: [PATCH] nearest raycast hit across enemy cols --- src/Arts.cs | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/Arts.cs b/src/Arts.cs index aaa9565..72aab95 100644 --- a/src/Arts.cs +++ b/src/Arts.cs @@ -165,26 +165,41 @@ static class Arts if (true) // hit test { - Vec3 hit_delta = rvolv_ori.Inverse * (enemy_pos - rvolv_pos); - float flat_z = hit_delta.z; - hit_delta.z = 0; - float hit_mag = hit_delta.Magnitude; - bool hit = hit_mag < enemy_rad; + Matrix enemy_m4 = enemy_test.pose.ToMatrix(); + Ray local_ray = enemy_m4.Inverse.Transform(ray); + bool hit = false; + float min_dist = float.MaxValue; + Vec3 min_hit_pos = Vec3.Zero; + for (int i = 0; i < enemy_test.cols.Count; i++) + { + Sphere col = enemy_test.cols[i]; + Vec3 hit_pos = Vec3.Zero; + if (col.Intersect(local_ray, out hit_pos)) + { + float hit_dist = Vec3.Distance(hit_pos, local_ray.position); + if (hit_dist < min_dist) + { + hit = true; + min_dist = hit_dist; + min_hit_pos = hit_pos; + } + } + } + min_hit_pos = enemy_m4.Transform(min_hit_pos); + if (hit) { - float hit_dist = flat_z + (Maths.smooth_stop((enemy_rad - hit_mag) / enemy_rad) * enemy_rad); - Vec3 hit_pos = rvolv_pos + rvolv_ori * V.XYZ(0, 0, hit_dist); Mesh.Sphere.Draw( mat_unlit, Matrix.TS( - hit_pos, + min_hit_pos, 4 * U.cm ), Color.White ); if (Rig.btn_select.delta == +1) { - VFX.Play(hit_pos); + VFX.Play(min_hit_pos); } } }