Search code examples
c#monogamephysics-enginefarseer

choppy/ Laggy sprite animation using Farseer Physics


So I am using farseer physics in this 2D platformer that I am making, but for some reason it makes the animations choppy and it's irritating. I have tried it with two different sprites. The first had 4 frames the other had 10 frames. The animation is only choppy when using farseer.

Here is the code where I create the body for the sprite:

public void CreateBody(World world, float density, Vector2 position, object userData = null)
{
    //magic number; so player doesn't look silly running into something he isn't touching because of a weapon-- CHANGE LATER
    _body = BodyFactory.CreateRectangle(world, ConvertUnits.ToSimUnits(_attackForkFrames[0].Width * _scale.X - 20), ConvertUnits.ToSimUnits(_attackForkFrames[0].Height * _scale.Y - 2), density, ConvertUnits.ToSimUnits(position), userData);
    _body.BodyType = BodyType.Dynamic;
    _body.Restitution = 0f;
    _body.CollisionCategories = Category.All;//Category.Cat1;
    _body.CollidesWith = Category.All;//Category.Cat10;

...

Here is the code updating the position:

_body.Position = new Vector2(_body.Position.X + ConvertUnits.ToSimUnits(_movementSpeed), _body.Position.Y);

I do convert the _body.position to display units.


Solution

  • You are manually setting the position of your physics body, in your simulation world. This is great if you are initializing the object, however, when simulating it is a bad idea.

    Farseer will handle, for example, collisions in the background. It does this by applying various forces to the bodies that are in your world. When you move it's Position, you are not following the rules of your physics engine, you are manually moving your object in the world.

    What you want to do is apply a force instead of manually moving your body. Try playing with:

    Body.ApplyForce();