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

Xaml interface with farseer physics

Topics: Developer Forum
Nov 12, 2013 at 9:40 AM
Hi everybody,

In a student project, i have to developped a windows phone game and we choose farser physics to our physics implementation.

But for our user interface we use a windows phone projet and XAML code, my question is how to connect a XAML interface to a farseer physics project ?

Thanks by advance and have a good day.
Nov 12, 2013 at 7:57 PM
Farseer is tightly integrated with XNA/Monogame, so easiest thing will be to create a simple Converter-Class between the XNA-specification and the XAML-classes.

Load Farseer and Monogame into your Solution (don't worry, you won't create a Monogame-Project, but you'll need classes such as Vector2 from it).
Then all you'll need is the Converter-Class. Simple example:
public static class XnaXamlTypeConverter
  public static Vector2 ToVector2(this Point pointToConvert)
     return new Vector2(pointToConvertX, pointToConvert.Y);

  public static Point ToPoint(this Vector2 vec2ToConvert)
      return new Point(vec2ToConvert.X, vec2ToConvert.Y);
On word of advise: Don't mix Pixel and Meters! Do every calculation in Meters. Just use Pixels when it comes to the "Presentation-Layer"!
Nov 12, 2013 at 9:21 PM
That is actually not true at all. Farseer is not integrated with XNA nor MonoGame at all. The samples (which are just that... samples) make heavy use of XNA for rendering. The usage of Farseer depicted in the samples though is totally applicable to any other .NET/Silverlight/XAML stuff out there, since Farseer is completely independent of any graphics and rendering specific code.

Just use the plain 'Farseer Physics.csproj' from the Farseer Physics Engine 3.5 download. It provides it own Vector2/3 and MathHelper classes. It is true that the XNA and MonoGame versions use their Vector and MathHelper implementations respectively. That is because those are optimized to use dedicated hardware on certain devices i.e. the XNA Vector stuff is optimized to work well on the Xbox 360, Zune, and Windows Phone 7 plattforms. You won't get this advantage on other plattforms anyway, so just use the software implementations provided within the Farseer download.

For XAML based applications things might work a bit differently, since those don't have a "classic" XNA like renderloop. The only thing that is important though is that you update Farseer regularly with a fixed interval by calling World.step(). Whether you do that in a dedicated thread or by triggering it via an event driven timer or else, is something you have to figure out on your own, since it is probably very application specific.