private BreakableBody(World world) { World = world; World.ContactManager.PostSolve += PostSolve; State = BreakableBodyState.Unbroken; }
private void Decompose() { if (State == BreakableBodyState.Broken) { throw new InvalidOperationException("BreakableBody is allready broken"); } //Unsubsribe from the PostSolve delegate World.ContactManager.PostSolve -= PostSolve; for (int i = 0; i < Parts.Count; i++) { Fixture oldFixture = Parts[i]; Shape shape = oldFixture.Shape.Clone(); object fixtureTag = oldFixture.Tag; MainBody.Remove(oldFixture); Body body = World.CreateBody(MainBody.Position, MainBody.Rotation, BodyType.Dynamic); body.Tag = MainBody.Tag; Fixture newFixture = body.CreateFixture(shape); newFixture.Tag = fixtureTag; Parts[i] = newFixture; body.AngularVelocity = _angularVelocitiesCache[i]; body.LinearVelocity = _velocitiesCache[i]; } World.Remove(MainBody); State = BreakableBodyState.Broken; }
private void PostSolve(Contact contact, ContactVelocityConstraint impulse) { if (State != BreakableBodyState.Broken) { if (Parts.Contains(contact.FixtureA) || Parts.Contains(contact.FixtureB)) { float maxImpulse = 0.0f; int count = contact.Manifold.PointCount; for (int i = 0; i < count; ++i) { maxImpulse = Math.Max(maxImpulse, impulse.points[i].normalImpulse); } if (maxImpulse > Strength) { // Flag the body for breaking. State = BreakableBodyState.ShouldBreak; } } } }