Body Drag

Jan 30, 2012 at 8:17 PM

I am currently applying a force to a body based on a players input from a touch thumb stick.  This seems to work well for me but I would like the body to stop immediately when the player releases the thumb stick.  How can I achieve this?  I've played around with the LinearDamping of the body as well as my force being applied, but I cannot get the results I desire.  I noticed that even if I set the Damping of the body very high and leave the force being applied to the same value, I still have slight movement to the body when the force being applied is released.

Here is how I setup the Body:

            Body = BodyFactory.CreateRectangle(Engine.World, ConvertUnits.ToSimUnits(_hero.Width),
                                               ConvertUnits.ToSimUnits(_hero.Height - 12), .5f);
            Body.IsStatic = false;
            Body.Friction = .5f;
            Body.LinearDamping = BodyDamping; //6f
            Body.IgnoreGravity = true;
            Body.FixedRotation = true;
            Body.BodyType = BodyType.Dynamic;
            Body.Position = new Vector2(0,0);
            Body.CollisionCategories = Category.Cat1;
            Body.CollidesWith = ~Category.Cat15;

            Fixture = FixtureFactory.AttachRectangle(ConvertUnits.ToSimUnits(_hero.Width),
                                                     ConvertUnits.ToSimUnits(_hero.Height - 12), .5f, Vector2.Zero,
                                                     Body);

 

//If the Player enables movement, a force is applied as follows:

                leftStickDirection.Normalize();
                var force = ConvertUnits.ToSimUnits(new Vector2(leftStickDirection.X * Speed, -leftStickDirection.Y * Speed)); //Speed is normally 15000
                Body.ApplyForce(ref force);

Jan 30, 2012 at 9:22 PM

How about setting linear and angular velocity to 0. 

Jan 31, 2012 at 10:37 AM

Setting the velocity to zero didn't seem to work.  I finally got it working the way I want, but i'm not happy with the way I did it.  Basically, I set the BodyType to static when the user is not making any movements.  I change it back to Dynamic when controls are activated again.

Jan 31, 2012 at 7:28 PM

Is your world.AutoClearForces set to false? This will make the force persist until an equal and opposite force is applied.

Jan 31, 2012 at 9:23 PM
jerrysb wrote:

Is your world.AutoClearForces set to false? This will make the force persist until an equal and opposite force is applied.

Its set to true, I don't seem to be adjusting this anywhere either.  I posted a small video of my issue: http://youtu.be/pblKde3p3Yk, sorry for the bad quality.  You can see at the top is an output of Body.LinearVelocity.Length().  What is strange about this issue, the body always moves upward, even with no force being applied.  Even when my game first starts, this similar effect happens to my body before any force is ever applied.

 

Jan 31, 2012 at 9:40 PM

How about gravity? I assume it's set to 0 but just in case... 

Is there some collision between the hero and the gun that might make them act on each other. Or some joint issue between them? Or is the gun just part of the sprite?

In your event loop is it possible that the stick takes some time to "calm down" before it return Vector.Zero? Then floating point issues might still poduce a force even when it's essentially not moving.

Another thing - the force you have to apply to move the body is huge - 15kN means big dimensions. Perhaps it's unstable because of that. Try to scale down the things a bit - will be good for the future as well. A character like this should weigh around 70kg plus some for the weapon. That would be realistic.

Sorry I have no more ideas but I copy/pasted your code fragment into my testbed on a PC and it works the way it should - no issues (I even made the hero big 10x20m).