Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm developing a 2D game for windows phone using Xna and Farseer library. For now I have two objects/bodies. One is falling from the top and the other one which is static is at the button of the screen and is supposed to be used to catch the object that is falling.

I need to move the static body at the button using mobile phones touch screen only on X axis.

Here is the sample code that I wrote for using the touch pad, note that "tray" is the static body.

private void UpdatePlayer(GameTime gameTime)
        // Windows Phone Controls
        while (TouchPanel.IsGestureAvailable)
            GestureSample gesture = TouchPanel.ReadGesture();
            if (gesture.GestureType == GestureType.FreeDrag)
                tray.Position.X += gesture.Delta.X;

        // Make sure that the player does not go out of bounds
        tray.Position.X = MathHelper.Clamp(tray.Position.X, 0, GraphicsDevice.Viewport.Width - trayTexture.Width);
        tray.Position.Y = MathHelper.Clamp(tray.Position.Y, 0, GraphicsDevice.Viewport.Height - trayTexture.Height);

and this is the initialization of this Body

//Create Floor (tray)
        tray = BodyFactory.CreateRectangle(MyWorld, ConvertUnits.ToSimUnits(143), ConvertUnits.ToSimUnits(12), 10);
        tray.Restitution = 0.2f;        //Bounceability
        tray.Friction = 0.1f;           //Friction
        tray.IsStatic = true;         //Static objects wont move when hit by other objects

        tray.Position = new Vector2(((int)ConvertUnits.ToSimUnits(playerPosition).X),
            ((int)ConvertUnits.ToSimUnits(playerPosition).Y)); //set the position of tray

But the problem is I cannot modify the return value of tray.Position because it's not a variable.

So how can I do it?

share|improve this question
up vote 1 down vote accepted

One is falling from the top and the other one which is static is at the button of the screen and is supposed to be used to catch the object that is falling.

Sounds like you should use a kinematic body instead of a static one and adjust its velocity so that it moves towards the targeted location quickly when its far away and none at all when the x position is the same.

A kinematic body moves under simulation according to its velocity. Kinematic bodies do not respond to forces. They can be moved manually by the user, but normally a kinematic body is moved by setting its velocity. A kinematic body behaves as if it has infinite mass, however, Box2D stores zero for the mass and the inverse mass. Kinematic bodies do not collide with other static or kinematic bodies.

This prevents the object from teleporting and confusing Box2D and looks better to your users since the object is now chasing around their finger. Also it encourages them to keep their finder on the pad and move the object. The way you have it set up know the best thing to do as a user would to be tapping wherever is under the ball kind of destroying the appeal of a touchscreen which is that you are interacting and physically moving the objects on screen.

I can't really think of a good interpolation function for the velocity off the top of my head unfortunately. Something along the lines of MathHelper.Lerp(CurrVel, vWeight * dX, iWeight) might work though.

share|improve this answer

Probably because it is a property and not a normal member variable. You need to change the entire tray.Position variable like this:

tray.Position = new Vector2(tray.Position.X + delta.X, tray.Position.Y);

Note that properties are a C# language feature.

However this is what Farseer considers teleporting an object and this can interfere with the stability of the physics simulation.

Also note that the SAP algorithm does not like teleporting objects or very high speed objects such as moving from one end of the world to the other or bullets. It may break down from it and cause unreliable collisions.

That's from the old manual but probably still relevant. I suggest you first try just setting the position like you're trying now. If this gives weird behavior try moving the tray using Force and Inertia.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.