Mouse Picking

Aug 5, 2011 at 7:02 PM

How would I implement mouse picking into farseer? I am using farseer to handle 100% of collisions and images in my application, and I need to use mouse picking for interaction.

Aug 5, 2011 at 9:15 PM

hi

this is my implementation for Mouse Joint

 

       private FixedMouseJoint fixedMouseJoint;


       public void MouseJointHandler()
        {
            Vector2 position = new Vector2(Mouse.GetState().X / MeterInPixel, Mouse.GetState().Y / MeterInPixel);
            if (Mouse.GetState().LeftButton == ButtonState.Pressed && fixedMouseJoint == null)
            {
                fixture = world.TestPoint(position);
                if (fixture != null)
                {
                    Body body = fixture.Body;
                    fixedMouseJoint = new FixedMouseJoint(body, position);
                    fixedMouseJoint.MaxForce = 1000.0f * body.Mass;
                    world.AddJoint(fixedMouseJoint);
                    body.Awake = true;

                }
            }

            if (Mouse.GetState().LeftButton == ButtonState.Released && fixedMouseJoint != null)
            {
                world.RemoveJoint(fixedMouseJoint);
                fixedMouseJoint = null;
            }

            if (fixedMouseJoint != null)
            {
                fixedMouseJoint.WorldAnchorB = position;
            }
        }
Aug 5, 2011 at 9:39 PM

Fantastic.

Curious, if I try to draw something out of the viewport, does XNA automatically cull it away or do I have to do a manual viewport test??

Aug 6, 2011 at 3:07 PM

I recommend reading "Learning XNA 4.0" its good for understanding all the XNA things :)

Aug 7, 2011 at 7:04 PM
Edited Aug 7, 2011 at 7:05 PM

This does not work for me somehow. It might be because of my positioning.

I use ConvertUnits and world.TestPointAll:

 

List<Fixture> fixtures = world.TestPointAll(ConvertUnits.ToSimUnits(mouseXY));
            foreach (Fixture element in fixtures)
            {
                System.Windows.Forms.MessageBox.Show("hit");
                ((tempClass)element.UserData).MousePress(mouse);
            }

I do a messagebox.show to test my object's position, and it looks fine.
Aug 8, 2011 at 1:11 AM

ok good,

but I didn't tell you that the variable MeterInPixel for me 64 int but int the XNA samples they use 100 int

and just put this function in Update method

MouseJointHandler();