From 68f3823ddb10110c2abafb5f1aab2f3e6f3fa360 Mon Sep 17 00:00:00 2001 From: chai Date: Fri, 16 Oct 2020 08:09:25 +0800 Subject: * ability system * physics system --- Assets/Scripts/Physics/PhysicsWorld.cs | 62 ++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 14 deletions(-) (limited to 'Assets/Scripts/Physics/PhysicsWorld.cs') diff --git a/Assets/Scripts/Physics/PhysicsWorld.cs b/Assets/Scripts/Physics/PhysicsWorld.cs index 5ef747b2..3514934c 100644 --- a/Assets/Scripts/Physics/PhysicsWorld.cs +++ b/Assets/Scripts/Physics/PhysicsWorld.cs @@ -1,16 +1,7 @@ -using System.Collections; + using System.Collections; using System.Collections.Generic; using UnityEngine; -/// -/// primitive标记 -/// -public enum PhysicsTag -{ - Player = 1, // 从属于玩家 - Oponent = 1 << 1, // 从属于对手 -} - /// /// primitive分类 /// @@ -20,17 +11,36 @@ public enum PhysicsGroup Prop, // 物体 Ground, // 地面 Wall, // 墙面 + + GroupCount, +} + +/// +/// primitive标记 +/// +public enum PhysicsTag +{ + Player = 1, // 从属于玩家 + Oponent = 1 << 1, // 从属于对手 } public class PhysicsWorld : Singleton { - private int m_UpdateRate = 30; + private int m_UpdateRate = 60; // 重力加速度 private readonly Vector3 m_Gravity = new Vector3(0, -9.8f, 0); // 当前管理的碰撞体 private List m_Primitives; private float m_TimeCount; + private readonly int[] m_CollisionTable = { + // wall ground prop character +/*character*/ 1, 1, 1, 1, +/*prop */ 1, 1, 0, 0, +/*ground */ 0, 0, 0, 0, +/*wall */ 0, 0, 0, 0, + }; + public void Init() { m_Primitives = new List(); @@ -74,9 +84,33 @@ public class PhysicsWorld : Singleton private void Tick() { - float deltaTime = 1f / m_UpdateRate; - //Debug.Log("Physics Tick"); + float deltaTime = 1f / m_UpdateRate; + int groupCount = (int)PhysicsGroup.GroupCount; + for (int i = 0; i < m_Primitives.Count; ++i) + { + PhysicsPrimitive prim1 = m_Primitives[i]; + for(int j = i + 1; j < m_Primitives.Count; ++j) + { + PhysicsPrimitive prim2 = m_Primitives[j]; + + // check collision by group + int minType = Mathf.Min((int)prim1.Type, (int)prim2.Type); + int maxType = Mathf.Max((int)prim1.Type, (int)prim2.Type); + if (m_CollisionTable[minType * groupCount + groupCount - maxType - 1] == 0) + continue; + + // check collision by label + if (prim1.Label == prim2.Label) + continue; + PhysicsCollisionInfo info; + if(PhysicsHelper.PrimvsPrim(prim1, prim2, out info)) + { + //没有physics body的primitive将不会被移动,只有那些绑定了physics body的会被施加物理效果,比如角色身体、物品 + + } + } + } } -} +} \ No newline at end of file -- cgit v1.1-26-g67d0