/// <summary> /// Perform the necessary checks for collision between objects /// </summary> /// <param name="tree"></param> public static void UpdateCollision(QuadTree <IQuadtreeObject> tree) { // Insert objects into the tree foreach (IQuadtreeObject obj in AllObjects) { tree.Insert(obj); } // The centre of the query range // Currently will be just the size of the screen Vector2 centre = new Vector2(tree.Boundary.centre.x, tree.Boundary.centre.y); // The half size of the query range Vector2 size = centre; AABB range = new AABB(centre, size); // Getting the objects that are in range List <IQuadtreeObject> objects = tree.QueryRange(range); bool hasCollided = false; foreach (IQuadtreeObject obj in objects) { foreach (IQuadtreeObject quadObj in objects) { if (obj != quadObj) { hasCollided = SAT.IsColliding(obj.collider, quadObj.collider); if (hasCollided && !obj.collider.isColliding && !quadObj.collider.isColliding) { // Update the velocity after collision // Combined masses double masses = quadObj.Mass + obj.Mass; Vector2[] velocities = OnCollision(obj, quadObj); obj.velocity = velocities[0]; quadObj.velocity = velocities[1]; obj.collider.isColliding = true; quadObj.collider.isColliding = true; if (DebugTools.PrintCollisionVelocities) { // Print out the name of the class, the name of the objects and the velocity of each object Console.WriteLine("(1) Object Manager - Name: " + obj.Name + "\t Velocity: " + obj.velocity.ToString()); Console.WriteLine("(2) Object Manager - Name: " + quadObj.Name + "\t Velocity: " + quadObj.velocity.ToString()); } } else { obj.collider.isColliding = false; quadObj.collider.isColliding = false; } } } } // Once every collision has been calculated, the quad is cleared. tree.ClearQuad(); }