Physics Problems

Mar 4, 2010 at 4:44 PM

I'm trying to make something with the Farseer Physics Engine.

As far as I'm now there is lot of crazy things goin on; just watch this: http://www.youtube.com/watch?v=xObmiu6E3K0&feature=player_embedded

I don't know how to solve this so I'm asking you!

Mar 4, 2010 at 6:31 PM

Could you give more information on how you constructed that demo please?

Are you using Distance Grid or SAT, how did you create the rectangle geoms?

 

This would help us help you

Mar 5, 2010 at 10:51 AM
Edited Mar 5, 2010 at 10:52 AM

Looks to me like you are drawing the rectangles off center, however I can't be sure without more info. Have you tried using the debug view?

Mar 6, 2010 at 8:20 AM

Thanks for the replies.

In the initialization:

        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            IsMouseVisible = true;

            physics = new PhysicsSimulator(new Vector2(0, 9.81f));

            for (int i = 0; i < 100; i++)
            {
                Body BoxBody;
                Geom BoxGeom;
                BoxBody = BodyFactory.Instance.CreateRectangleBody(physics, 128, 64, 2);
                BoxBody.IsStatic = true;
                BoxBody.Position = new Vector2(0 + new Random().Next(150, 200) * i, new Random().Next(200, 500));
                BoxGeom = GeomFactory.Instance.CreateRectangleGeom(physics, BoxBody, 128, 64);
                BoxGeom.BodyType = Geom.BodyTypes.Rectangle;
            }

            PlayerBody = BodyFactory.Instance.CreateCircleBody(physics, 128, 64);
            PlayerGeom = GeomFactory.Instance.CreateCircleGeom(physics, PlayerBody, 128, 256);
            PlayerGeom.BodyType = Geom.BodyTypes.Circle;
            PlayerBody.ApplyImpulse(new Vector2(10000, 5000));

            base.Initialize();
        }

I've added the BodyType variable to the framework as it's easier to keep control of it all.

In the update:

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            viewPosition = new Vector2(PlayerBody.Position.X + 128 / 2 - GraphicsDevice.Viewport.Width / 2,
                PlayerBody.Position.Y + 64 / 2 - GraphicsDevice.Viewport.Height / 2);

            physics.Update((float)gameTime.ElapsedGameTime.Milliseconds * 0.001f);
            base.Update(gameTime);
        }
And finaly in the draw:
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);

            foreach (Geom geom in physics.GeomList)
            {
                switch (geom.BodyType)
                {
                    case Geom.BodyTypes.Rectangle:
                        spriteBatch.Draw(box, geom.Position - viewPosition, null, Color.White, geom.Rotation, new Vector2(-(128 / 2), -(64 / 2)), 1, SpriteEffects.None, 1);
                        break;
                    case Geom.BodyTypes.Circle:
                        spriteBatch.Draw(circle, geom.Position - viewPosition, null, Color.White, geom.Rotation, new Vector2(-(128 / 2)), 1, SpriteEffects.None, 1);
                        break;
                }
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }
That's the code.
Mar 6, 2010 at 11:33 AM

I solved it trought using the debug view.

The circle was too big (I've changed some stuff since the video).

And the origin was totally wrong (used this: width / 2, height / 2) and it works perfectly!

Thanks for the help guys!