summaryrefslogtreecommitdiff
path: root/marching/Assets/Scripts/Physics/PhysicsManager_Collide.cs
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2023-05-10 18:09:12 +0800
committerchai <215380520@qq.com>2023-05-10 18:09:12 +0800
commit530881df3968089a8b07e0f9b79185b844d0cdd0 (patch)
tree6f61565923012e7e8e0c610a10928a5d39230201 /marching/Assets/Scripts/Physics/PhysicsManager_Collide.cs
parentcaeba98e0385edebb344e6dbd024c01801a75fc4 (diff)
* raycast
Diffstat (limited to 'marching/Assets/Scripts/Physics/PhysicsManager_Collide.cs')
-rw-r--r--marching/Assets/Scripts/Physics/PhysicsManager_Collide.cs81
1 files changed, 70 insertions, 11 deletions
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;
+/// <summary>
+/// 物理查询
+/// </summary>
public partial class PhysicsManager : Singleton<PhysicsManager>
{
+ // 碰撞检测结果
private List<IQuadTreeObject> m_SharedCollideResults = new List<IQuadTreeObject>();
- 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<PhysicsManager>
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();
@@ -109,13 +139,42 @@ public partial class PhysicsManager : Singleton<PhysicsManager>
}
/// <summary>
- /// x,y dir.x dir.y
+ /// 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, float maxLength = 20f)
+ 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;
}