Reusing joints

Topics: User Forum
Mar 25, 2011 at 2:57 AM

In my quest for removing allocations, I have encountered an issue when attempting to reuse a joint. I am using a revolute joint to attach the player's body to walls so achieve crawling. My problem comes when attempting to transition from crawling on one body to another.

Ideally I would do something like:

              // update joint
              m_grabJoint.BodyB = closestFixture.Body;
              m_grabJoint.LocalAnchorA = body.Body.GetLocalPoint(body.Body.Position + direction);
              m_grabJoint.LocalAnchorB = closestFixture.Body.GetLocalPoint(body.Body.Position + direction * length);

Instead I have to do this:

            // change joint bodies if necessary
            if (m_grabJoint.BodyB == closestFixture.Body)
            {
              // closest fixture still belongs to the same body, update anchors
              m_grabJoint.LocalAnchorA = body.Body.GetLocalPoint(body.Body.Position + direction);
              m_grabJoint.LocalAnchorB = closestFixture.Body.GetLocalPoint(body.Body.Position + direction * length);
            }
            else
            {
              // closest fixture belongs to a different body, recreate joint
              PhysicsWorld world = Game.Singleton.Instance.GameWorld.PhysicsWorld;
              world.World.RemoveJoint(m_grabJoint);
              m_grabJoint = new RevoluteJoint(body.Body, closestFixture.Body, body.Body.GetLocalPoint(body.Body.Position + direction), closestFixture.Body.GetLocalPoint(body.Body.Position + direction * length));
              m_grabJoint.CollideConnected = true;
              world.World.AddJoint(m_grabJoint);
            }

I guess I could live with needing to do RemoveJoint/AddJoint as well. Anyway, the issue seems to be with JointEdges that are created when the joint is added to the world. If I change BodyB, these edges don't appear to be touched. A single frame Remove/Add is incorrect also, as the world batches these operations and performs them in the order Add/Remove.

Is anyone able to provide any information on potential fixes/workarounds for this? I had thought to have 2 joints for each joint I plan to re-use and double-buffer them (so the Remove/Add would occur in the correct order and the edges would be resolved correctly). Obviously this isn't an ideal situation, but as last resort it should serve.

Thanks :)

Coordinator
Mar 25, 2011 at 12:06 PM

Please create an issue in the issue tracker.

Mar 26, 2011 at 3:02 AM

Ok, done :)

http://farseerphysics.codeplex.com/workitem/30416