Enabling/Disabling body causes IndexOutOfRangeException?

Jan 24, 2012 at 5:10 PM

I have a question posted on gamedev.stackexchange.com, but I figured I would post here also.

For anyone interested, the SE question is here: http://gamedev.stackexchange.com/questions/22860/farseer-physics-indexoutofrangeexception-when-calling-world-step-after-enablin


I am making a 2D side-scroller, and I want to be able to have multiple physical bodies for the player, so that when the player is in different states (Standing, Walking, Jumping, etc), the physical body matches up with the sprite I draw.

Currently, my function to change the state and swap the physical bodies is:

new protected void SetState(object nState)
            //If mBody == null, the player is being loaded for the first time
            if (mBody == null)
                mBody = mBodies[mStates.IndexOf(nState)];
                mBody.Enabled = true;
                //Get the body for the given state
                Body nBody = mBodies[mStates.IndexOf(nState)];

                //Enable the new body
                nBody.Enabled = true;

                //Disable the current body
                mBody.Enabled = false;

                //Copy the current body's attributes to the new one
                nBody.SetTransform(mBody.Position, mBody.Rotation);
                nBody.LinearVelocity = mBody.LinearVelocity;
                nBody.AngularVelocity = mBody.AngularVelocity;

                mBody = nBody;


This is related to Setting .Enabled on the bodies, because if I remove that, everything else works. But when setting .Enabled, the next call to World.Step fails with an IndexOutOfRangeException.


Any ideas? Is there a more proper way to disable the bodies?