Body jittering when "afar" from origin

Topics: Developer Forum, Project Management Forum, User Forum
Oct 19, 2013 at 1:56 PM
Edited Oct 19, 2013 at 2:00 PM
I'm having issues with physics body jittering when you move objects "far away" from origin. These issues are noticeable in rendering when you are around 100 meters (FPE units) away from origin which isn't very far.

I made an example program and an Excel spreadsheet that shows these issues.

My question is, am I doing something wrong, is this just a nature of floating point operations that you can't avoid or is this a bug with FPE?

Excel spreadsheet: https://www.dropbox.com/s/ld8po2s1n2q7seb/fpe_jitter.xlsx

Example code:
using FarseerPhysics.Common;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using Microsoft.Xna.Framework;
using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        World world = new World(new Vector2(0f, 0f));
        Body body = BodyFactory.CreatePolygon(world, new Vertices() { new Vector2(0f, 0f), new Vector2(5f, 0f), new Vector2(0f, 5f) }, 1f);
        body.BodyType = BodyType.Dynamic;

        const float step = 1f / 120f;
        float t = 0f;
        float targetT = 100f;

        using (TextWriter tw = File.CreateText("data.txt"))
        {
            while (t <= targetT)
            {
                if (t < 50f)
                    body.ApplyForce(new Vector2(0f, 1f));

                world.Step(step);
                t += step;

                Vector2 pos = body.Position * 100f; // To "display units"
                Console.WriteLine("T: " + t + " Pos: " + "(" + pos.X.ToString("0.#####") + ", " + pos.Y.ToString("0.#####") + ")");
                tw.WriteLine(t + "\t" + pos.X + "\t" + pos.Y);

                //acc -= 1f;
            }
        }

        Console.ReadKey();
    }
}
Oct 20, 2013 at 8:33 AM
Edited Oct 20, 2013 at 8:34 AM
Very well documented, props on that. However do you have any data on objects within the range of 100f? Just for comparison purposes purely because I got curious about it.

I noticed in my case where my player walks on the X-axis with a max linear velocity of 2.5f or -2.5f. The value goes back and forth between 2.47f and 2.50f. I figured this was happening because of the floating points we are using.

EDIT: I am new to FPE though, so don't blindly take my word for it.
Oct 22, 2013 at 10:54 AM
I fixed the problem. The issue was in my camera position calculation. The jitters in the excel spreadsheet don't actually affect rendering too much.