problem with world.step and different fps

Topics: Developer Forum, Project Management Forum, User Forum
Jun 23, 2011 at 9:47 AM

hi, I made a game in 60fps.

for world.step use this code:

StepWorld = (float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f;
World.Step(StepWorld);

 

my player can be fire and when fire run this code:

            Vector2 force = direction;
            force.X *= 20000;
            force.Y *= 20000;
            bullet.Body.ApplyLinearImpulse(force);

 

when my game run at 60 fps no problem, but if my game start with limit 30 fps the bullet are very slow!

why?

help me! please! I dont understand....

 

Jun 23, 2011 at 2:53 PM

i have the same problem with applying force and torque. with set LinearVelocity = value*StepWorld; it works fine. also tryed ApplyForce(value*StepWorld*Body.Mass) but this also doesn't work.

for now i just set the linear/angular velocity.

Developer
Jun 23, 2011 at 11:36 PM

@HellGate: Why multiply by Body.Mass? This is done for you by the engine.

@kuma17: You shouldn't need to change your forces at all.  Your problem is that you are using Impulses instead of Forces. Impulses are applied every frame without regard to the delta time. Whereas Forces use the delta time. That is why your bullets would be twice as slow.

Jun 24, 2011 at 7:11 AM

 

ok, thanks for the responses, I will test today.

Jun 25, 2011 at 10:13 AM

i got another problem with applying force. i use a scale for farseer (the physics runs 25 times smaller) and erverything i do i scale  up or down by 25. but if i change the 25 to 250 applying force is much stronger (i do force/250). how to fix this?

Dec 30, 2011 at 3:41 AM

You need to convert to simulator units, see here: http://farseerphysics.codeplex.com/discussions/267272

/// /// Convert units between display and simulation units.
///     public static class ConvertUnits
    {
        private static float _displayUnitsToSimUnitsRatio = 100f;
        private static float _simUnitsToDisplayUnitsRatio = 1 / _displayUnitsToSimUnitsRatio;

        public static void SetDisplayUnitToSimUnitRatio(float displayUnitsPerSimUnit)
        {
            _displayUnitsToSimUnitsRatio = displayUnitsPerSimUnit;
            _simUnitsToDisplayUnitsRatio = 1 / displayUnitsPerSimUnit;
        }

        public static float ToDisplayUnits(float simUnits)
        {
            return simUnits * _displayUnitsToSimUnitsRatio;
        }

        public static float ToDisplayUnits(int simUnits)
        {
            return simUnits * _displayUnitsToSimUnitsRatio;
        }

        public static Vector2 ToDisplayUnits(Vector2 simUnits)
        {
            return simUnits * _displayUnitsToSimUnitsRatio;
        }

        public static void ToDisplayUnits(ref Vector2 simUnits, out Vector2 displayUnits)
        {
            Vector2.Multiply(ref simUnits, _displayUnitsToSimUnitsRatio, out displayUnits);
        }

        public static Vector3 ToDisplayUnits(Vector3 simUnits)
        {
            return simUnits * _displayUnitsToSimUnitsRatio;
        }

        public static Vector2 ToDisplayUnits(float x, float y)
        {
            return new Vector2(x, y) * _displayUnitsToSimUnitsRatio;
        }

        public static void ToDisplayUnits(float x, float y, out Vector2 displayUnits)
        {
            displayUnits = Vector2.Zero;
            displayUnits.X = x * _displayUnitsToSimUnitsRatio;
            displayUnits.Y = y * _displayUnitsToSimUnitsRatio;
        }

        public static float ToSimUnits(float displayUnits)
        {
            return displayUnits * _simUnitsToDisplayUnitsRatio;
        }

        public static float ToSimUnits(double displayUnits)
        {
            return (float)displayUnits * _simUnitsToDisplayUnitsRatio;
        }

        public static float ToSimUnits(int displayUnits)
        {
            return displayUnits * _simUnitsToDisplayUnitsRatio;
        }

        public static Vector2 ToSimUnits(Vector2 displayUnits)
        {
            return displayUnits * _simUnitsToDisplayUnitsRatio;
        }

        public static Vector3 ToSimUnits(Vector3 displayUnits)
        {
            return displayUnits * _simUnitsToDisplayUnitsRatio;
        }

        public static void ToSimUnits(ref Vector2 displayUnits, out Vector2 simUnits)
        {
            Vector2.Multiply(ref displayUnits, _simUnitsToDisplayUnitsRatio, out simUnits);
        }

        public static Vector2 ToSimUnits(float x, float y)
        {
            return new Vector2(x, y) * _simUnitsToDisplayUnitsRatio;
        }

        public static Vector2 ToSimUnits(double x, double y)
        {
            return new Vector2((float)x, (float)y) * _simUnitsToDisplayUnitsRatio;
        }

        public static void ToSimUnits(float x, float y, out Vector2 simUnits)
        {
            simUnits = Vector2.Zero;
            simUnits.X = x * _simUnitsToDisplayUnitsRatio;
            simUnits.Y = y * _simUnitsToDisplayUnitsRatio;
        }
    }