Skip to content

Instantly share code, notes, and snippets.

@izackp
Created July 22, 2015 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save izackp/67949de525305796244a to your computer and use it in GitHub Desktop.
Save izackp/67949de525305796244a to your computer and use it in GitHub Desktop.
Example of moving objects and checking for collisions. However, it can be cleaned up a bit, and it does not do any stepping!! meaning instances jump from point a to point b. So fast objects such a bullets may jump past walls.
public class CollisionUpdater
{
List<IPhysics> components;
public CollisionUpdater ()
{
components = new List<IPhysics> ();
}
public void AddComponent (IPhysics newComp)
{
components.Add (newComp);
}
public void RemoveComponent (IPhysics oldComp)
{
components.Remove (oldComp);
}
public void ReactCollisions ()
{
for (int i = 0; i < components.Count; i++) {
IPhysics objA = components [i];
objA.Move ();
if (i < components.Count - 1)
FindCollisionsWith (objA, i + 1);
}
}
public void FindCollisionsWith (IPhysics objA, int fromIndex)
{
for (int j = fromIndex; j < components.Count; j++) {
PhysicsComponent objB = components [j];
if (objA.frame.Intersects (objB.frame)) {
if (objA.ShouldIgnoreCollision (objB))
continue;
if (objB.ShouldIgnoreCollision (objA))
continue;
objA.Collision (objB);
objB.Collision (objA);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment