/// <summary>
        /// Destroy a rigid body.
        /// Warning: This function is locked during callbacks.
        /// Warning: This automatically deletes all associated shapes and joints.
        /// </summary>
        /// <param name="body">The body.</param>
        public void RemoveBody(Body body)
        {
            Debug.Assert(BodyList.Count > 0);
            Debug.Assert(!IsLocked);

            if (IsLocked)
            {
                return;
            }

            // You tried to remove a body that is not contained in the BodyList.
            // Are you removing the body more than once?
            Debug.Assert(BodyList.Contains(body));

            // Delete the attached joints.
            JointEdge je = body.JointList;

            while (je != null)
            {
                JointEdge je0 = je;
                je = je.Next;

                RemoveJoint(je0.Joint);
            }
            body.JointList = null;

            // Delete the attached contacts.
            ContactEdge ce = body.ContactList;

            while (ce != null)
            {
                ContactEdge ce0 = ce;
                ce = ce.Next;
                ContactManager.Destroy(ce0.Contact);
            }
            body.ContactList = null;

            // Delete the attached fixtures. This destroys broad-phase proxies.

            foreach (Fixture fixture in body.FixtureList)
            {
                fixture.DestroyProxies(ContactManager.BroadPhase);
                fixture.Destroy();
            }

            body.FixtureList = null;

            // Remove world body list.
            BodyList.Remove(body);

            if (BodyRemoved != null)
            {
                BodyRemoved(body);
            }
        }
Example #2
0
        private void ProcessRemovedBodies()
        {
            if (_bodyRemoveList.Count > 0)
            {
                foreach (Body body in _bodyRemoveList)
                {
                    Debug.Assert(BodyList.Count > 0);

                    // You tried to remove a body that is not contained in the BodyList.
                    // Are you removing the body more than once?
                    //Debug.Assert(BodyList.Contains(body));
                    if (!BodyList.Contains(body))
                    {
                        continue;
                    }

                    // Delete the attached joints.
                    foreach (Joint j in body.JointList)
                    {
                        RemoveJoint(j, false);
                    }
                    body.JointList.Clear();

                    // Delete the attached contacts.
                    ContactEdge ce = body.ContactList;
                    while (ce != null)
                    {
                        ContactEdge ce0 = ce;
                        ce = ce.Next;
                        ContactManager.Destroy(ce0.Contact);
                    }
                    body.ContactList = null;

                    // Delete the attached fixtures. This destroys broad-phase proxies.
                    for (int i = 0; i < body.FixtureList.Count; i++)
                    {
                        body.FixtureList[i].DestroyProxies(ContactManager.BroadPhase);
                        body.FixtureList[i].Destroy();
                    }

                    body.FixtureList = null;

                    // Remove world body list.
                    BodyList.Remove(body);

                    if (BodyRemoved != null)
                    {
                        BodyRemoved(body);
                    }
                }

                _bodyRemoveList.Clear();
            }
        }