problem of movement

Topics: Developer Forum, User Forum
Feb 25, 2011 at 4:13 PM

Hi,

I don't understand why the movements are so slow (with a very high gravity ... nothing change when I increase it, my object falling down at the same velocity than if I put a low gravity). Could someone help me please ?

Thanks

 

 

 

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        World world;

        Texture2D texture;
        Fixture rectangle;
        Vector2 posRect;

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

        protected override void Initialize()
        {

            base.Initialize();

            graphics.PreferredBackBufferWidth = 1024;
            graphics.PreferredBackBufferHeight = 800;
            graphics.ApplyChanges();

            world = new World(new Vector2(0, 1000000)); 
            posRect = new Vector2(100, 100);

            rectangle = FixtureFactory.CreateRectangle(world, 80, 69, 1,posRect);
            rectangle.Body.BodyType = BodyType.Dynamic;
            rectangle.Body.Mass = 100;

        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            texture = Content.Load<Texture2D>("image");
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            

world.Step(Math.Min((float)gameTime.ElapsedGameTime.TotalMilliseconds

* 0.001f, (1f / 30f)));

 

base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); spriteBatch.Draw(texture, rectangle.Body.Position, Color.White); spriteBatch.End(); base.Draw(gameTime); }

 

 

Feb 25, 2011 at 8:09 PM
Edited Feb 25, 2011 at 9:32 PM

The Farseer Physics Engine uses the Meters-Kilograms-Second (MKS) system of units.  More information about this can be found in section 1.7 of the Box2D Manual and in its FAQ page. This means that you SHOULD NOT use pixels as units to build your fixtures.  You need to convert the pixels to meters.

As per your example:

rectangle = FixtureFactory.CreateRectangle(world, 80, 69, 1, posRect);

In the above line, you are creating a fixture which is 80 meters wide and 69 meters high and that will probably overflow the engine, which is why everything slows down.

One way to get around this is by using the ConvertUnits class (found under "Samples\Samples XNA\Samples XNA\ScreenSystem") to switch between display units (pixels) and simulation units (MKS).  


For your example, first we need to convert the pixels to simulation units:

float width = ConvertUnits.ToSimUnits(80),
       height = ConvertUnits.ToSimUnits(69);

posRect = ConvertUnits.ToSimUnits(100, 100);

rectangle = FixtureFactory.CreateRectangle(world, width, height, 1, posRect);

 

Then in the Draw method, you need to convert the position of the rectangle back to display units:

spriteBatch.Draw(texture, ConvertUnits.ToDisplayUnits(rectangle.Body.Position), Color.White);

Try that out and see how it works for you.

Feb 25, 2011 at 8:30 PM

it works great, thank you very much ^.^