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/PhysicsWorld.cs | 356 --------------------------------- 1 file changed, 356 deletions(-) delete mode 100644 Assets/Scripts/Physics/PhysicsWorld.cs (limited to 'Assets/Scripts/Physics/PhysicsWorld.cs') 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 -- cgit v1.1-26-g67d0