Project Description

The Physics Helper for Blend, Silverlight and Windows Phone 7 contains several Behaviors which allow you to draw objects in Expression Blend 4, and have those objects translated into Physics objects using the Farseer Physics Engine. This can be a great timesaver for creating games, as it is traditionally difficult to line up underlying physics bodies and geometries with your Blend artwork.


NOTE: INTERESTED IN WINDOWS 8 METRO DEVELOPMENT?
Check out the port of the Physics Helper to Physics Helper XAML for Metro style app development in C# or VB.

New to Version 4 of the Physics Helper:
  • Now supports Farseer Physics Engine 3
    • Bullets
    • Continuous Collision Detection
    • Resting Bodies
    • see the Farseer project page for more details on this release of the Farseer Engine!
  • Installer (msi) makes setup easier

Intro Videos

Watch these short videos to see how easy it can be to design physics using Expression Blend 3, Silverlight 3, and the Physics Helper Behaviors:

VIDEO ON VERSION 4.0 VIEW
This is an intro video on using the Behaviors to apply Physics in Expression Blend.

VIDEO ON VERSION 3.0 VIEW
This is an intro video on using the Behaviors to apply Physics in Expression Blend.

VIDEO ON VERSION 3.0 "JOINTS + PROGRAMMING" VIEW
A more advanced video on using Behaviors to create Joints and accessing objects via code-behind.

CODING4FUN SHOW VIEW
Brian Peek interviews Andy Beaulieu on the Physics Helper. Note that this demos Version 1, which is based on the User Controls instead of Behaviors.

MIX09 SHOW OFF! VIEW
The Physics Helper Library took 1st Place at the 2009 MIX Show Off! Event. This is the winning video.

Demos

These demos were created with minimal or no coding using the Physics Helper Behaviors:

DEMOS for Version 4.0

DEMOS for Version 3.0


Other Documentation Sources

*Farseer Physics Manual
*Creating a Shuffleboard Game for Windows Phone 7 Using Silverlight
*Creating a Pinball Game in Silverlight

Notes

  • run the .msi to "install" the Behaviors into Blend 4
  • If you want the demo source code, download the latest Change Set under "Source Code"
  • remember to use Canvas layout containers, NOT Grid containers!
  • when using Raster Images as Physics Objects, be sure that you have a Clean Outline. That is, make sure there are no stray pixels in the background - because the boundary detection algorithm must be able to "trace" the complete outline of the image.
  • if your boundaries are not being determined correctly, it may be because of Rotate or Scale Transforms that you applied in Blend. To work around this issue, Group your element into a Canvas and then apply the Physics Behaviors to the Canvas instead of the shape. You can easily Group things into a Canvas in Blend by right-clicking the object and selecting Group Into/Canvas.
  • Remember you can set DebugMode on the PhysicsController to see the boundary determined by the helper.

Coding Techniques

Behaviors are great, but it would be difficult to complete a full game using only the behaviors included in the library. So at some point, you will need to add code to do AI, control levels and game state, and other tasks. To do this more advanced coding you will need to get access to the underlying Physics simulation and Physics bodies. Here are some pointers to get started with that:

Once you drop a PhysicsController Behavior onto your main Canvas, you can later get a reference to that Physics Controller (the object that contains the simulation context) in code. Suppose your main Canvas is named "LayoutRoot" then you can get a reference as follows:

PhysicsControllerMain _physicsController = LayoutRoot.GetValue(PhysicsControllerMain.PhysicsControllerProperty) as PhysicsControllerMain; 


After you have a reference to the PhysicsController, you can modify any of the Farseer Physics Geometry or Body objects by getting a reference through the PhysicsObjects dictionary. You can also get a reference to the main Farseer Physics simulation to change things such as gravity (for details on the classes provided by the Farseer Physics Engine, go here):

_physicsController.PhysicsObjects["ball"].BodyObject.IsStatic = false;
_physicsController.Simulator.Gravity = new Vector2(0, 500);


In the newer versions of Farseer Physics, some properties are nested inside Fixtures - which are the geometry shapes used for collision. You can get at these through the FixtureList...

            foreach (PhysicsSprite spr in _physicsController.PhysicsObjects.Values)
            {
                    foreach (Fixture f in spr.BodyObject.FixtureList)
                    {
                        // grapple hook should collide with both bottom and boxes
                        f.CollisionCategories = Category.Cat2;
                        f.CollidesWith = Category.Cat1;
                    }                
            }


You can also handle events, such as the Collision event, raised by the PhysicsController when two physics geometries collide:

    _physicsController.Collision += new PhysicsControllerMain.CollisionHandler(_physicsController_Collision);

    void _physicsController_Collision(PhysicsSprite sprite1, PhysicsSprite sprite2)
    {
        if (sprite1 == sprShip && sprite2.Name.StartsWith("asteroid"))
        {
            // blow up the ship
        }
    }


... and you can also get a reference to the oringal XAML UI Element using the uiElement property of the Physics Object:

Ellipse ball = _physicsController.PhysicsObjects["ball"].uiElement as Ellipse;
ball.Fill = new SolidColorBrush(Colors.Red);




The Physics Helper is currently in Beta and is still a work in progress. You can find out more at Andy Beaulieu's blog: Here and Here

Last edited Sep 24, 2011 at 4:15 PM by AndyBeaulieu, version 49