From 2fc9585797067730f28b03b0727bf05f9deed091 Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Fri, 12 May 2023 10:32:11 +0800 Subject: + worldline keepers --- .../Scripts/Physics/PhysicsManager_Collide.cs | 186 +++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 WorldlineKeepers/Assets/Scripts/Physics/PhysicsManager_Collide.cs (limited to 'WorldlineKeepers/Assets/Scripts/Physics/PhysicsManager_Collide.cs') 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; + +/// +/// 物理查询 +/// +public partial class PhysicsManager : Singleton +{ + // 碰撞检测结果 + private List m_SharedCollideResults = new List(); + + 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; + } + + /// + /// 把x,y,w,h的box转换为lowerx,higherx,lowery,highery + /// + /// + 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 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 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; + } + + /// + /// x,y dir.x dir.y(dir带长度) + /// + /// + /// + /// + public ref readonly List 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 PointCast(ColliderType target, Vector2 point) + { + return ref m_SharedCollideResults; + } + +} \ No newline at end of file -- cgit v1.1-26-g67d0