Windows Universal apps Getting Started
The current release was created using
Visual Studio 2013 Update 2.
Sample Project
Once you have a development environment and hardware setup, you can
download the source code and check out the sample project inside the solution.
- FarseerPhysics contains a Portable Class Library for the Farseer Physics Engine, slightly tweaked to run in WinRT.
- Spritehand.PhysicsHelper contains a Portable Class Library for the Physics Helper core controls. These wrap the physics engine in familiar XAML controls.
- Spritehand.PhysicsHelper.Demos contains a sample Universal Windows app:
- a Windows 8.1 app project
- a Windows Phone 8.1 app project
- a Shared project which contains all of the core game components.
Trying the Demo
Set the Windows app as your default project - right click Spritehand.PhysicsHelper.Demos.Windows and select "Set as startup project". This demo shows how to dynamically add and delete user controls, track performance, and use nested Storyboard animations inside physics objects. Here are the main highlights of the advanced demo:
- the code-beind file MainPage.xaml.cs shows how to handle the TimerLoop and Collision events raised by the PhysicsCanvas.
- The AddAnItem method shows how to dynamically add a complex UserControl containing multiple sprites and joints.
Shape Types
PhysicsSprite objects contain a ShapeType property which defines the boundary shape for collisions. The default ShapeType for a PhysicsSprite is a Rectangle, but if we needed a circular or polygon shape, we can change the ShapeType property to Ellipse or Polygon.
Here is an example of defining a circular sprite using ShapeType Ellipse:
<ph:PhysicsSprite x:Name="ball" Canvas.Left="350" Width="100" Height="100" ShapeType="Ellipse" RestitutionCoefficient="0.8">
<Ellipse Width="100" Height="100" Fill="LightYellow"/>
</ph:PhysicsSprite>
If we need a more exact (polygon) shape, we can accomplish complex shapes by defining a Path element as the
first element in our PhysicsSprite, and setting the ShapeType to Polygon. Note that you could set the Visibility of this boundary sprite to Collapsed if you have a complex design inside the PhysicsSprite that you are defining the basic boundary for.
<ph:PhysicsSprite Height="78.667" x:Name="cnvBody" Width="197" Canvas.Left="10" Canvas.Top="0" ShapeType="Polygon">
<Path x:Name="bodyBoundary" Fill="Red" Data="M2,72 L196,76 L190,43 L136,28 L123,2 L84,2 L83,28 L2,32 z" Height="75"
Canvas.Left="1.5" Stretch="Fill" Stroke="#FF000000" Canvas.Top="1.5" UseLayoutRounding="False" Width="195">
</Path>
</ph:PhysicsSprite>
- To debug shape types that the simulation is using visually, we can set DebugMode="true" on the PhysicsCanvas. This draws a handy outline on top of each physics element and is handy when you are seeing collision problems.
Accessing Farseer Objects
Under every PhysicsSprite is a Farseer Body and Fixture elements (to learn more about Farseer and see docs please visit
farseerphysics.codeplex.com).
- All PhysicsSprites defined inside the PhysicsCanvas are accessible through the PhysicsCanvas.PhysicsObjects dictionary. We can get to the Farseer BodyObject to change things such as Mass, Restitution, and even the shape (Fixtures) of the object.
PhysicsSprite spr = cnvGame.PhysicsObjects["ball"];
spr.BodyObject.Restitution = 0;