Changing Mass has no effect on force/impulse

Dec 26, 2010 at 1:47 PM

hi

i recently switched from farseer physics 2.1.3 to farseer 3.1 in order to get my project right with xna 4.0.

thanks to some post in the forum i understood that smaller body have stronger reaction to impulse ( i already scale my "physics world" in order to give farseer small units to work with ).

In my game each characters has different size, but their reaction to impulse need to be barely the same so i decided to use the "Mass" property of the body  to achieve this.

My idear was to give a greater mass to smaller body, but setting the mass property doesn't seem to affect impulse or force at all.

I think i miss something on the engine usage.

Thanks

Dec 29, 2010 at 9:47 PM

up

Coordinator
Dec 29, 2010 at 11:05 PM

Setting Mass on bodies manually works here. Could you create a testbed test (using the testbed project from downloads) to show what is wrong. Alternatively, you can send me your project by email.

Dec 30, 2010 at 9:50 AM

hi Genbox

thanks for your answer

i've made a little testbed test ( you just have to add it to the SimpleSample project, and add new screen entry in farseergame.cs ) to show what i mean.

by making this sample i directly see where my problem come from :

the mass setting is working well until i set the "FixedRotation" property to true.

I copy my demo code here  :

using System.Text;
using FarseerPhysics.DemoBaseXNA;
using FarseerPhysics.DemoBaseXNA.ScreenSystem;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace FarseerPhysics.SimpleSamplesXNA
{
    internal class Demo8Screen : GameScreen, IDemoScreen
    {
        private Fixture _rectangle;

        private Fixture _HeavyRectangle;

        #region IDemoScreen Members

        public string GetTitle()
        {
            return "Demo8";
        }

        public string GetDetails()
        {
            return "";
        }

        #endregion

        public override void Initialize()
        {
            World = new World( new Vector2( 0, -200) );

            base.Initialize();
        }

        public override void LoadContent()
        {
            _rectangle = FixtureFactory.CreateRectangle(World, 5, 5, 1);
            _rectangle.Body.BodyType = BodyType.Dynamic;

            _HeavyRectangle = FixtureFactory.CreateRectangle(World, 10, 10, 1);
            _HeavyRectangle.Body.BodyType = BodyType.Dynamic;

            _rectangle.Body.Mass = 1.0f;
            _HeavyRectangle.Body.Mass = 1.0f;

            _rectangle.Body.FixedRotation = true;
            _HeavyRectangle.Body.FixedRotation = true;

            base.LoadContent();
        }

        public override void HandleInput(InputState input)
        {
            if (input.CurrentGamePadState.IsConnected)
            {
                HandleGamePadInput(input);
            }
            else
            {
                HandleKeyboardInput(input);
            }

            base.HandleInput(input);
        }

        private void HandleGamePadInput(InputState input)
        {
            Vector2 force = 50 * input.CurrentGamePadState.ThumbSticks.Left;
            _rectangle.Body.ApplyForce(force);

            float rotation = 40 * input.CurrentGamePadState.Triggers.Left;
            _rectangle.Body.ApplyTorque(rotation);

            rotation = -40 * input.CurrentGamePadState.Triggers.Right;
            _rectangle.Body.ApplyTorque(rotation);
        }

        private void HandleKeyboardInput(InputState input)
        {
            const float forceAmount = 3000;
            const float ImpulseAmount = 560;
            Vector2 force = Vector2.Zero;
            Vector2 Impulse = Vector2.Zero;
            force.Y = -force.Y;

            if (input.CurrentKeyboardState.IsKeyDown(Keys.Left))
            {
                force += new Vector2(-forceAmount, 0);
            }
            if (input.CurrentKeyboardState.IsKeyDown(Keys.Down))
            {
                Impulse += new Vector2(0, -ImpulseAmount);
            }
            if (input.CurrentKeyboardState.IsKeyDown(Keys.Right))
            {
                force += new Vector2(forceAmount, 0);
            }
            if (input.CurrentKeyboardState.IsKeyDown(Keys.Up))
            {
                Impulse += new Vector2(0, ImpulseAmount);
            }

            _rectangle.Body.ApplyForce(force);
            _rectangle.Body.ApplyLinearImpulse( ref Impulse );

            force = Vector2.Zero;
            Impulse = Vector2.Zero;

            if (input.CurrentKeyboardState.IsKeyDown(Keys.Q))
            {
                force += new Vector2(-forceAmount, 0);
            }
            if (input.CurrentKeyboardState.IsKeyDown(Keys.S))
            {
                Impulse += new Vector2(0, -ImpulseAmount);
            }
            if (input.CurrentKeyboardState.IsKeyDown(Keys.D))
            {
                force += new Vector2(forceAmount, 0);
            }
            if (input.CurrentKeyboardState.IsKeyDown(Keys.Z))
            {
                Impulse += new Vector2(0, ImpulseAmount);
            }

            _HeavyRectangle.Body.ApplyForce(force);
            _HeavyRectangle.Body.ApplyLinearImpulse(ref Impulse);

            /*const float torqueAmount = 40;
            float torque = 0;

            if (input.CurrentKeyboardState.IsKeyDown(Keys.Left))
            {
                torque += torqueAmount;
            }
            if (input.CurrentKeyboardState.IsKeyDown(Keys.Right))
            {
                torque -= torqueAmount;
            }

            _rectangle.Body.ApplyTorque(torque);*/
        }
    }
}