Created
July 22, 2015 14:33
-
-
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.
This file contains 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 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