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

Body object fall inside the platform

Topics: Developer Forum, User Forum
Aug 23, 2011 at 10:02 AM
Edited Aug 23, 2011 at 10:05 AM

Hello all,

I try to write a sample program using XNA and Farseer together. My problem is that the object (dynamic body) fall inside the platform, into a lot of pixels. That's a problem of collision between objects or a bad texture displaying?

The source code is the following :

using FarseerPhysics;
using FarseerPhysics.Collision;
using FarseerPhysics.Controllers;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Farseer
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        World world;
        Body myBody;
        Body plateforme;

        Texture2D spriteTexture;
        Texture2D plateformeTexture;

        SpriteFont font;

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

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
            // TODO: Add your initialization logic here
            world = new World(new Vector2(0, 20));
            myBody = BodyFactory.CreateRectangle(world, 91, 91, 1f, new Vector2(graphics.PreferredBackBufferWidth / 2 - 91 / 2, 0));
            myBody.IsStatic = false;
            myBody.BodyType = BodyType.Dynamic;

            plateforme = BodyFactory.CreateRectangle(world, 320, 60, 1f, new Vector2(graphics.PreferredBackBufferWidth / 2 - 320 / 2, graphics.PreferredBackBufferHeight / 2 - 60 / 2));
            plateforme.IsStatic = true;
            plateforme.BodyType = BodyType.Static;
            plateforme.Restitution = 0.5f;


        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            spriteTexture = Content.Load<Texture2D>("square");
            plateformeTexture = Content.Load<Texture2D>("plateforme");
            font = Content.Load<SpriteFont>("Default");

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
            // TODO: Unload any non ContentManager content here

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

            // TODO: Add your update logic here
            float deltatime = (float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f;


        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)

            // TODO: Add your drawing code here

            spriteBatch.Draw(spriteTexture, myBody.Position, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
            spriteBatch.Draw(plateformeTexture, plateforme.Position, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
            spriteBatch.DrawString(font, "Framerate : " + (int)(1000 / gameTime.ElapsedGameTime.TotalMilliseconds) + " FPS\nX : " + myBody.Position.X + "\nY : " + myBody.Position.Y + "\n", new Vector2(0, 0), Color.White);


The sprites called "square" and "plateforme" have a size of 91x91 px and 320x60 px, respectively. I'm using the latest version of Farseer Physics (3.3.1) and I'm not found a tutorial nor documentation about this version unfortunately.

Can you help me ?

Aug 23, 2011 at 10:23 AM

may try it with myBody.IsBullet = true;

Aug 23, 2011 at 10:29 AM

No, it doesn't work :(

Aug 23, 2011 at 2:54 PM
Edited Aug 23, 2011 at 2:54 PM

i'm nearly 100% sure its because the drawing. first of all you need to scale the physics down (about 25 times) and scale it back up at drawing.

and also you have to change the drawing code. example:

and your framerate counter doesn't really work. take a look at this: