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
{
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();
private List m_Contacts = new List();
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()
{
BeforeUpdate();
float preTime = m_TimeCount;
m_TimeCount = Time.time;
float dt = m_TimeCount - preTime;
while (dt > 1f / m_UpdateRate)
{
Tick();
dt -= 1f / m_UpdateRate;
}
m_TimeCount -= dt;
AfterUpdate();
}
// 更新之前
private void BeforeUpdate()
{
m_CollisionInfo.Clear();
}
// 更新之后
private void AfterUpdate()
{
}
public void DrawGizmos()
{
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);
}
}
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)
{
m_Contacts.Clear();
PhysicsCollisionInfo info = new PhysicsCollisionInfo();
// 处理动力学
for(int i = 0; i < m_Primitives.Count; ++i)
{
PhysicsPrimitive prim = m_Primitives[i];
HandleDynamics(prim, deltaTime);
}
// 处理碰撞
int groupCount = (int)PhysicsGroup.GroupCount;
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];
// group
int minGroup = Mathf.Min((int)prim1.Group, (int)prim2.Group);
int maxGroup = Mathf.Max((int)prim1.Group, (int)prim2.Group);
if (m_CollisionTable[minGroup * groupCount + groupCount - maxGroup - 1] == 0)
continue;
// label
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);
m_Contacts.Add(info.contact);
}
}
}
}
//没有physics body的primitive将不会被移动,只有那些绑定了physics body的会被施加物理效果,比如角色身体、物品
void HandleDynamics(PhysicsPrimitive prim, float dt)
{
PhysicsBody body = prim.Body;
if (body == null)
return;
if (!body.UseGravity)
return;
Vector3 position = body.transform.position;
Vector3 velocity = body.Velocity;
velocity += m_Gravity * dt;
body.Velocity = velocity;
// impluse = d(mv) = m*dv
Vector3 impluse = body.Force * dt;
Vector3 deltaV = impluse / body.Weight;
velocity += deltaV;
body.Velocity = velocity;
position += velocity * dt;
body.transform.position = position;
if (prim.IsOnGround)
{
// pos=0, Vy=0
position.y = 0.1f;
body.transform.position = position;
// 地面摩擦力
if(body.Velocity.x != 0 && body.GroundFriction != 0)
{
float dv = body.GroundFriction * dt;
dv = Mathf.Min(dv, Mathf.Abs(body.Velocity.x));
dv = body.Velocity.x > 0 ? -dv : dv;
velocity.x += dv;
}
velocity.y = 0;
body.Velocity = velocity;
}
else if(prim.IsInAir) // 空气摩擦力
{
if(body.Velocity.x != 0 && body.AirFriction != 0)
{
float dv = body.AirFriction * dt;
dv = Mathf.Min(dv, Mathf.Abs(body.Velocity.x));
dv = body.Velocity.x > 0 ? -dv : dv;
velocity.x += dv;
body.Velocity = velocity;
}
}
body.SetForce(Vector3.zero);
}
void SolveCollision(PhysicsPrimitive prim, PhysicsCollisionInfo collision, float dt)
{
PhysicsPrimitive other = collision.prim1 == prim ? collision.prim2 : collision.prim1;
if(prim.Body != null && other.Body != null)
{
Vector3 pos = prim.Body.transform.position;
pos.x += pos.x > collision.contact.x ? collision.size.x / 2f : - collision.size.x / 2f;
prim.Body.transform.position = pos;
}
return;
PhysicsBody body = prim.Body;
if (body == null)
return;
if (!body.UseGravity)
return;
Vector3 contact = collision.contact;
Vector3 dir = (prim.Position - contact).normalized;
dir.z = 0;
float mag = Mathf.Max(0.3f, body.Velocity.magnitude);
Vector3 position = body.transform.position;
position += mag * dir * dt;
body.Velocity = Vector3.zero;
body.transform.position = position;
}
// 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;
}
}