This project has moved and is read-only. For the latest updates, please go here.

Best way to handle objects with the mouse ?

May 21, 2009 at 10:24 PM


I´ve been working on a game that has the objective of teaching simple Math Concepts to kids of an age range from 4-6 years old.

All of the gameplay is made using the mouse, to drag objects  with different weights and sizes, and drop them on some platforms, to achieve this i´m using a FixedLinearSpring just like in the Samples.

The thing is that it seems a little hard to controle the objects, and i think that the kids could have some problem with this, in the next week i´m going to make some tests with some children, but i was wondering if there´s other way to controle objects and in a more easy way.


May 22, 2009 at 3:16 AM
Edited May 22, 2009 at 3:18 AM

Hi David

One thing you could do is disable the physics and collisions for the object when you pick it up, and then reenable it again when you release the object. This will allow you to drag the object across anything lying in its path without it colliding, which might make it an easier concept for children.

The only problem with this is if they release the object half over another object, which could cause some trouble.

The only other thing is using a Joint instead of a spring. This will make the object stick really tightly to the mouse, no lagging behind like the spring does. If you want to stop the object spinninng when its being held you could use an angle joint.

Itl probably take a bit of tweaking to get it to feel right, especialy for kids.

Anyway good luck and hope this helps.

May 29, 2009 at 5:24 PM

Hi, thanks for the reply robertdodd,

So, today was the day that the kids tested the application, and it was very difficult for some of the kids to play the game, only the ones with 6 years managed to solve some of the problems.

I need to make the game more user friendly, so what are the options ?

  • I´ve tried a FixedRevoluteJoint, but it seems that the object gets stationary and only rotates, not following the mouse cursor.
  • For a RevoluteJoint, there is a need for a second Body, which is none...
  • Updating the Geom Position manually is not a good thing.

If someone more experienced with the engine than me, could give me some help it would be awesome!



May 29, 2009 at 6:40 PM

We are looking into this in the next version of Farseer Physics. I think that robertdodd outlined your current options. Currently we don't have anything built in the physics engine to move geometries with physics enabled.

May 30, 2009 at 1:44 AM
Edited May 30, 2009 at 1:49 AM

FOr the fixed revolute joint are you updating the anchor?

Sorry if you are doing this but it seems like this could be an easy to miss little thing,

have it like this

///This might be easier on the eyes

//Declare a revolute joint

FixedRevoluteJoint MouseHandle;

//Then in some handleMouseInput method or just in your Update Method do these mouse checks

if(mouseState.Buttons.Left == buttonstate.pressed && LastmouseState.Buttons.Left == buttonstate.released)


Vector2 MousePosition = new Vector2(mouseState.X, mouseState.Y);

foreach(Geom a in PhysicsSimulator.GeomList)




          //Make the fixed revolute joint (mouseHandle) at MousePosition that attatches to a.Body





if(mouseState.Buttons.Left == buttonstate.pressed)


     if(MouseHandle != null)


          //Modify the anchor position to new Vector2(mouseState.x, mouseState.y)




if(mouseState.Buttons.Left == buttonstate.released && LastmouseState.Buttons.left == buttonstate.pressed)


     if(MouseHandle != null)


          MouseHandle = null;

          //Or whatever logic you desire to break the revolute joint or nullify it, or remove it from affecting the body




Hope this helps or works for you



sorry for the unnapealing appearance I will try to get a pastebin

///Pastebin at top : )

May 31, 2009 at 2:32 PM


i´ve already tried that way and still with no results, but just in case i gave it another try, and it doesn´t work, i think maybe it

has to be with the fact that the FixedRevolutionJoint is fixed, and then the object only rotates... And sometimes it get´s crazy!!!

Thanks for the help anyway.

Jul 30, 2009 at 5:32 AM

hi there.

did you find a solution already (I noticed that the last reply was 2 months ago)

anyway, if you didn't I've just made a solution that works fine.

& I thought I'd post it here for anybody who might need it in the future


don't forget to add FarseerPhysicsXNA & (SimpleSamplesXNA.Drawing OR AdvancedSamplesXNA.Drawing //used for making rectangle brush)


using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Collisions;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Dynamics.Joints;
using FarseerGames.FarseerPhysics.Factories;

using FarseerGames.SimpleSamplesXNA.DrawingSystem;

namespace FarseerMouseDraggingObjects

    public class Game1 : Microsoft.Xna.Framework.Game
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        PhysicsSimulator physicsSimulator;

        Body movingBody;
        Geom movingBodyGeom;
        RectangleBrush movingBodyBrush;

        //this body doesn't need a geometry
        Body dummyBody;

        RevoluteJoint mouseJoint;

        MouseState prevMouseState;
        bool moveDummy;

        public Game1()
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

        protected override void Initialize()


            IsMouseVisible = true;

        protected override void LoadContent()
            spriteBatch = new SpriteBatch(GraphicsDevice);

            physicsSimulator = new PhysicsSimulator();

            movingBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 80, 50, 1);
            movingBodyGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, movingBody, 80, 50);
            movingBodyBrush = new RectangleBrush(80, 50, Color.Gold, Color.Black);
            movingBody.Position = new Vector2(400, 400);

            dummyBody = BodyFactory.Instance.CreateRectangleBody(20, 20, 1);

            mouseJoint = JointFactory.Instance.CreateRevoluteJoint(movingBody, dummyBody, dummyBody.Position);

        protected override void UnloadContent()

        protected override void Update(GameTime gameTime)
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))

            MouseState mouseState = Mouse.GetState();

            //if mouse button have just been pressed
            if (prevMouseState.LeftButton == ButtonState.Released && mouseState.LeftButton == ButtonState.Pressed)
                Vector2 mouseLocation = new Vector2(mouseState.X, mouseState.Y);
                Geom tempGeom = physicsSimulator.Collide(mouseLocation);

                if (tempGeom != null)
                    dummyBody.Position = mouseLocation;
                    //the following line will make garbage every time it's executed
                    //mouseJoint = JointFactory.Instance.CreateRevoluteJoint(tempGeom.Body, dummyBody, dummyBody.Position);

                    //this way we'll reuse the joint without making unnecessary garbage
                    mouseJoint.Body1 = tempGeom.Body;
                    mouseJoint.Body2 = dummyBody;
                    mouseJoint.Anchor = mouseLocation;


                    moveDummy = true;

            //if mouse button is already pressed & still held down
            if (moveDummy && prevMouseState.LeftButton == ButtonState.Pressed && mouseState.LeftButton == ButtonState.Pressed)
                float movementSpeed = 5.0f;

                Vector2 MouseDirection = new Vector2(mouseState.X, mouseState.Y);
                MouseDirection = MouseDirection - dummyBody.Position;
                if (MouseDirection.Length() > movementSpeed)
                    //if the movement is more than movementSpeed, limit it to movementSpeed
                    dummyBody.Position = dummyBody.Position + (MouseDirection * movementSpeed);
                    //if the movement is less than movementSpeed, add it normally
                    dummyBody.Position += MouseDirection;

            //if mouse button have just been released
            if (moveDummy && prevMouseState.LeftButton == ButtonState.Pressed && mouseState.LeftButton == ButtonState.Released)

                moveDummy = false;

            prevMouseState = mouseState;


        protected override void Draw(GameTime gameTime)

            movingBodyBrush.Draw(spriteBatch, movingBodyGeom.Position, movingBodyGeom.Rotation);



hope this helps.