From 22891bf59032ba88262824255a706d652031384b Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 10 Mar 2022 14:07:40 +0800 Subject: * move folder --- Assets/Scripts/Physics/PhysicsBall.cs | 39 --- Assets/Scripts/Physics/PhysicsBall.cs.meta | 11 - Assets/Scripts/Physics/PhysicsBody.cs | 167 ----------- Assets/Scripts/Physics/PhysicsBody.cs.meta | 11 - Assets/Scripts/Physics/PhysicsBox.cs | 82 ------ Assets/Scripts/Physics/PhysicsBox.cs.meta | 11 - Assets/Scripts/Physics/PhysicsHelper.cs | 112 -------- Assets/Scripts/Physics/PhysicsHelper.cs.meta | 11 - Assets/Scripts/Physics/PhysicsPrimGroup.cs | 19 -- Assets/Scripts/Physics/PhysicsPrimGroup.cs.meta | 11 - Assets/Scripts/Physics/PhysicsPrimitive.cs | 199 ------------- Assets/Scripts/Physics/PhysicsPrimitive.cs.meta | 11 - Assets/Scripts/Physics/PhysicsWorld.cs | 356 ------------------------ Assets/Scripts/Physics/PhysicsWorld.cs.meta | 11 - 14 files changed, 1051 deletions(-) delete mode 100644 Assets/Scripts/Physics/PhysicsBall.cs delete mode 100644 Assets/Scripts/Physics/PhysicsBall.cs.meta delete mode 100644 Assets/Scripts/Physics/PhysicsBody.cs delete mode 100644 Assets/Scripts/Physics/PhysicsBody.cs.meta delete mode 100644 Assets/Scripts/Physics/PhysicsBox.cs delete mode 100644 Assets/Scripts/Physics/PhysicsBox.cs.meta delete mode 100644 Assets/Scripts/Physics/PhysicsHelper.cs delete mode 100644 Assets/Scripts/Physics/PhysicsHelper.cs.meta delete mode 100644 Assets/Scripts/Physics/PhysicsPrimGroup.cs delete mode 100644 Assets/Scripts/Physics/PhysicsPrimGroup.cs.meta delete mode 100644 Assets/Scripts/Physics/PhysicsPrimitive.cs delete mode 100644 Assets/Scripts/Physics/PhysicsPrimitive.cs.meta delete mode 100644 Assets/Scripts/Physics/PhysicsWorld.cs delete mode 100644 Assets/Scripts/Physics/PhysicsWorld.cs.meta (limited to 'Assets/Scripts/Physics') diff --git a/Assets/Scripts/Physics/PhysicsBall.cs b/Assets/Scripts/Physics/PhysicsBall.cs deleted file mode 100644 index 5baceeb8..00000000 --- a/Assets/Scripts/Physics/PhysicsBall.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -/// -/// 碰撞球 -/// -public class PhysicsBall : PhysicsPrimitive -{ - public override PrimitiveType Type - { - get - { - return PrimitiveType.Ball; - } - } - - [SerializeField] - private float m_Radius; - - public float Radius - { - get - { - return m_Radius; - } - } - - public void OnDrawGizmos() - { - Gizmos.DrawSphere(m_Center, m_Radius); - } - - void Start() - { - base.OnInit(); - } - -} diff --git a/Assets/Scripts/Physics/PhysicsBall.cs.meta b/Assets/Scripts/Physics/PhysicsBall.cs.meta deleted file mode 100644 index 39ba3e3f..00000000 --- a/Assets/Scripts/Physics/PhysicsBall.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 04071fd85519f204fb0d0dc080c41bae -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Physics/PhysicsBody.cs b/Assets/Scripts/Physics/PhysicsBody.cs deleted file mode 100644 index 80026962..00000000 --- a/Assets/Scripts/Physics/PhysicsBody.cs +++ /dev/null @@ -1,167 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -// 需要受物理系统托管的动作: -// 跳跃上升 -// 跳跃下降 -// 空中受击 -// -// -// 不会受物理系统托管的动作: -// 单个招式 -// 空中连击 -// -// -// 一般来说启用物理是被动的 - -/// -/// 刚体 -/// -public sealed class PhysicsBody : MonoBehaviour -{ - - // 是否朝向右侧(正向) - public bool IsFaceRight - { - get - { - float rotY = Quaternion.ToEulerAngles(transform.rotation).y; - rotY = Mathf.Rad2Deg * rotY; - bool right = rotY >= 0 && rotY <= 180; - return right; - } - } - - // 全局速度,以世界为参考系 - [SerializeField] - public Vector3 Velocity - { - get - { - Vector3 vel = m_LocalVelocity; - if (!IsFaceRight) - vel.x = -vel.x; - return vel; - } - set - { - if (IsFaceRight) - m_LocalVelocity = value; - else - m_LocalVelocity = new Vector3(-value.x, value.y, value.z); - } - } - - // 以自身为参考系的速度,x>0向前,x<0向后,y>0向上,y<0向下。默认情况下以右为正方向 - // 设置速度应该以local velocity为准 - [SerializeField] - private Vector3 m_LocalVelocity; - - public Vector3 LocalVelocity - { - get - { - return m_LocalVelocity; - } - set - { - m_LocalVelocity = value; - } - } - - [SerializeField] - private float m_Weight; - public float Weight - { - get - { - return m_Weight; - } - } - - [Tooltip("ignore gravity?")] - [SerializeField] - private bool m_UseGravity; - public bool UseGravity - { - get - { - return m_UseGravity; - } - set - { - m_UseGravity = value; - } - } - - [SerializeField] - private Vector3 m_Accelaration; - public Vector3 Accelaration - { - get - { - return m_Accelaration; - } - } - - [Tooltip("地面摩擦系数")] - [SerializeField] - private float m_Frication; - public float GroundFriction - { - get { return m_Frication; } - } - - [Tooltip("空气摩擦力")] - [SerializeField] - private float m_AirFriction; - public float AirFriction - { - get { return m_AirFriction; } - } - - [Tooltip("力")] - [SerializeField] - private Vector3 m_Force; - public Vector3 Force - { - get - { - return m_Force; - } - } - - [SerializeField] - private PhysicsPrimitive m_Primitive; - - - public void AddForce(Vector3 force) - { - m_Force += force; - } - - public void AddLocalForce(Vector3 localForce) - { - m_Force += localToWorldDir(localForce); - } - - public void SetForce(Vector3 force) - { - m_Force = force; - } - - public void SetLocalForce(Vector3 localForce) - { - m_Force = localToWorldDir(localForce); - } - - public Vector3 localToWorldDir(Vector3 local) - { - if (IsFaceRight) - return local; - else - return new Vector3(-local.x, local.y, local.z); - } - -} diff --git a/Assets/Scripts/Physics/PhysicsBody.cs.meta b/Assets/Scripts/Physics/PhysicsBody.cs.meta deleted file mode 100644 index a80b9d50..00000000 --- a/Assets/Scripts/Physics/PhysicsBody.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: cd65b198d10c5b441b5c40b05c8f3d41 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Physics/PhysicsBox.cs b/Assets/Scripts/Physics/PhysicsBox.cs deleted file mode 100644 index f8b4fee4..00000000 --- a/Assets/Scripts/Physics/PhysicsBox.cs +++ /dev/null @@ -1,82 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -/// -/// AABB碰撞盒 -/// -public class PhysicsBox : PhysicsPrimitive -{ - public override PrimitiveType Type - { - get - { - return PrimitiveType.Box; - } - } - - [SerializeField] - private Vector3 m_Size; - public Vector3 Size - { - get - { - return m_Size; - } - } - - public float Long { get { return m_Size.x; } } - public float Wide { get { return m_Size.z; } } - public float Height { get { return m_Size.y; } } - - public bool m_DrawGizmo = true; - - public float Top - { - get - { - return Position.y + Height / 2f; - } - } - - public float Left - { - get - { - return Position.x - Long / 2f; - } - } - - public float Right - { - get - { - return Position.x + Long / 2f; - } - } - - public float Bottom - { - get - { - return Position.y - Height / 2f; - } - } - - public void OnDrawGizmos() - { - if (!m_IsActive || !m_DrawGizmo) - return; - Vector3 pos = Position; - Gizmos.color = m_HintColor; - Gizmos.DrawCube(pos, m_Size); - //Gizmos.color = Color.blue; - //Gizmos.DrawSphere(pos, 0.1f); - } - - private void Start() - { - base.OnInit(); - } - -} diff --git a/Assets/Scripts/Physics/PhysicsBox.cs.meta b/Assets/Scripts/Physics/PhysicsBox.cs.meta deleted file mode 100644 index 528c7474..00000000 --- a/Assets/Scripts/Physics/PhysicsBox.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 637138a0ba30abc498c5c9dcf6bb5d78 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Physics/PhysicsHelper.cs b/Assets/Scripts/Physics/PhysicsHelper.cs deleted file mode 100644 index 44bf513e..00000000 --- a/Assets/Scripts/Physics/PhysicsHelper.cs +++ /dev/null @@ -1,112 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public enum CollisionType -{ - BallvsBall = 1, - BallvsBox, - BoxvsBox -} - - -/// -/// 碰撞的信息 -/// -public struct PhysicsCollisionInfo -{ - public CollisionType type; - public PhysicsPrimitive prim1; - public PhysicsPrimitive prim2; - public Vector3 contact; - public Vector3 size; -} - -public sealed class PhysicsHelper -{ - - public static bool BallContains(PhysicsBall ball, Vector3 point) - { - return false; - } - - public static bool BallContains(PhysicsBall ball, Vector2 point) - { - return false; - } - - public static bool BoxContains(PhysicsBox box, Vector3 point) - { - return false; - } - - public static bool BoxContains(PhysicsBox box, Vector2 point) - { - return false; - } - - public static bool BallvsBall2D(PhysicsBall ball1, PhysicsBall ball2, ref PhysicsCollisionInfo info) - { - - return true; - } - - public static bool BoxvsBox2D(PhysicsBox box1, PhysicsBox box2, ref PhysicsCollisionInfo info) - { - if(box1.Left < box2.Right && box1.Right > box2.Left && box1.Bottom < box2.Top && box1.Top > box2.Bottom) - { - info.type = CollisionType.BoxvsBox; - info.prim1 = box1; - info.prim2 = box2; - - float left = Mathf.Max(box1.Left, box2.Left); - float right = Mathf.Min(box1.Right, box2.Right); - float top = Mathf.Min(box1.Top, box2.Top); - float bottom = Mathf.Max(box1.Bottom, box2.Bottom); - info.contact = new Vector3((left + right )/2f, (top + bottom)/2f, 0 ); - info.size = new Vector3(right - left, top - bottom, 1); - - return true; - } - return false; - } - - public static bool BallvsBox2D(PhysicsBall ball, PhysicsBox box, ref PhysicsCollisionInfo info) - { - return true; - } - - public static bool BallvsRay(PhysicsBall ball, Vector3 ray) - { - return false; - } - - public static bool BoxvsRay(PhysicsBox box, Vector3 ray) - { - return false; - } - - public static bool RayvsRay(Vector3 ray1, Vector3 ray2) - { - return false; - } - - public static bool PrimvsPrim(PhysicsPrimitive prim1, PhysicsPrimitive prim2, ref PhysicsCollisionInfo info) - { - if(prim1.Type == PrimitiveType.Ball) - { - if (prim2.Type == PrimitiveType.Ball) - return BallvsBall2D(prim1 as PhysicsBall, prim2 as PhysicsBall, ref info); - else - return BallvsBox2D(prim1 as PhysicsBall, prim2 as PhysicsBox, ref info); - } - else - { - if (prim2.Type == PrimitiveType.Ball) - return BallvsBox2D(prim2 as PhysicsBall, prim1 as PhysicsBox, ref info); - else - return BoxvsBox2D(prim1 as PhysicsBox, prim2 as PhysicsBox, ref info); - } - } - -} diff --git a/Assets/Scripts/Physics/PhysicsHelper.cs.meta b/Assets/Scripts/Physics/PhysicsHelper.cs.meta deleted file mode 100644 index 6259c4db..00000000 --- a/Assets/Scripts/Physics/PhysicsHelper.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 5feab7ce586a9aa468329f97b89a69a3 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Physics/PhysicsPrimGroup.cs b/Assets/Scripts/Physics/PhysicsPrimGroup.cs deleted file mode 100644 index 0a79eeff..00000000 --- a/Assets/Scripts/Physics/PhysicsPrimGroup.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class PhysicsPrimGroup : MonoBehaviour -{ - - [SerializeField] - private PhysicsPrimitive[] m_Primitives; - - public PhysicsPrimitive[] Primitives - { - get - { - return m_Primitives; - } - } - -} diff --git a/Assets/Scripts/Physics/PhysicsPrimGroup.cs.meta b/Assets/Scripts/Physics/PhysicsPrimGroup.cs.meta deleted file mode 100644 index 48e8ed4e..00000000 --- a/Assets/Scripts/Physics/PhysicsPrimGroup.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 1f05f73c4d0805e4ba5dd659c305c91c -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Physics/PhysicsPrimitive.cs b/Assets/Scripts/Physics/PhysicsPrimitive.cs deleted file mode 100644 index fde705fd..00000000 --- a/Assets/Scripts/Physics/PhysicsPrimitive.cs +++ /dev/null @@ -1,199 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public enum PrimitiveType -{ - None, - Ball, - Box -} - -/// -/// 物理碰撞体基类 -/// 对于格斗游戏来说只需要Box和Ball两种 -/// -public abstract class PhysicsPrimitive : MonoBehaviour -{ - public virtual PrimitiveType Type - { - get - { - return PrimitiveType.None; - } - } - - /// - /// 世界坐标,物理模拟是在世界空间,本地坐标没有意义 - /// - public Vector3 Position - { - get - { - Vector3 euler = Quaternion.ToEulerAngles(transform.rotation); - euler.y = Mathf.Rad2Deg * euler.y; - Vector3 res = m_Center; - res.x = (euler.y > 0 && euler.y <= 180) ? -res.x : res.x; - res = transform.position + res; - return res; - } - } - - [SerializeField] - /// - /// 这个primitive是否参与物理计算,用来快速给物体取消和恢复重力影响 - /// - protected bool m_IsActive; - public bool IsActive - { - get - { - return m_IsActive; - } - } - - /// - /// 唯一ID - /// - protected int m_ID; - public int ID - { - get - { - return m_ID; - } - } - - [Tooltip("给primitive分组,决定大组和大组之间是否有碰撞检测")] - [SerializeField] - protected PhysicsGroup m_Group; - public PhysicsGroup Group - { - get - { - return m_Group; - } - } - - [Tooltip("给primitive一个标记,表明它的从属关系。帮助做hitbox和hurtbox")] - [SerializeField] - protected PhysicsTag m_Tag; - public PhysicsTag Tag - { - get - { - return m_Tag; - } - } - - [Tooltip("同一个标签的没有碰撞检测")] - [SerializeField] - protected string m_Label; - public string Label - { - get - { - return m_Label; - } - } - - [Tooltip("单个碰撞体的描述(名称),作为标识用来识别")] - [SerializeField] - protected string m_Title; - public string Title - { - get - { - return m_Title; - } - } - - [SerializeField] - protected string m_Desc; - public string Desc - { - get - { - return m_Desc; - } - } - - /// - /// 初始化,设置一些公共数据 - /// - protected void OnInit() - { - m_ID = UIDManager.Acquire(); - PhysicsWorld.Instance.AddPrimitive(this); - } - - protected Color Color_Green = new Color(0, 1, 0, 0.5f); - - [SerializeField] - protected Color m_HintColor = new Color(0, 0, 0, 0.5f); - - [Tooltip("Physics body, leave blank and primitive will be static.")] - [SerializeField] - protected PhysicsBody m_Body; - - public PhysicsBody Body - { - get - { - return m_Body; - } - } - - /// - /// 中心点在本地空间的位置 - /// - public Vector3 m_Center; - - /// - /// 边界:左、右、上、下 - /// - public Vector4 Bound - { - get - { - Vector4 bound = new Vector4(); - if(this is PhysicsBox) - { - PhysicsBox box = this as PhysicsBox; - bound.x = box.Left; - bound.y = box.Right; - bound.z = box.Top; - bound.w = box.Bottom; - } - else if(this is PhysicsBall) - { - PhysicsBall ball = this as PhysicsBall; - bound.x = ball.Position.x - ball.Radius; - bound.y = ball.Position.x + ball.Radius; - bound.z = ball.Position.y + ball.Radius; - bound.w = ball.Position.y - ball.Radius; - } - return bound; - } - } - - public bool IsOnGround - { - get - { - if (Mathf.Approximately(Bound.w, PhysicsWorld.Ground)) - return true; - return Bound.w <= PhysicsWorld.Ground; - } - } - - public bool IsInAir - { - get - { - //return Bound.w > PhysicsWorld.Ground; - return !IsOnGround; - } - } - -} \ No newline at end of file diff --git a/Assets/Scripts/Physics/PhysicsPrimitive.cs.meta b/Assets/Scripts/Physics/PhysicsPrimitive.cs.meta deleted file mode 100644 index 725a8459..00000000 --- a/Assets/Scripts/Physics/PhysicsPrimitive.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: bf2b8ac8c3958a449891e20d634a76de -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Physics/PhysicsWorld.cs b/Assets/Scripts/Physics/PhysicsWorld.cs deleted file mode 100644 index 7b7787d4..00000000 --- a/Assets/Scripts/Physics/PhysicsWorld.cs +++ /dev/null @@ -1,356 +0,0 @@ - using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -/// -/// primitive分类 -/// -public enum PhysicsGroup -{ - Character, // 角色 - Prop, // 物体 - Ground, // 地面 - Wall, // 墙面 - - HitBox, // hitbox - HurtBox, // hurtbox - - GroupCount, -} - -/// -/// primitive标记 -/// -public enum PhysicsTag -{ - Null = 0, - Player = 1, // 从属于玩家 - Oponent = 1 << 1, // 从属于对手 -} - -/// -/// 物理子系统 -/// -public class PhysicsWorld : Singleton -{ - public delegate void Callback(); - - public Callback onBeforeUpdate; - public Callback onUpdate; - - private int m_UpdateRate = 60; - // 重力加速度 - private readonly Vector3 m_Gravity = new Vector3(0, -30f, 0); - // 当前管理的碰撞体 - private List m_Primitives = new List(); - private float m_TimeCount; - - private const int _ = 0; - private readonly int[] m_CollisionTable = { - // hurtbox hitBox wall ground prop character -/*character*/ 0, 0, 1, 1, 1, 1, -/*prop */ 0, 0, 1, 1, 0, _, -/*ground */ 0, 0, 0, 0, _, _, -/*wall */ 0, 0, 0, _, _, _, -/*hitbox */ 1, 0, _, _, _, _, -/*hurtbox */ 0, _, _, _, _, _, - }; - - private List m_Animators = new List(); -#if UNITY_EDITOR - private List m_Contacts = new List(); -#endif - - private List m_CollisionInfo = new List(); - - public const float Ground = 0.1f; - - // 所有碰撞 - public List Collisions - { - get - { - return m_CollisionInfo; - } - } - - public void Init() - { - m_TimeCount = Time.time; - } - - public void AddPrimitive(PhysicsPrimitive prim) - { - if (prim == null) - return; - if(m_Primitives.Contains(prim)) - { - Debug.LogError("PhysicsWorld已经存在此碰撞体,ID=" + prim.ID + ", 类型=" + prim.Tag); - return; - } - m_Primitives.Add(prim); - } - - public void RemovePrimitive(PhysicsPrimitive prim) - { - if (prim == null) - return; - m_Primitives.Remove(prim); - } - - public void AddAnimator(Animator animator) - { - if (m_Animators.Contains(animator)) - return; - m_Animators.Add(animator); - } - - public void RemoveAnimator(Animator animator) - { - if (m_Animators.Contains(animator)) - m_Animators.Remove(animator); - } - - /// - /// 物理系统以稳定的逻辑帧率执行 - /// - public void Update() - { - float preTime = m_TimeCount; - m_TimeCount = Time.time; - float deltaTime = m_TimeCount - preTime; - while (deltaTime > 1f / m_UpdateRate) - { - BeforeTick(); - onBeforeUpdate?.Invoke(); - Tick(); - onUpdate?.Invoke(); - deltaTime -= 1f / m_UpdateRate; - } - m_TimeCount -= deltaTime; - } - - // 更新之前 - private void BeforeTick() - { - m_CollisionInfo.Clear(); - } - - public void DrawGizmos() - { -#if UNITY_EDITOR - if (m_Contacts.Count == 0) - return; - - for(int i = 0; i < m_Contacts.Count; ++i) - { - Vector3 center = m_Contacts[i]; - Gizmos.DrawSphere(center, 0.05f); - } -#endif - } - - - void Tick() - { - float deltaTime = 1f / m_UpdateRate; - // animator -> OnAnimatorMove() -> physics - //UpdateAnimator(deltaTime); - UpdatePrimitives(deltaTime); - } - - // 更新动画,并处理OnAnimatorMove() - void UpdateAnimator(float deltaTime) - { - for (int i = 0; i < m_Animators.Count; ++i) - { - Animator animator = m_Animators[i]; - animator.speed = 1; - animator.Update(deltaTime); - animator.speed = 0; - } - } - - // 更新物理系统 - void UpdatePrimitives(float deltaTime) - { -#if UNITY_EDITOR - m_Contacts.Clear(); -#endif - - PhysicsCollisionInfo info = new PhysicsCollisionInfo(); - - // 1) 处理刚体的动力学 - for (int i = 0; i < m_Primitives.Count; ++i) - { - PhysicsPrimitive prim = m_Primitives[i]; - PhysicsBody body = prim.Body; - if (body == null) - continue; - HandleDynamics(prim, deltaTime); - } - - // 2) 处理碰撞 - for (int i = 0; i < m_Primitives.Count; ++i) - { - PhysicsPrimitive prim1 = m_Primitives[i]; - if (!prim1.IsActive) - continue; - - for (int j = i + 1; j < m_Primitives.Count; ++j) - { - PhysicsPrimitive prim2 = m_Primitives[j]; - - int minGroup = Mathf.Min((int)prim1.Group, (int)prim2.Group); - int maxGroup = Mathf.Max((int)prim1.Group, (int)prim2.Group); - int index = minGroup * (int)PhysicsGroup.GroupCount + (int)PhysicsGroup.GroupCount - maxGroup - 1; - if (m_CollisionTable[index] == 0) - continue; - - if (prim1.Label == prim2.Label) - continue; - - if (PhysicsHelper.PrimvsPrim(prim1, prim2, ref info)) - { - SolveCollision(prim1, info, deltaTime); - SolveCollision(prim2, info, deltaTime); - m_CollisionInfo.Add(info); - -#if UNITY_EDITOR - m_Contacts.Add(info.contact); -#endif - } - } - } - - // 3) 处理刚体的约束,必须在最后处理 - for (int i = 0; i < m_Primitives.Count; ++i) - { - PhysicsPrimitive prim = m_Primitives[i]; - PhysicsBody body = prim.Body; - if (body == null) - continue; - HandleConstrain(prim, deltaTime); - } - - } - - /// - /// 处理动力学 - /// - /// - /// - void HandleDynamics(PhysicsPrimitive prim, float deltaTime) - { - PhysicsBody body = prim.Body; - if (body == null) - return; - - Vector3 position = body.transform.position; - Vector3 velocity = body.Velocity; - - // 重力 - if(body.UseGravity) - { - velocity += m_Gravity * deltaTime; - } - - // 受力(冲量) - Vector3 impluse = body.Force * deltaTime; - Vector3 deltaV = impluse / body.Weight; - velocity += deltaV; - - if (prim.IsOnGround) - { - // 地面摩擦力 - if (body.Velocity.x != 0 && body.GroundFriction != 0) - { - float dv = body.GroundFriction * deltaTime; - dv = Mathf.Min(dv, Mathf.Abs(body.Velocity.x)); - dv = body.Velocity.x > 0 ? -dv : dv; - velocity.x += dv; - } - } - - if (prim.IsInAir) - { - // 空气阻力 - if(body.Velocity.x != 0 && body.AirFriction != 0) - { - float dv = body.AirFriction * deltaTime; - dv = Mathf.Min(dv, Mathf.Abs(body.Velocity.x)); - dv = body.Velocity.x > 0 ? -dv : dv; - velocity.x += dv; - } - } - - position += velocity * deltaTime; - - body.Velocity = velocity; - body.transform.position = position; - body.SetForce(Vector3.zero); - } - - /// - /// 处理物体的环境(地面、墙体)约束 - /// - /// - /// - void HandleConstrain(PhysicsPrimitive prim, float deltaTime) - { - PhysicsBody body = prim.Body; - if (body == null) - return; - - Vector3 position = body.transform.position; - Vector3 velocity = body.Velocity; - - if (prim.IsOnGround) - { - position.y = PhysicsWorld.Ground; - velocity.y = 0; - } - - body.transform.position = position; - body.Velocity = velocity; - } - - void SolveCollision(PhysicsPrimitive prim, PhysicsCollisionInfo collision, float deltaTime) - { - PhysicsPrimitive other = collision.prim1 == prim ? collision.prim2 : collision.prim1; - - // 1. 对于刚体,根据碰撞对位置进行约束 - if(prim.Body != null) - { - Vector3 pos = prim.Body.transform.position; - if(collision.size.x <= collision.size.y) - { - float offsetX = Mathf.Min(collision.size.x / 2f, 0.8f); - pos.x += prim.Position.x > collision.contact.x ? offsetX : -offsetX; - } - else - { - float offsetY = Mathf.Min(collision.size.y / 2f, 0.1f); - pos.y += prim.Position.y > collision.contact.y ? offsetY : -offsetY; - } - prim.Body.transform.position = pos; - } - - return; - } - - // prim在当前帧是否有碰撞 - public bool HasCollision(PhysicsPrimitive prim) - { - for(int i = 0; i < m_CollisionInfo.Count; ++i) - { - PhysicsCollisionInfo info = m_CollisionInfo[i]; - if(info.prim1 == prim || info.prim2 == prim) - { - return true; - } - } - return false; - } - -} \ No newline at end of file diff --git a/Assets/Scripts/Physics/PhysicsWorld.cs.meta b/Assets/Scripts/Physics/PhysicsWorld.cs.meta deleted file mode 100644 index febf844d..00000000 --- a/Assets/Scripts/Physics/PhysicsWorld.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 35e8dc39af163994ba23b57f51d397f2 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: -- cgit v1.1-26-g67d0