This project has moved. For the latest updates, please go here.

[Solved]Two rectangles not colliding

Topics: User Forum
Sep 24, 2012 at 6:19 PM

Okay so i have decided to have a look at the farseer physics engine. My first test project was to essentially add two rectangular bodies to the world. I then set one of the bodies to move up and the other to move down, however instead of colliding the two bodies just pass through each other, Here is my code:

namespace farseer_2_rectangles
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D texture;
        Body mbody1;
        Body mbody2;

        Vector2 velocity1;
        Vector2 velocity2;
        Vector2 position1;
        Vector2 position2;
        float rotation1;
        float rotation2;

        World world;
        int tankwidth=20, tankheight=35;

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

        public float converttometers(float pixel)

            return pixel / 64;
        public float converttopixels(float meters)

            return meters * 64;
        /// <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,1));
            position1 = new Vector2(converttometers(50),converttometers(20));
            position2 = new Vector2(converttometers(100),converttometers(200));


        /// <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);
            texture = Content.Load<Texture2D>("wall");

            mbody1 = BodyFactory.CreateRectangle(world, converttometers(tankwidth), converttometers(tankheight), 1f, position1);
            mbody2 = BodyFactory.CreateRectangle(world, converttometers(tankwidth), converttometers(tankheight), 1f, position2);
            mbody1.BodyType = BodyType.Dynamic;
            mbody2.BodyType = BodyType.Dynamic;

            mbody1.Restitution = 0.3f;
            mbody2.Restitution = 0.3f;
            mbody1.Friction = 0.5f;
            mbody2.Friction = 0.5f;
            // TODO: use this.Content to load your game content here

        /// <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)
            mbody2.ApplyForce(new Vector2(0,-1));
            // TODO: Add your update logic here
            world.Step((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)

            Vector2 origin = new Vector2(texture.Width/2f,texture.Height/2f);
            position1 = mbody1.Position*64;
            position2 = mbody2.Position*64;

            spriteBatch.Draw(texture, position1, null, Color.Wheat, rotation1, origin, 1f, SpriteEffects.None, 0f);
            spriteBatch.Draw(texture, position2, null, Color.Wheat, rotation2, origin, 1f, SpriteEffects.None, 0f);
            // TODO: Add your drawing code here


any help on where i am going wrong or how i can set up two rectangular bodies to collide with each other would be great.

Oct 6, 2012 at 11:34 AM

Okay so this may be stupid but i managed to solve the problem, as i am a noob i just refrenced the dll, i didnt realise i had to refrence the project. When i added the project and refrenced it everything worked fine. Great engine and easy to understand, like everything it takes a couple of tries to get it to work :).