Simple collision - First time using farseer

Topics: User Forum
Apr 29, 2010 at 12:21 PM

Hi everyone,

I have just started using Farseer and I'm trying to follow the included samples to create a simple collision system between some rectangles and apply gravity to them. After following the samples, and to start things off, I created just one box and all I'm trying to do is to apply gravity. I did the following:

class Buildings : Minigame
    {
        PhysicsSimulator physicsSimulator = null;
        Texture2D background;
        Texture2D brickTexture;
        ArrayList bodies = new ArrayList();
        Body test;

        int windowWidth;
        int windowHeight;

        float brickMass = 1.0f;

        public Buildings(int windowWidth, int windowHeight)
        {
            this.windowWidth = windowWidth;
            this.windowHeight = windowHeight;

            this.background = Renderer.getContent().Load<Texture2D>("buildings/sky");
            this.brickTexture = Renderer.getContent().Load<Texture2D>("buildings/buildings_brick");

            this.physicsSimulator = new PhysicsSimulator(new Vector2(0.0f, 200.0f));
            this.test = BodyFactory.Instance.CreateRectangleBody(this.brickTexture.Width, this.brickTexture.Height, brickMass);
            this.test.Position = new Vector2(100, 100);

            GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, this.test, this.brickTexture.Width, this.brickTexture.Height);
        }

        private void update(GameTime gameTime)
        {
            KeyboardState kbState = Keyboard.GetState();
            MouseState mState = Mouse.GetState();

            //Update the physics simulator
            this.physicsSimulator.Update(gameTime.ElapsedGameTime.Milliseconds * .001f);

        }

        private void draw(GameTime gameTime)
        {
            Renderer.getDevice().Clear(Color.White);
            Renderer.getSpriteBatch().Begin();

            ////Background
            Renderer.getSpriteBatch().Draw(background, new Rectangle(0, 0, windowWidth, windowHeight), Color.White);

             Vector2 origin = new Vector2(this.brickTexture.Width / 2.0f, this.brickTexture.Height / 2.0f);
            Renderer.getSpriteBatch().Draw(this.brickTexture, test.Position, null, Color.White, test.Rotation, origin, 1, SpriteEffects.None, 0);
            
            Renderer.getSpriteBatch().End();

            Console.WriteLine(" >>> Test: " + test.Position.X + ", " + test.Position.Y + " => " + test.Rotation);
        }

        public override void play(GameTime gameTime)
        {
            this.update(gameTime);

            this.draw(gameTime);
            base.play(gameTime);
        }
    }

However, nothing happens, the box is just lying there in the same place... I'm probably missing something very stupid, but can't figure out what... Any help is appreciated. : P

Thanks in advance...

Coordinator
Apr 29, 2010 at 3:04 PM
You need to add the Body object to the physics simulator.
Apr 29, 2010 at 3:10 PM

Thanks for your answer : D Hmm, isn't that automatically done by calling

 GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, this.test, this.brickTexture.Width, this.brickTexture.Height);

?