private static void SimulateCollision(HashMap container) { container.Rebuild(); container.ForEachVisible(object1 => { if (object1.RigidBody.Motion != MotionEnum.Dynamic) { return; } container.Find(object1.RigidBody.Bound, objectsNearby); objectsNearby.Consume(object2 => { if (object1 != object2 && !objectsChecked.Contains(object2)) { RigidBody.DetectCollide(object1, object2, collisions); } }); objectsChecked.Add(object1); }); objectsChecked.Clear(); collisions.ForEach(collision => { CollisionHandler.PreCollide(collision.object1, collision.object2); CollisionHandler.PreCollide(collision.object2, collision.object1); }); collisions.ForEach(collision => { RigidBody rigidbody1 = collision.object1.RigidBody, rigidbody2 = collision.object2.RigidBody; if (!rigidbody1.CollisionLayerMask.HasOne(rigidbody2.CollisionLayerMask)) { return; } (var velocityfix1, var velocityfix2) = RigidBody.ResolveCollide(collision); velocityFix.AddIfNotExistStruct(rigidbody1, Vector3.Zero); velocityFix[rigidbody1] += new Vector3(velocityfix1, 1); velocityFix.AddIfNotExistStruct(rigidbody2, Vector3.Zero); velocityFix[rigidbody2] += new Vector3(velocityfix2, 1); locationFix.AddIfNotExistStruct(rigidbody1, Vector3.Zero); var f1 = collision.side.Select(velocityfix1.DivS(velocityfix1.Abs() + velocityfix2.Abs()) * collision.depth); locationFix[rigidbody1] += new Vector3(f1, 1); locationFix.AddIfNotExistStruct(rigidbody2, Vector3.Zero); var f2 = collision.side.Select(velocityfix2.DivS(velocityfix1.Abs() + velocityfix2.Abs()) * collision.depth); locationFix[rigidbody2] += new Vector3(f2, 1); }); locationFix.Consume((rigidbody, dp) => { dp /= dp.Z; rigidbody.Object.Location += new Vector2(dp.X, dp.Y); }); velocityFix.Consume((rigidbody, dv) => { dv /= dv.Z; rigidbody.Velocity += new Vector2(dv.X, dv.Y); }); collisions.Consume(collision => { CollisionHandler.PostCollide(collision.object1, collision.object2, collision.side); CollisionHandler.PostCollide(collision.object2, collision.object1, collision.side.Invert()); }); }