summaryrefslogtreecommitdiff
path: root/WorldlineKeepers/Assets/Scripts/Physics/PhysicsManager_Collide.cs
diff options
context:
space:
mode:
Diffstat (limited to 'WorldlineKeepers/Assets/Scripts/Physics/PhysicsManager_Collide.cs')
-rw-r--r--WorldlineKeepers/Assets/Scripts/Physics/PhysicsManager_Collide.cs186
1 files changed, 186 insertions, 0 deletions
diff --git a/WorldlineKeepers/Assets/Scripts/Physics/PhysicsManager_Collide.cs b/WorldlineKeepers/Assets/Scripts/Physics/PhysicsManager_Collide.cs
new file mode 100644
index 0000000..4710b2e
--- /dev/null
+++ b/WorldlineKeepers/Assets/Scripts/Physics/PhysicsManager_Collide.cs
@@ -0,0 +1,186 @@
+using mh;
+using System.Collections;
+using System.Collections.Generic;
+using Unity.VisualScripting.Antlr3.Runtime.Tree;
+using UnityEngine;
+using UnityEngine.UIElements;
+
+/// <summary>
+/// 物理查询
+/// </summary>
+public partial class PhysicsManager : Singleton<PhysicsManager>
+{
+ // 碰撞检测结果
+ private List<IQuadTreeObject> m_SharedCollideResults = new List<IQuadTreeObject>();
+
+ public static Vector4 GetCircleBound(Vector3 circle)
+ {
+ float size = circle.z * 2;
+ return new Vector4(circle.x, circle.y, size, size);
+ }
+
+ public static Vector4 GetBoxBound(Vector4 box)
+ {
+ return box;
+ }
+
+ public static Vector4 GetRayBound(Vector4 ray)
+ {
+ Vector2 tr = ray.xy() + ray.zw();
+ Vector4 bound = new Vector4();
+ bound.x = (tr.x + ray.x) / 2;
+ bound.y = (tr.y + ray.y) / 2;
+ bound.z = Mathf.Max(Mathf.Abs(tr.x - ray.x), 1);
+ bound.w = Mathf.Max(Mathf.Abs(tr.y - ray.y), 1);
+ return bound;
+ }
+
+ public static Vector4 GetRaySegment(Vector4 ray)
+ {
+ Vector2 tr = ray.xy() + ray.zw();
+ Vector4 seg = new Vector4();
+ seg.x = ray.x;
+ seg.y = ray.y;
+ seg.z = tr.x;
+ seg.w = tr.y;
+ return seg;
+ }
+
+ public static Vector4 GetPointBound(Vector2 point)
+ {
+ Vector4 bound = new Vector4();
+ bound.x = point.x;
+ bound.y = point.y;
+ bound.z = 1;
+ bound.w = 1;
+ return bound;
+ }
+
+ /// <summary>
+ /// 把x,y,w,h的box转换为lowerx,higherx,lowery,highery
+ /// </summary>
+ /// <returns></returns>
+ public static Vector4 GetBoxRange(Vector4 box)
+ {
+ Vector4 Range = new();
+ Range.x = box.x - box.z / 2;
+ Range.y = box.x + box.z / 2;
+ Range.z = box.y - box.w / 2;
+ Range.w = box.y + box.w / 2;
+ return Range;
+ }
+
+ public ref readonly List<IQuadTreeObject> CircleCast(ColliderType target, Vector3 circle)
+ {
+ m_SharedCollideResults.Clear();
+ var retriver = GetRetriverByType(target);
+ if(retriver != null)
+ {
+ if (retriver(GetCircleBound(circle)))
+ {
+ for(int i = 0; i < m_SharedRetriveResults.Count; ++i)
+ {
+ var collider = m_SharedRetriveResults[i];
+ if(collider != null)
+ {
+ if(collider is FastCircleCollider)
+ {
+ if(CircleVsCircle((collider as FastCircleCollider).circle, circle))
+ {
+ m_SharedCollideResults.Add(collider);
+ }
+ }
+ else if(collider is FastBoxCollider)
+ {
+ if (BoxVsCircle((collider as FastBoxCollider).box, circle))
+ {
+ m_SharedCollideResults.Add(collider);
+ }
+ }
+ }
+ }
+ }
+ }
+ return ref m_SharedCollideResults;
+ }
+
+ public ref readonly List<IQuadTreeObject> BoxCast(ColliderType target, Vector4 box)
+ {
+ m_SharedCollideResults.Clear();
+ var retriver = GetRetriverByType(target);
+ if (retriver != null)
+ {
+ if (retriver(GetBoxBound(box)))
+ {
+ for (int i = 0; i < m_SharedRetriveResults.Count; ++i)
+ {
+ var collider = m_SharedRetriveResults[i];
+ if (collider != null)
+ {
+ if (collider is FastCircleCollider)
+ {
+ if (BoxVsCircle(box, (collider as FastCircleCollider).circle))
+ {
+ m_SharedCollideResults.Add(collider);
+ }
+ }
+ else if (collider is FastBoxCollider)
+ {
+ if (BoxVsBox(box, (collider as FastBoxCollider).box))
+ {
+ m_SharedCollideResults.Add(collider);
+ }
+ }
+ }
+ }
+ }
+ }
+ return ref m_SharedCollideResults;
+ }
+
+ /// <summary>
+ /// x,y dir.x dir.y(dir带长度)
+ /// </summary>
+ /// <param name="target"></param>
+ /// <param name="line"></param>
+ /// <returns></returns>
+ public ref readonly List<IQuadTreeObject> RayCast(ColliderType target, Vector4 ray)
+ {
+ m_SharedCollideResults.Clear();
+ var retriver = GetRetriverByType(target);
+ if(retriver != null)
+ {
+ if(retriver(GetRayBound(ray)))
+ {
+ for (int i = 0; i < m_SharedRetriveResults.Count; ++i)
+ {
+ var collider = m_SharedRetriveResults[i];
+ if (collider != null)
+ {
+ if (collider is FastCircleCollider)
+ {
+ if (RayVsCircle(ray, (collider as FastCircleCollider).circle))
+ {
+ m_SharedCollideResults.Add(collider);
+ }
+ }
+ else if (collider is FastBoxCollider)
+ {
+ if (RayVsBox(ray, (collider as FastBoxCollider).box))
+ {
+ m_SharedCollideResults.Add(collider);
+ }
+ }
+ }
+ }
+ }
+ }
+ return ref m_SharedCollideResults;
+ }
+
+ public ref readonly List<IQuadTreeObject> PointCast(ColliderType target, Vector2 point)
+ {
+ return ref m_SharedCollideResults;
+ }
+
+} \ No newline at end of file