diff options
author | chai <215380520@qq.com> | 2023-05-10 18:09:12 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2023-05-10 18:09:12 +0800 |
commit | 530881df3968089a8b07e0f9b79185b844d0cdd0 (patch) | |
tree | 6f61565923012e7e8e0c610a10928a5d39230201 /marching/Assets/Scripts/Physics/PhysicsManager_CollisionDetection.cs | |
parent | caeba98e0385edebb344e6dbd024c01801a75fc4 (diff) |
* raycast
Diffstat (limited to 'marching/Assets/Scripts/Physics/PhysicsManager_CollisionDetection.cs')
-rw-r--r-- | marching/Assets/Scripts/Physics/PhysicsManager_CollisionDetection.cs | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/marching/Assets/Scripts/Physics/PhysicsManager_CollisionDetection.cs b/marching/Assets/Scripts/Physics/PhysicsManager_CollisionDetection.cs index a0dcc97..5210767 100644 --- a/marching/Assets/Scripts/Physics/PhysicsManager_CollisionDetection.cs +++ b/marching/Assets/Scripts/Physics/PhysicsManager_CollisionDetection.cs @@ -1,7 +1,11 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; +using static UnityEditor.Rendering.CameraUI; +/// <summary> +/// Åöײ¼ì²â +/// </summary> public partial class PhysicsManager : Singleton<PhysicsManager> { @@ -70,6 +74,88 @@ public partial class PhysicsManager : Singleton<PhysicsManager> return Vector2.Dot(u, u) < radius * radius; } + public static bool RayVsCircle(Vector4 ray, Vector3 circle) + { + Vector4 seg = GetRaySegment(ray); + Vector2 dir = ray.zw().normalized; + Vector2 center = circle.xy(); + float u = Vector2.Dot(center - seg.xy(), dir); + Vector2 near = new Vector2(); + if(u <= 0) + { + near = seg.xy(); + } + else if(u >= ray.zw().magnitude) + { + near = seg.zw(); + } + else + { + near = seg.xy() + dir * u; + } + return (near - center).sqrMagnitude <= circle.z * circle.z; + } + + + // From Real-time Collision Detection, p179 + // box2d + public static bool RayVsBox(Vector4 ray, Vector4 box) + { + float tmin = float.MinValue; + float tmax = float.MaxValue; + Vector2 dir = ray.zw().normalized; + Vector4 range = GetBoxRange(box); //lowerx,higherx,lowery,highery + Vector2 lowerBound = new Vector2(range.x, range.z); + Vector2 upperBound = new Vector2(range.y, range.w); + //Vector2 normal = new Vector2(); + Vector2 p = ray.xy(); + + for (int i = 0; i < 2; ++i) + { + + if (dir[i] < float.Epsilon) // ºÍx-slabƽÐÐ + { + if (p[i] < lowerBound[i] || p[i] > upperBound[i]) + { + return false; + } + } + else + { + float inv_d = 1f / dir[i]; + float t1 = (lowerBound[i] - p[i]) * inv_d; + float t2 = (upperBound[i] - p[i]) * inv_d; + float s = -1f; + if (t1 > t2) + { + MathUtils.Swap(ref t1, ref t2); + s = 1f; + } + if (t1 > tmin) + { + //normal = Vector2.zero; + //normal[i] = s; + tmin = t1; + } + + tmax = Mathf.Min(tmax, t2); + + if (tmin > tmax) + { + return false; + } + } + } + + if (tmin < 0 || ray.zw().magnitude < tmin) + return false; + //// Intersection. + //output->fraction = tmin; + //output->normal = normal; + + return true; + } + /// <summary> /// box x,y,w,h circle x,y,raduis /// </summary> |