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

Problem in adjusting physics property of image - Need to assign shape

Topics: Developer Forum, User Forum
Jul 4, 2012 at 8:04 AM


I am a newcomer in Farseer but have worked on box 2d before. Clearly, there is major difference in the way i need to code for Farseer for windows 8. I have some basic
questions which i am not able to find anywhere so if any one can help me or give us links to study, it would be great.

1) In Farseer engine, what are the units of properties? Means if i give 1 as mass, what is unit for that?

2) I need to assign physics properties to image, suppose a box. I am assigning following values.
    Image is of 32*32 size.

    Mass = 2000
    Friction = 0.1f
    Restitution = 0.5f
    Inertia = 5000           

    Note: I have calculated inertia based on following formula of Mass, height and weight but i am doubtful for this.
        (mass * (square of(width of object)+square of(height of object))) / 12 which give me 1260833.33f     which i thought was wrong.

    Have I put all correct? I can not assign the Density directly so i have put Mass. If any property wrong or If i have missed putting any property.

3) Can I assign shape to image since that will be critical for curve or other images so react properly?
Please guide me for this since i am lacking few things before i can start my project.

Thanks in advance.

Jul 4, 2012 at 10:35 PM


Farseer is a port of Box2D. Everything is pretty much the same. I have no problems using c++ code with minor changes (like properties instead of getters/setter but that's just syntactic sugar).

1) Farseer uses the mks unit system just as Box2D so for mass 1 means 1 kg

2) You can see for example ComputeProperties() in Collision/Shapes/PolygonShape.cs for how Farseer calculates mass properties.

3) Image is a "graphical" concept while "shape" in farseer/Box2D is a "physics" concept so the two have nothing to do with each other. How you draw your collision shapes is up to you in your drawing code. If you decide to draw a rectangle collision shape as a sprite (using for example XNA's SpriteBatch) it's quite easy. Just remember XNA uses a coordinate system with 0,0 at top left and y increaising down whereas farseer uses 0,0 at the center y increasing up. But you can automatically convert all that with a proper projection matrix. Also the Position property of the body refers to the position of the center so you should pass the offset parameter to the SpriteBatch (that would be a new Vector2(width/2, height/2) for a rectangular sprite) to draw the sprite at the correct location.

I believe you would also have a problem with scale since you want to use pixels as units. The engine is best tuned in the 0.1-10 scale for things. You should decide on a scaling factor to apply to/from the graphics stuff. Check the ConvertUnits.cs class in the Samples project which incidentally also contains a lot of useful code including a nice screensystem, camera and an asset manager for managing sprites.

Jul 5, 2012 at 5:59 AM

Hi Jerry,

Thanks so much for this detailed answer. One thing I would like to mention is that I am working on Windows 8 metro style app development. Secondly, I am seriously facing issue in finding the Inertia of the Image object. If I remove this property, the object goes out of screen.

We are facing scaling issue. Our current calculation is 50 pixels = 1 meter. Is this correct? If so, can you please give us Inertia based on following?

Image is of 32*32 size.

    Mass = 2000
    Friction = 0.1f
    Restitution = 0.5f

Are these 3 properties correct?

Thanks again for the reply and hope that I will pass this issue with help of you.



Jul 5, 2012 at 11:01 PM


There is no "correct" value for the pixel-to-meter ratio. You decide it. It's recomended (not to mention useful), though, for it to be physically accurate. For example if your character is human he/she should be about 1,7m tall. As another example in my simulation I need very thin objects (like fractions of a centimeter) and my real world dimensions are about 2m x 1m. So I've decided that 1 Farseer meter will correspond to about 1cm for the real world. This allows me to keep stuff at the sweet spot of about 0.1-10 scale which correspond to 1mm to 10 cm in the real world. It's not just dimensions and positions but mostly this helps velocities and accelerations and joint stability.

Same is true for friction and restitution - these are arbitrary values decided by you according to what you need to simulate. 0.1 friction is like ice. 0.5 restititution means half the energy of collision is lost so it's quite inelastic (I'd picture it like a soccer ball hitting tall grass rather than cement).

I don't get why you need to set Inertia directly (or mass for that matter). Are you having some specific needs based on the thing you need to simulate? Or does Win8 force you to set those explicitly?

If you give density to your shape it will calculate all values  for you. Give it a 1 density and see what comes out and  if needed increase/decrease the density. Or run through Shape.ComputeProperties() to find out a value that you can use for setting all the propeties manually. If you need to dynamically change the density of shapes during runtime call Body.ResetMassData().

I'm having the feeling you are creating the bodies in some non standard ways. Box2D and Farseer give way too much freedom but that's not always a good thing. Check BodyFactory for helper methods.

Jul 6, 2012 at 12:20 PM


I want to achive like this. I want to have a game so I need to put images within game using silverlight image control. Suppose it a image of box, i put that and i need
to assign rectangle so they sync together.

I do it like following, now my question is, how can I assign the Density to "Test_PhysicsSprite" PhysicsSprite since if i assign properties to image like mass, interia and other, it does not react properly. I know i am missing something but not able to find out.

<Canvas Height="100" Width="100" x:Name="LayoutRoot">
    <FarseerHelper:PhysicsCanvas x:Name="Test_PhysicsCanvas" Background="White" Width="1366" Height="768" MousePickEnabled="true">
        <FarseerHelper:PhysicsSprite Height="100" Width="100" x:Name="Test_PhysicsSprite">
            <Rectangle x:Name="Test_Rectangle" Height="100" Stroke="Black" Width="100">
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="LightBlue" Offset="0.004"/>
                        <GradientStop Color="#FF2574AA" Offset="1"/>
               <Image Height="100" Source="Test_Image_100X100.png" Stretch="Fill" Width="100"/>


Jul 6, 2012 at 9:53 PM

Ok I think I get it now. You are using this: 


You should check their demos to see how they set the things up. From a quick glance they set the physics in the loaded event handler in the code behind. 

Jul 7, 2012 at 11:51 AM


I have figured out few things but the main issue is that the different density i have applied to different wait boxes, it is not taking any effect. Things are almost same.

If I assign different density to different objects, it should react differently but in my case, it does not. Here is the updated code for assigning shape to image and creating fixture for same.

            List<Vector2> L= new List<Vector2>();
            L.Add(new Vector2(0.5f, 0.5f));
            L.Add(new Vector2(0.5f, -0.5f));
            L.Add(new Vector2(-0.5f, -0.5f));
            L.Add(new Vector2(-0.5f, 0.5f));

            Vertices v = new Vertices(L);

            PolygonShape poly = new PolygonShape(v, 10f);
            Fixture f1 = physicssprite.BodyObject.CreateFixture(poly);
             UIShapes.Shape SS;
             u = new Utility();
             SS = physicssprite.BodyObject,  physicssprite, new WinUI.Color() { A = 0, R = 0, G = 0, B = 0 });

             f1.Body.Friction = 1;
             f1.Body.Restitution = 0.3f;


As you can see, i have given density 10f to Polygon shape, which is fixtured with actual box image. Any idea? The density is not reacting at all.

Thanks for all your help till now.


Jul 7, 2012 at 12:23 PM
Edited Jul 7, 2012 at 12:25 PM

On first glance you are creating the poly in clock-wise order while it should be in counter-clockwise. v.IsCounterClockwise will be false. Here's the helper method from PolygonTools.CreateRectangle:


       public static Vertices CreateRectangle(float hx, float hy)
           Vertices vertices = new Vertices(4);
           vertices.Add(new Vector2(-hx, -hy));
           vertices.Add(new Vector2(hx, -hy));
           vertices.Add(new Vector2(hx, hy));
           vertices.Add(new Vector2(-hx, hy));

           return vertices;


I don't know if this is related but it will create problems in the future and messes up a lot of calculations.

Now back to the issue setting a different density will give you a different mass. Check Body.Mass. I have no experience with the library you're using but perhaps it creates a "default" body with default fixture and the new one is added. Just in case try Body.ResetMassData() after and see if the mass changes.