diff options
Diffstat (limited to 'Assets/Scripts/Physics')
-rw-r--r-- | Assets/Scripts/Physics/PhysicsBody.cs | 25 | ||||
-rw-r--r-- | Assets/Scripts/Physics/PhysicsHelper.cs | 2 | ||||
-rw-r--r-- | Assets/Scripts/Physics/PhysicsPrimitive.cs | 8 | ||||
-rw-r--r-- | Assets/Scripts/Physics/PhysicsWorld.cs | 50 |
4 files changed, 69 insertions, 16 deletions
diff --git a/Assets/Scripts/Physics/PhysicsBody.cs b/Assets/Scripts/Physics/PhysicsBody.cs index 4077696a..68f9dec3 100644 --- a/Assets/Scripts/Physics/PhysicsBody.cs +++ b/Assets/Scripts/Physics/PhysicsBody.cs @@ -112,17 +112,24 @@ public sealed class PhysicsBody : MonoBehaviour get { return m_Frication; }
}
- //[Tooltip("空气摩擦力")]
- //[SerializeField]
- //private Vector3 m_AirFriction;
- //public Vector3 AirFriction
- //{
- // get { return m_AirFriction; }
- //}
-
- [Tooltip("力")]
+ [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;
diff --git a/Assets/Scripts/Physics/PhysicsHelper.cs b/Assets/Scripts/Physics/PhysicsHelper.cs index 821241e3..44bf513e 100644 --- a/Assets/Scripts/Physics/PhysicsHelper.cs +++ b/Assets/Scripts/Physics/PhysicsHelper.cs @@ -19,6 +19,7 @@ public struct PhysicsCollisionInfo public PhysicsPrimitive prim1;
public PhysicsPrimitive prim2;
public Vector3 contact;
+ public Vector3 size;
}
public sealed class PhysicsHelper
@@ -63,6 +64,7 @@ public sealed class PhysicsHelper 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;
}
diff --git a/Assets/Scripts/Physics/PhysicsPrimitive.cs b/Assets/Scripts/Physics/PhysicsPrimitive.cs index 26fb5aa9..c94aee1a 100644 --- a/Assets/Scripts/Physics/PhysicsPrimitive.cs +++ b/Assets/Scripts/Physics/PhysicsPrimitive.cs @@ -185,4 +185,12 @@ public abstract class PhysicsPrimitive : MonoBehaviour }
}
+ public bool IsInAir
+ {
+ get
+ {
+ return Bound.w > PhysicsWorld.Ground;
+ }
+ }
+
}
\ No newline at end of file diff --git a/Assets/Scripts/Physics/PhysicsWorld.cs b/Assets/Scripts/Physics/PhysicsWorld.cs index 41ea59f8..ba9b1bc1 100644 --- a/Assets/Scripts/Physics/PhysicsWorld.cs +++ b/Assets/Scripts/Physics/PhysicsWorld.cs @@ -43,7 +43,7 @@ public class PhysicsWorld : Singleton<PhysicsWorld> private const int _ = 0;
private readonly int[] m_CollisionTable = {
// hurtbox hitBox wall ground prop character
-/*character*/ 0, 0, 1, 1, 1, 0,
+/*character*/ 0, 0, 1, 1, 1, 1,
/*prop */ 0, 0, 1, 1, 0, _,
/*ground */ 0, 0, 0, 0, _, _,
/*wall */ 0, 0, 0, _, _, _,
@@ -59,6 +59,15 @@ public class PhysicsWorld : Singleton<PhysicsWorld> public const float Ground = 0.1f;
+ // 所有碰撞
+ public List<PhysicsCollisionInfo> Collisions
+ {
+ get
+ {
+ return m_CollisionInfo;
+ }
+ }
+
public void Init()
{
m_TimeCount = Time.time;
@@ -184,13 +193,13 @@ public class PhysicsWorld : Singleton<PhysicsWorld> {
PhysicsPrimitive prim2 = m_Primitives[j];
- // check collision by group
+ // 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;
- // check collision by label
+ // label
if (prim1.Label == prim2.Label)
continue;
@@ -220,11 +229,17 @@ public class PhysicsWorld : Singleton<PhysicsWorld> velocity += m_Gravity * dt;
body.Velocity = velocity;
- position += velocity * dt;
+ // impluse = d(mv) = m*dv
+ Vector3 impluse = body.Force * dt;
+ Vector3 deltaV = impluse / body.Weight;
+ velocity += deltaV;
+ body.Velocity = velocity;
- body.transform.position = position;
+ position += velocity * dt;
+
+ body.transform.position = position;
- if(prim.IsOnGround)
+ if (prim.IsOnGround)
{
// pos=0, Vy=0
position.y = 0.1f;
@@ -241,11 +256,32 @@ public class PhysicsWorld : Singleton<PhysicsWorld> 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)
{
- return;
+ 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)
|