This project has moved and is read-only. For the latest updates, please go here.

So confused trying to use 3.0...

Oct 5, 2010 at 3:39 AM

Hey all, I'm trying to use farseer with Tiled/TileLib and I'm running into some problems. I'm trying to loop through the tiles in my collision layer and make bodies for them so I can get collision working with my player. Here's the code I have for that:


            TileLayer tileLayer = map.GetLayer("Collision") as TileLayer;
            for (int y = 0; y < tileLayer.Height; y++)
                for (int x = 0; x < tileLayer.Width; x++)
                    Fixture tileFixture = FixtureFactory.CreateRectangle(world, 32, 32, 1);
                    tileFixture.Body.Position = new Vector2(x * 32, y * 32);
                    tileFixture.Body.BodyType = BodyType.Static;


The first problem is when I run this loop, it takes forever to run and once the game is finally loaded up, the FPS is incredibly low, around 2-5 fps. Any idea why? Does it have something to do with having to convert from pixels to meters? I'm confused as to how to do that. Should I divide all my tile width/heights by 100 and use that as my conversion factor? I had this working in farseer 2.x a few months ago but now decided to come back to this project using 3.0 and I'm just having trouble getting anything to work right.


Second: Do I need to do anything specific to turn on collision, or will it just work once I have the fixtures/bodies added to the world?


Finally: I can't figure out how to use DebugView for the life of me. Do I need to add that project, "DebugView 3.0 XNA" to my solution and then instantiate it or build it and add it as a reference or what?



Thanks in advance!

Oct 5, 2010 at 5:02 AM
Edited Oct 5, 2010 at 5:39 AM


Hi alkier,

I can't say I know the engine as well as others here, but I believe your problem is that you are passing in pixel units for your rectangle width and height. A value of 32 is considered VERY large to farseer.

What you need is a scalar value that you define and use to convert from pixels to "meters" and back. Don't be confused by the term "meters"...its actually just a scaling value.

For example:

public float PixelsInAMeter = 100.0f;

You can store the width and height of tiles in pixels and use it for drawing as you normally would, but when you are passing info to Farseer you have to apply the scalar value(PixelsInAMeter).

Example(based off your code):

 Fixture tileFixture = FixtureFactory.CreateRectangle(world, 32/PixelsInAMeter , 32/PixelsInAMeter, 1);

32/100.0f = 0.32f ... a much smaller value which is what Farseer 3.0 likes. 

Example of getting position values from Farseer:

player.Position = new Vector2d(fixture.Body.Position.X*PixelsInMeter, fixture.Body.Position.Y*PixelsInMeter);

^this converts Farseer's small value back to pixels. For drawing ect.

I hope this helps. Good luck!

Oct 5, 2010 at 6:16 AM

For debug view you do need to add it to your project. If you use a camera getting the debug view to work can be kind of tricky.

I use TileLib and Farseer 3.0 in my current game so if you have any questions let me know.

I use a object layer for collision instead of setting it for every tile I just mark out the parts that are walls with objects. Makes it a lot easier for me and you get less objects.

Oct 5, 2010 at 7:25 AM

What you said about pixels in a meter has helped so much. I was sitting here googling about how to convert pixels to meters, haha. Now I know it's just some arbitrary scaling factor to make your units in the proper range for farseer. Whew, keeping all these different conversions straight is tough. I think I almost got things working but it's still a little screwy. Will take a lot of trial and error.


jrommann: I tried your idea of using an object layer instead and it seems to work. Just a bit puzzled why what I was using above slows it down sooo much. Going to have to figure it out eventually as I'm going to have other situations where I'll need to get the tile metadata. Can you post an example of looping through a tile layer? Maybe I'm doing it wrong...

Also, would you mind explaining how to use the debug view? Is the only thing I need to add to my project the "DebugView 3.0 XNA"?


And I think one last question I have. The game I'm working on is overhead view, so mainly I want my player to face and move in the direction the analog stick is facing. I can do that just fine by doing pos.x += cos(rotation) and player.y += sin(rotation). With using farseer, should I do that and then set the body to have the new X and Y values, or should I be using ApplyForce on the body and then set the player texture to be the X/Y/rotation values I extract from farseer? If the latter, how would I apply a force behind my player like that?


Ahh, sorry for so many questions. Greatly appreciate all the help!

Oct 5, 2010 at 7:57 AM
Edited Oct 5, 2010 at 8:18 AM

You'll get used to doing the conversions very quickly. I feel your pain..its tough to grasp at first. Check out Section 1.7 - "Units" of the Box2d manual (the engine farseer was based off of) here for more explanation: -- It mentions that that the engine works best (most stable) with objects measurements between 0.01-10 "meters". Now you understand why having hundreds of 32x32 tiles without scaling made it run slow. :) -- Grr... maybe we should drop the term 'meters' and use "world units"or something to stop confusing people hah.

As for your last question: It makes more sense (atleast to me) to do all  rotations and movements to the Farseer Body and then every frame update the players draw rotation and draw postition from it's Body. I suppose you could probably get away with ignoring rotation in the physics engine and use rotation only for drawing purposes....but having the rotation in farseer is, of course, taking into account for physics calculations... say for example, a 2d side scroller with the player as a Box+ball with torque(like a wheelcar): the rotation of it's "feet" will have added calculations to boxes you run into based on how the circle is spinning.

Oct 5, 2010 at 7:57 PM

Yeah that helps.

I'm still having trouble trying to figure out how to use the debugview in my project. Could someone explain the process?

Oct 6, 2010 at 2:30 AM
Edited Oct 6, 2010 at 2:33 AM

First you add the debug view to your project then add a reference to it.

Then in your draw code you just add this to draw the debug info. You will need to change the matrix if your are using a camera.

float aspect = (float)Engine.Settings.Video.Width / (float)Engine.Settings.Video.Height;
m_Projection = Matrix.CreateOrthographic(40 * aspect, 40, 0, 1);                   
m_View = Matrix.Identity;               

DebugView.RenderDebugData(ref m_Projection, ref m_View);

That should at least get you started in the right direction.


Oct 6, 2010 at 2:39 AM
Edited Oct 6, 2010 at 4:03 AM

I built DebugViewXNA and added it to my project. edit: Yay, finally got debug view showing up! And wow, what's happening is so funky. But that's another subject for later.


If I created a camera matrix and passed it into spritebatch like so:

Matrix cameraMatrix = Matrix.CreateTranslation(-(int)camera.X, -(int)camera.Y, 0);

Globals.spriteBatch.Begin(SpriteBlendMode.Additive, SpriteSortMode.Deferred, SaveStateMode.None, cameraMatrix);


Would I be able to just multiply that camera matrix by your view and projection matrix? Agh, I'm really trying to understand how matrix works. Anyone know any good resources to learn the gist of creating matrices to achieve some goal (and not necessarily the math behind them)?

Oct 6, 2010 at 9:29 PM
Edited Oct 6, 2010 at 9:32 PM

I'm not very good with Matrices either but I got this figured out. Just replace the red highlighted part with your cameras position and I think everything will line up. Also my m_PixelsMeter is set to 100
The Engine.Settings.Video is just away to get the current resolution. Example 1024x768

m_View = Matrix.CreateTranslation((Engine.Settings.Video.Width / 2) / -m_PixelsMeter, (Engine.Settings.Video.Height / 2) / -m_PixelsMeter, 0);
Vector2 size = (((new Vector2(Engine.Settings.Video.Width, Engine.Settings.Video.Height)) / (m_PixelsMeter * 2)) / 1f);
m_Projection = Matrix.CreateOrthographicOffCenter(-size.X, size.X, size.Y, -size.Y, 0, 1);


Oct 7, 2010 at 4:16 AM
Edited Oct 7, 2010 at 4:18 AM
jrommann wrote:
I use TileLib and Farseer 3.0 in my current game so if you have any questions let me know.

I use a object layer for collision instead of setting it for every tile I just mark out the parts that are walls with objects. Makes it a lot easier for me and you get less objects.

Alright, you seem to be the perfect person to ask about this. I spent all day yesterday trying to figure this out. I tried using object layers for collision layers, and then I load it in and create fixtures for them all in farseer, scaling the values by my pixeslPerMeter value:



            MapObjectLayer mol = map.GetLayer("Collision") as MapObjectLayer;
            foreach (MapObject mo in mol.Objects)
                Fixture tileFixture = FixtureFactory.CreateRectangle(
                    new Vector2((mo.Bounds.X)/pixelsPerMeter, (mo.Bounds.Y)/pixelsPerMeter)

                tileFixture.Body.BodyType = BodyType.Static;



Once I got the debug view working, I saw that it was making bodies for each object layer but it's not coming out right at all. First, they're positioned completely wrong. When I run my game, the collision layers are overlapping when they shouldn't at all. The dimensions of my two object layers are, in rectangle format: 0 0 1472 32 and 0 32 32 1408 (x y width height), so they shouldn't be overlapping at all. Second, the bodies don't match up to my actual map. But come to think of it, I don't think they ever will since I'm scaling it down by pixeslPerMeter (100), so that'd make my tile width .32x.32 when it would have to be 32x32 to match up with the actual map. Is there something I'm missing, or a way to make the debug view scale it back up by pixelsPerMeter when it's drawing it? I hope any of this paragraph makes sense.

Since you say this is how you're doing it, I'd so greatly appreciate you telling me how. Going insane with this. :(


(oh, and I'll try your matrix suggestion a little later tonight, have to run in a few so just posting this question here real quick)

Oct 7, 2010 at 5:19 AM
Edited Oct 7, 2010 at 5:21 AM

This is how I load my collision layer.
m_Walls is just a single Body and I attach all the collision layer fixtures to it.
Below my map load code is my functions for conversion.

The view matrices take care of the scaling and what not so they should be extremely close. I would suggest trying the matrix code I posted to see if it matches better.
I use RogueCommanderIX camera class ->

Hope this answers your questions.

//load the maps walls
MapObjectLayer walls = (MapObjectLayer)m_Map.GetLayer("Collision");

m_Walls = BodyFactory.CreateBody(Engine.Physics.World);
m_Walls.BodyType = BodyType.Static;

foreach (MapObject o in walls.Objects)
    Rectangle rect = o.Bounds;                                        

    Vertices verts = new Vertices();
    verts.Add(Engine.Physics.PositionToWorld(new Vector2(rect.Left, rect.Top)));
    verts.Add(Engine.Physics.PositionToWorld(new Vector2(rect.Right, rect.Top)));
    verts.Add(Engine.Physics.PositionToWorld(new Vector2(rect.Right, rect.Bottom)));
    verts.Add(Engine.Physics.PositionToWorld(new Vector2(rect.Left, rect.Bottom)));

    Fixture f = m_Walls.CreateFixture(new PolygonShape(verts));
    f.Friction = 0f;
    f.Restitution = 0f;
    f.CollidesWith = CollisionCategory.All;
    f.CollisionCategories = Globals.CollisionGroup[Globals.Group.Map];

/// <summary>
/// Converts the position from game world to the physics world
/// </summary>
/// <param name="position">Position in game world</param>
/// <returns></returns>
public Vector2 PositionToWorld(Vector2 position)
    return position / PixelsAMeter;
/// <summary>
/// Converts the position from physics world to game world
/// </summary>
/// <param name="position">Position in physics world</param>
/// <returns></returns>
public Vector2 PositionToGame(Vector2 position)
    return position * PixelsAMeter;

Oct 7, 2010 at 10:02 AM

Wow, you're my hero! I'm so close to getting this fully working. Without the camera, the debug view is lining up perfectly w/my map. The only thing that's left is getting my camera working. I'm not sure what you mean by replacing the red with my camera position. Right now I'm just using a simple camera I made where it's a vector set based on the player's position and I later made my cameraMatrix with that and passed that over to spriteBatch.Begin(). I tried replacing the red part with its X/Y values like you said, but no go. Maybe that only works based on rogue's camera class you're using? Maybe I'll try to change to that later if it's absolutely necessary. Here's what I have:

In update:

camera.X = (playerFixture.Body.Position.X * pixelsPerMeter) - (1280 / 2);
camera.Y = (playerFixture.Body.Position.Y * pixelsPerMeter) - (720 / 2);

In draw:

            Matrix cameraMatrix = Matrix.CreateTranslation(-camera.X, -camera.Y, 0);

            Matrix view = Matrix.CreateTranslation(( / 2) / -pixelsPerMeter, ( / 2) / -pixelsPerMeter, 0);
            Vector2 size = (((new Vector2(, / (pixelsPerMeter * 2)) / 1f);
            Matrix projection = Matrix.CreateOrthographicOffCenter(-size.X, size.X, size.Y, -size.Y, 0, 1);



                new Vector2(playerFixture.Body.Position.X * pixelsPerMeter, playerFixture.Body.Position.Y * pixelsPerMeter),
                new Vector2(playerTexture.Width / 2, playerTexture.Height / 2),



Any ideas? Thanks!

Oct 7, 2010 at 6:27 PM

You can try this for the m_View, it is what I use for my camera.

view = Matrix.CreateTranslation(Engine.Camera2D.Position.X/-m_PixelsMeter, Engine.Camera2D.Position.Y /-m_PixelsMeter, 0);

Oct 8, 2010 at 9:09 AM
Edited Oct 8, 2010 at 9:16 AM

Argh, this is so frustrating. I just can't figure this out. I tried plugging in RC's camera class like you and doing things the same way you did but it just doesn't work right. I even tried messing around with yours and the other person's way of doing it in this thread: but that still didn't work right. I can't even get the camera to track my body properly. The camera doesn't keep up with it at all and center it on the screen, and I don't know why. My body just keeps going way off screen and the camera moves really slowly behind it.


camera/body stuff:


            playerFixture = FixtureFactory.CreateRectangle(world, playerTexture.Width/pixelsPerMeter, playerTexture.Height/pixelsPerMeter, 1);
            playerFixture.Body.Position = PositionToWorld(new Vector2(spawnPoint.Bounds.X, spawnPoint.Bounds.Y));
            playerFixture.Body.BodyType = BodyType.Dynamic;

            camera.Position = playerFixture.Body.Position;
            camera.TrackingBody = playerFixture.Body;






            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

            GamePadState currentState = GamePad.GetState(PlayerIndex.One);
            if (currentState.IsConnected)
                // Handle player movement
                if(input.LeftStickPosition.Length() > 0.25f)
                    playerRotation = (float)Math.Atan2(input.LeftStickPosition.Y, input.LeftStickPosition.X);

                        new Vector2((float)Math.Cos(playerRotation) / pixelsPerMeter, (float)Math.Sin(playerRotation) / pixelsPerMeter),


            world.Step(Math.Min((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f, (1f / 30f)));




            view = Matrix.CreateTranslation(camera.Position.X / -pixelsPerMeter, camera.Position.Y / -pixelsPerMeter, 0);
            Vector2 size = (((new Vector2(, / (pixelsPerMeter * 2)) / 1f);
            projection = Matrix.CreateOrthographicOffCenter(-size.X, size.X, size.Y, -size.Y, 0, 1);

            debugView.RenderDebugData(ref projection, ref view);

            //Globals.spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.None, camera.CameraMatrix);


                new Vector2(camera.TrackingBody.Position.X * pixelsPerMeter, camera.TrackingBody.Position.Y * pixelsPerMeter),
                new Vector2(playerTexture.Width / 2, playerTexture.Height / 2),




The result is farseer doesn't line up with my world at all (player texture/body both move, but they're in different places, and my collision layers don't line up with my map like they should), and I can see the debug view moving reeeeaally slowly as I move the player. At one point I did actually have farseer lining up w/my map before I tried using RC's camera class, but I can't even remember how I had it setup. Any ideas? I've been stuck on this for too long and it sucks. :(


edit: Hey, a picture explains it so much better than words can, and I just learned you can take screenshots on xbox using xna game studio device center. Sweet! Here's what's happening:

Oct 8, 2010 at 3:37 PM
you need to add the camera matrix to your spritebatch begin() and
then just draw them normally.
You probably have to convert the body.position back to game units for
it to track properly.
Other than that it looks like it should work just fine.
Oct 8, 2010 at 7:33 PM
Edited Oct 8, 2010 at 7:37 PM

You guys say you are using "Tiled/TileLib"... i couldnt find anything on google(generic names much? hah) and I'm interested to see what features it has. A link would be appreciated. Thanks!

Edit: nevermind... I should have searched codeplex first ;) - this one?:

Oct 8, 2010 at 7:37 PM
TileLib ->
Tiled ->

Oct 8, 2010 at 9:09 PM

vectorj: That's weird. Both "tiled" and "tiledlib" searches return the tiled/tiledlib as the first search result. Different country maybe?



But I did try using camera matrix in my spritebatch begin, as you can see it's commented out above. Not much different. As for converting the body position back to game units, I'm already doing that too in spritebatch.draw() where I do * pixels per meter. But just to be cleaner I changed it to using your function. Tried this code and the results aren't much different =( :



            view = Matrix.CreateTranslation(camera.Position.X / -pixelsPerMeter, camera.Position.Y / -pixelsPerMeter, 0);
            Vector2 size = (((new Vector2(, / (pixelsPerMeter * 2)) / 1f);
            projection = Matrix.CreateOrthographicOffCenter(-size.X, size.X, size.Y, -size.Y, 0, 1);

            debugView.RenderDebugData(ref projection, ref view);

            Globals.spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.None, camera.CameraMatrix);

                new Vector2(playerTexture.Width / 2, playerTexture.Height / 2),


I get this:

vs the previous one:


Oct 8, 2010 at 10:08 PM

can you send your project to me(jrommann at gmail dot com) or post more source code.

I'm not sure why your post above does not draw right.

Oct 8, 2010 at 10:57 PM

Alright, I sent it. Thanks a ton for helping.

Oct 9, 2010 at 4:32 AM

I fixed it and you will not believe what it was. It took me a while to find it, but I found it by starting a new project to make sure the camera was working right since it was off center. The problem you were having was that the camera size was set to 1280x720 but the actual view port was still set to 800x600 because you forgot to do; and that fixed it.

so in your init function you just needed to add;

protected override void Initialize()
    // TODO: Add your initialization logic here = 1280; = 720;;



Merry Christmas :)

Oct 9, 2010 at 6:18 AM

Ahh thank you so much! I can't believe that. You mentioned that you checked to see if the camera was working right because it was off center... well, it still is for me and doesn't track my body properly. My body goes way off screen and the camera is moving so slow and doesn't keep up with it. Might you know why that is?

Oct 9, 2010 at 7:55 AM
Edited Oct 9, 2010 at 7:56 AM

in the camera update method under the tracking body chunk where it
sets _postition = body.postion you just need to convert the bodies
position back to game units. ie _postition = body.position * pixelsMeter

I did that in your project and it tracked just fine.

Oct 9, 2010 at 8:03 AM

You're a genius! Finaaaally, been trying to get this part to work all week and it's finally where I need it to be. Now I can start adding the fun stuff! Wish I had a way to repay you, I feel like I bugged you too much. I'll try to keep beta testing your game some more!

Oct 9, 2010 at 8:07 AM
your welcome and no worries I had the time and I like to help when I
can. Thanks for playing/testing my game.
Oct 12, 2010 at 3:22 AM

Hi, since you guys actually understand how DebugView works....  Lemme ask a few quick questions:

I, too, am using Rogue's camera class.  It has a property "CameraMatrix" which returns a matrix describing both position & zoom (as calculated by the Camera2D object).  I wanted to use this matrix not only with a spriteBatch, but also with DebugView... 

Is that possible?  It looks like DebugView's  RenderDebugData  method wants two separate matricies-- one for projection (does that mean zoom?), and one for view (is that like position?)...  I don't really understand how this RenderDebugData

method works...  I can see that the method then passes the matrices on to a BasicEffect instance...  I've never used the BasicEffect class & know nothing about it...  Any tips?


Oct 12, 2010 at 3:50 AM
Edited Oct 12, 2010 at 4:55 AM

no tips. haven't used basic effect either, but the code below will work if rogue's cam

m_View = Matrix.CreateTranslation(Engine.Camera2D.Position.X/-m_PixelsMeter, Engine.Camera2D.Position.Y /-m_PixelsMeter, 0);
Vector2 size = Engine.Camera2D.CurSize / (m_PixelsMeter*2);
m_Projection = Matrix.CreateOrthographicOffCenter(-size.X, size.X, size.Y, -size.Y, 0, 1);

Trust and abide in the LORD,
Jacob Rommann
Blog -
My video games -

Oct 22, 2010 at 3:08 AM

Preying_V -

The projection matrix only defines how big the screen space is. That is how many units wide and tall your window is.

The view matrix simply lets you translate/scale/zoom/rotate the debug view to your will. Start out with Matrix.Identity at first and then start adding translation and stuff.

I how this helps.