Last active
August 29, 2015 14:16
-
-
Save Zerophase/fa6d3241ecb2cf67ac0f to your computer and use it in GitHub Desktop.
Physics Director
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class PhysicsDirector : Director | |
| { | |
| private List<MovablePhysicsMediator> movablePhysicsMediators = new List<MovablePhysicsMediator>(); | |
| private List<Ground> grounds = new List<Ground>(); | |
| private Vector3 gravity = new Vector3(0f, -30f, 0f); | |
| private AABBIntersection aabbIntersection = new AABBIntersection(); | |
| private AABB3D playerBoundingBox; | |
| private AABB3D groundBoundingBox; | |
| private Vector3 velocity = new Vector3(0.0f, 0.0f, 0.0f); | |
| private Sweep sweep = new Sweep(); | |
| void Update() | |
| { | |
| GroundCollision(); | |
| } | |
| private void GroundCollision() | |
| { | |
| float hitTime = 0f; | |
| for (int k = 0; k < movablePhysicsMediators.Count; k++) | |
| { | |
| movablePhysicsMediators[k].UpdateVelocity(gravity); | |
| playerBoundingBox = movablePhysicsMediators[k].BoundingBox; | |
| for (int i = 0; i < grounds.Count; i++) | |
| { | |
| groundBoundingBox = grounds[i].BoundingBox; | |
| if (sweep.TestMovingAABB(playerBoundingBox, | |
| playerBoundingBox.Velocity * Time.deltaTime, 0f, 1f, | |
| groundBoundingBox, ref hitTime)) | |
| { | |
| float actualHittime = 1.0f - hitTime; | |
| if (playerBoundingBox.NormalCollision[0].x > 0.0f) | |
| { | |
| velocity.x = playerBoundingBox.Velocity.x; | |
| velocity.y = 0.0f; | |
| velocity.z = 0.0f; | |
| movablePhysicsMediators[k].UpdateVelocity(-velocity * actualHittime); | |
| } | |
| else if (playerBoundingBox.NormalCollision[0].x < 0.0f) | |
| { | |
| velocity.x = playerBoundingBox.Velocity.x; | |
| velocity.y = 0.0f; | |
| velocity.z = 0.0f; | |
| movablePhysicsMediators[k].UpdateVelocity(-velocity * actualHittime); | |
| } | |
| if (playerBoundingBox.NormalCollision[1].y < 0.0f && playerBoundingBox.Velocity.y < 0.0f) | |
| { | |
| velocity.x = 0.0f; | |
| velocity.y = playerBoundingBox.Velocity.y; | |
| velocity.z = 0.0f; | |
| movablePhysicsMediators[k].UpdateVelocity(-velocity * actualHittime); | |
| } | |
| } | |
| } | |
| movablePhysicsMediators[k].UpdatePosition(); | |
| movablePhysicsMediators[k].ResetVelocity(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment