FixedJointObject follows Mouse

Topics: Developer Forum, User Forum
Feb 15, 2011 at 12:32 PM

Hi Guys,

I am fairly new to Farseer and Box2D, I have a few issues regarding an object which would have a fixed joint to the mouse control. I have looked all around the forum but I have found nothing about this.

So far I have understood the how the click works so I managed to change the code to this so when I click on an object, the object will then follow the mouse and right click will cancel the action:

        private void Mouse(InputHelper state)
        {
            Vector2 position = Camera2D.ConvertScreenToWorld(state.MousePosition);

            if (state.IsNewButtonPress(MouseButtons.RightButton))
            {
                MouseUp();
            }
            else if (state.IsNewButtonPress(MouseButtons.LeftButton))
            {
                MouseDown(position);
            }

            if (_fixedMouseJoint != null)
            {
                _fixedMouseJoint.WorldAnchorB = position;
            }
        }

        public void MouseDown(Vector2 p)
        {
            if (_fixedMouseJoint != null)
            {
                return;
            }

            Fixture savedFixture = World.TestPoint(p);

            if (savedFixture != null)
            {

                Body body = savedFixture.Body;
                _fixedMouseJoint = new FixedMouseJoint(body, p);
                _fixedMouseJoint.MaxForce = 1000.0f * body.Mass;
                World.AddJoint(_fixedMouseJoint);
                body.Awake = true;
            }
        }

        private void MouseUp()
        {
            if (_fixedMouseJoint != null)
            {
                World.RemoveJoint(_fixedMouseJoint);
                _fixedMouseJoint = null;
            }
        }

Now the question is...

I am following the code in Demo1Screen.cs in the SimpleExamples 3.2 and what I want to do is that as soon as I access the game, the joint is already created between the object and the mouse (so I am not required to click on the rectangle in this case). I tried replicating the code for when the mouse is down but I get nothing.

Also is it possible to do a fixture in which the object always follows the mouse without any lag?

Thank you,

Roger

Developer
Feb 23, 2011 at 8:32 AM

If you want the body to be attached from the start you have to move it to the mouse position (by setting its position directly) and then creating a mouse joint like this:

_fixedMouseJoint = new FixedMouseJoint(the_body, mouse_position);

If you want an object to follow your mouse without lag create a kinematic body and set its position manually to the mousecursor on each frame. Keep in mind though that kinematic bodies do not collide with other kinematic or static bodies but only with dynamic ones, so you might have to work around that probably.

Manipulating position and rotation on a dynamic body directly is not a good idea, cause you kind of force them to not behave physically correct by doing that and it makes Farseer very very sad :/