diff options
author | chai <chaifix@163.com> | 2022-03-10 14:07:40 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2022-03-10 14:07:40 +0800 |
commit | 22891bf59032ba88262824255a706d652031384b (patch) | |
tree | 7595439ba9966c9402d37e37cee5e8cf098757d5 /Assets/Scripts/Physics/PhysicsWorld.cs | |
parent | 8b04ea73e540067f83870b61d89db4868fea5e8a (diff) |
* move folder
Diffstat (limited to 'Assets/Scripts/Physics/PhysicsWorld.cs')
-rw-r--r-- | Assets/Scripts/Physics/PhysicsWorld.cs | 356 |
1 files changed, 0 insertions, 356 deletions
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;
-
-/// <summary>
-/// primitive分类
-/// </summary>
-public enum PhysicsGroup
-{
- Character, // 角色
- Prop, // 物体
- Ground, // 地面
- Wall, // 墙面
-
- HitBox, // hitbox
- HurtBox, // hurtbox
-
- GroupCount,
-}
-
-/// <summary>
-/// primitive标记
-/// </summary>
-public enum PhysicsTag
-{
- Null = 0,
- Player = 1, // 从属于玩家
- Oponent = 1 << 1, // 从属于对手
-}
-
-/// <summary>
-/// 物理子系统
-/// </summary>
-public class PhysicsWorld : Singleton<PhysicsWorld>
-{
- 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<PhysicsPrimitive> m_Primitives = new List<PhysicsPrimitive>();
- 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<Animator> m_Animators = new List<Animator>();
-#if UNITY_EDITOR
- private List<Vector3> m_Contacts = new List<Vector3>();
-#endif
-
- private List<PhysicsCollisionInfo> m_CollisionInfo = new List<PhysicsCollisionInfo>();
-
- public const float Ground = 0.1f;
-
- // 所有碰撞
- public List<PhysicsCollisionInfo> 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);
- }
-
- /// <summary>
- /// 物理系统以稳定的逻辑帧率执行
- /// </summary>
- 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);
- }
-
- }
-
- /// <summary>
- /// 处理动力学
- /// </summary>
- /// <param name="prim"></param>
- /// <param name="deltaTime"></param>
- 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);
- }
-
- /// <summary>
- /// 处理物体的环境(地面、墙体)约束
- /// </summary>
- /// <param name="prim"></param>
- /// <param name="deltaTime"></param>
- 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 |