This project has moved. For the latest updates, please go here.

Simulate player carrying an object

Jan 9, 2012 at 7:35 AM

I was wondering if theres any examples of people using "Press E to pick up object" then attaching that object to the player until E is pressed again.

I've got a few ideas of how to go about it such as setting a bool to true OnCollision, false on seperation, then while it's true having a fixed distance joint between the player and the square's body until E is pressed again. I'm unsure how i'd do this with my current class structure though but any other ways of doing this would be appreciated!

Jan 15, 2012 at 12:06 AM

I've accomplished this recently by the use of a WeldJoint


here is a snippet for attaching and detaching the object to the player:


        public Fixture attachmentFixture;
        public FarseerPhysics.Dynamics.Joints.WeldJoint weldJoint;
        public FixedAngleJoint joint;

        public void AttachTo(Fixture fixture, World world)
            if (attachmentFixture != null)
                joint = JointFactory.CreateFixedAngleJoint(world, attachmentFixture.Body);                
                weldJoint = JointFactory.CreateWeldJoint(world, fixture.Body, attachmentFixture.Body, Vector2.Zero);
                weldJoint.CollideConnected = false;
                joint.TargetAngle = attachmentFixture.Body.Rotation;
                joint.Softness = .85f;//makes the joint soft so that it sways with the inertia                

        public void Detach(World world)
            if (weldJoint != null)
                weldJoint = null;
                joint = null;                

Feb 7, 2012 at 9:13 PM

Thanks alot! Much appreciated.

Only just realised some one had replied to this.