From 530881df3968089a8b07e0f9b79185b844d0cdd0 Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Wed, 10 May 2023 18:09:12 +0800 Subject: * raycast --- .../Scripts/Physics/PhysicsManager_Collide.cs | 81 +++++++++++++++++++--- 1 file changed, 70 insertions(+), 11 deletions(-) (limited to 'marching/Assets/Scripts/Physics/PhysicsManager_Collide.cs') diff --git a/marching/Assets/Scripts/Physics/PhysicsManager_Collide.cs b/marching/Assets/Scripts/Physics/PhysicsManager_Collide.cs index 7b4aa1c..4710b2e 100644 --- a/marching/Assets/Scripts/Physics/PhysicsManager_Collide.cs +++ b/marching/Assets/Scripts/Physics/PhysicsManager_Collide.cs @@ -3,34 +3,50 @@ 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 Vector4 GetCircleBound(Vector3 circle) + public static Vector4 GetCircleBound(Vector3 circle) { float size = circle.z * 2; return new Vector4(circle.x, circle.y, size, size); } - public Vector4 GetBoxBound(Vector4 box) + public static Vector4 GetBoxBound(Vector4 box) { return box; } - public Vector4 GetRayBound(Vector4 line, float maxLen=20f) + public static Vector4 GetRayBound(Vector4 ray) { - Vector2 tr = line.xy() + maxLen * line.zw().normalized; + Vector2 tr = ray.xy() + ray.zw(); Vector4 bound = new Vector4(); - bound.x = (tr.x + line.x) / 2; - bound.y = (tr.y + line.y) / 2; - bound.z = Mathf.Abs(tr.x - line.x); - bound.w = Mathf.Abs(tr.y - line.y); + 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 Vector4 GetPointBound(Vector2 point) + 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; @@ -40,6 +56,20 @@ public partial class PhysicsManager : Singleton 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(); @@ -109,13 +139,42 @@ public partial class PhysicsManager : Singleton } /// - /// x,y dir.x dir.y + /// x,y dir.x dir.y(dir带长度) /// /// /// /// - public ref readonly List RayCast(ColliderType target, Vector4 ray, float maxLength = 20f) + 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; } -- cgit v1.1-26-g67d0