Skip to content

Instantly share code, notes, and snippets.

@Zerophase
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save Zerophase/fa6d3241ecb2cf67ac0f to your computer and use it in GitHub Desktop.

Select an option

Save Zerophase/fa6d3241ecb2cf67ac0f to your computer and use it in GitHub Desktop.
Physics Director
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