Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.