3

I've just got Farseer Physics working and I've created a quick/crude base object I can use to create objects easily.

So I've set up a quick simulation and it looks like this: Farseer example of boxes not colliding

in DebugView, it looks like this: Farseer example of boxes in debug mode

Upon closer inspection, with both modes enabled, I can see that the Orange boxes have one row and column of pixels missing: Missing pixel on texture

Anyone know why that might be?

class BasePhys
{
    public float unitToPixel;
    public float pixelToUnit;
    public Vector2 size;
    public Body body;
    public Texture2D texture;

    public BasePhys(World world, int x, int y)
    {
        this.unitToPixel = 100.0f;
        this.pixelToUnit = 1 / unitToPixel;
        TextureManager.GetTextureByName("base", ref this.texture);
        this.size = new Vector2(this.texture.Width, this.texture.Height);
        this.body = BodyFactory.CreateRectangle(world, this.size.X * this.pixelToUnit, this.size.Y * this.pixelToUnit, 1);
        this.body.BodyType = BodyType.Dynamic;
        this.body.Position = new Vector2(x * this.pixelToUnit, y * this.pixelToUnit);
    }
    public void Draw(SpriteBatch spriteBatch)
    {
        Vector2 scale = new Vector2(this.size.X / (float)this.texture.Width, this.size.Y / (float)this.texture.Height);
        Vector2 position = this.body.Position * unitToPixel;
        spriteBatch.Draw(this.texture, position, null, Color.White, this.body.Rotation, new Vector2(this.texture.Width / 2.0f, this.texture.Height / 2.0f), scale, SpriteEffects.None, 0);
    }
}

Does anyone know why this may be? It doesn't do it in all the Farseer demos. I've checked the texture and it has no invisible pixels, orange pixels fill the entire file.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
MatthewMcGovern
  • 3,466
  • 1
  • 19
  • 19
  • How does it look in debug draw mode? Do your physics bodies line up perfectly with your boxes/textures? – Fred Gassmann Jan 11 '13 at 20:39
  • @FredGassmann - Having trouble getting debug view working. It's rendering everything on a minute scale. – MatthewMcGovern Jan 11 '13 at 20:50
  • I was having similar problems when getting farseer integrated into one of my XNA projects. I then tried debug view and it wasnt scaled properly for me either. I studied the farseer sample app and eventually was able to get debug view working. Once I did it became trivial to line up physics bodies with sprites. I dont have access to that code at the moment but I'll check back later today when I do and if you're still having problems I'll see if I can offer some more info. I would suggest focusing on the debug view, because if it's not working properly you're going to run into further problems – Fred Gassmann Jan 11 '13 at 21:07
  • @FredGassmann http://i.imgur.com/Yf2Qx.png - I got debug to work. – MatthewMcGovern Jan 11 '13 at 21:09
  • Can you turn your textures back on with some alpha so you can see through and check for alignment? Seams like your scaling is slightly off. – Fred Gassmann Jan 11 '13 at 21:17
  • @FredGassmann I tried that and noticed they line up except for one bit, http://i.imgur.com/hcPy0.png There's a pixel missing on the right and bottom of every square! It's fine on the big green bar weirdly, just not the orange ones. – MatthewMcGovern Jan 11 '13 at 21:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22606/discussion-between-fredgassmann-and-matthewmcgovern) – Fred Gassmann Jan 11 '13 at 21:41
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jan 12 '13 at 02:32

1 Answers1

1

Polygons in Farseer have a thin "skin" around them. They will not contact each other exactly - but be offset by this skin. This is by design.

For more details, see Settings.PolygonRadius. It's basically there to help improve collision detection.

By default the polygon radius is 0.01 physics units. You'll notice that, at your scale of 1 unit = 100 pixels, the polygon radius is equal to exactly one pixel - hence the one pixel gap between your objects.

Probably the easiest fix for this would be to make your rectangle sizes smaller by twice the polygon radius in each direction. Alternately you could scale the texture slightly larger to account for the radius.

(Don't turn off the radius itself - you'll get physics glitches.)


Don't forget that Farseer is tuned to handle objects of sizes and weights ranging from 0.1 to 10 units. Traditionally you would use meters and kilograms for these scales (although you don't have to).

Personally I find the pixel-to-unit conversion a bit ugly, because you end up with error-prone scaling code scattered throughout your project. For something substantial I'd recommend coding everything in physics-space, and then using a camera matrix (which you could pass to SpriteBatch.Begin) to scale everything at render time.

Andrew Russell
  • 26,924
  • 7
  • 58
  • 104
  • 1
    I've played around with some settings and values after reading your post and have been able to fix it :) I scale the textures so that they increase by 1 pixel in both axes and shift the textures to line up with the simulation. – MatthewMcGovern Jan 12 '13 at 20:18