Source Code available here:
It took me awhile to realize that the latest Physics Helper release posted on codeplex, Physics Helper 18.104.22.168 Alpha doesn’t support Silverlight 4. I couldn’t figure out why the physics behaviors were not available in new projects I opened in Blend 4, but if I opened the demo projects that come with the download, and referenced the same Physics Helper libraries, there they were. Long story short, the new Blend 4 projects couldn’t see the behaviors whereas the existing Blend 3 projects could. About the time I realized this, Andy Beaulieu posted a new set of source code, as Change Set 54255, with the comment “Fixed Silverlight 4 Compatibility in Blend 4” and all was well.
As I’m working on adding Physics to ooNaWorld, I’d like to encourage my potential audience to download and utilize the Physics Helper libraries for Silverlight 4.
So, to get started, go to Change Set 54255 download page and click on the Download link.
You don’t even have to open the solution, but if you do you want to open this one:
Which I craftily deduced targets Silverlight 4. The important thing is to reference the correct libraries, which I found out by looking in the project, but which I will tell you are in ClientBin4/debug.
So, I’m going to create a new Blend 4 project, reference the libraries, and build a simple two-wheeled cart using the PhysicsJoint UserControl. Here goes:
Add the references:
Select these assemblies:
Now, the first thing to do is to change LayoutRoot type to canvas. You’ll want to tattoo this on the foreheads of your loved ones. When things don’t work, always assume that you forgot to change LayoutRoot type to canvas, because believe me, there’ll be times when you’re sure you did, but you didn’t.
All the graphics are 2D, so typically you’ll have a backdrop, such as the sky, and a platform, such as the ground. We’ll use the LayoutRoot fill color as the backdrop, so set it to the color of your choice. I’m going to use a simple rectangle object as the ground, and I’ll set its fill color to green, and name it ‘ground’. And let me make the point right here that you always want to give names to the objects you expect to take part in physics behaviors. So far I’ve found that’s pretty much all or nothing, so you’ll want to name everything.
We have our virtual world, now let’s add some physics. We need a PhysicsController UserControl, so let’s add one:
1. Click on the Assets tab.
2. Click on Locations.
3. Click on Spritehand.FarseerHelper.dll.
4. Grab PhysicsController with the mouse and drag…
5. … onto LayoutRoot.
Next we set the PhysicsController properties:
1. Select [PhysicsController] in Objects and Timelines.
2. Select the Properties Tab.
3. Open the Physics Properties
4. Set GravityVertical to 500.
5. Set MousePickEnabled on. This allows you to drag objects around the screen.
We also need a PhysicsStaticHolder user control. When things fall, they need something to land on; you use the PhysicsStaticHolder to indicate which UIElement that is.
Do the same steps as for the PhysicsController except you select the PhysicsStaticHolder control and drag it onto LayoutRoot.
And set the PhysicsController body property…
1. Give the Rectangle object a name like ‘ground’.
2. Select the [PhysicsStaticHolder]
3. Select the Properties tab.
4. Select the Physics Properties
5. Set the Body property to ‘ground’.
Ok, at this point you can see if gravity is working. Draw an ellipse somewhere above the surface of the ground and name it ball. Run the application and the ball should fall to the ground.
Ya know, (there was a DJ on KMET in the 70’s who used to start out ‘Ya Know’ and just launch into these amazing raps, or so they seemed at the time. The seventies were great – then I became a programmer). But anyway, Ya know, humans are funny. I fired this up and got a runtime error. You’d think the first thing I would do would be to check if my LayoutRoot type was Canvas. But Noooooooooo!!!!!!!! (See what I mean, from the seventies and always funny). No, I actually ran it through the debugger, which was no help at all. Some eventhandler pointer was set to null in generated code. Then I remembered what I was always supposed to do in this case, so I called my wife over and read what it said on her forehead. And, confident that this time it was something else, sure that it was going to say ‘canvas’, I ran my mouse over LayoutRoot... You know that sound Sideshow Bob makes when the rake hits him in the face on the Simpsons – that sound went off in my brain. ALWAYS CHECK THAT FIRST!!!
Or maybe it’s just me.
So here’s the ball in the air and on the ground. Fascinating.
But what’s a two-dimensional ball if not a wheel? Let’s attach a chassis and another wheel.
Be sure and give them names.
Now we attach the joints.
1. Select Locations under the Projects tab.
2. Select the Spritehand.FarseerHelper.dll.
3. Select the PhysicsJoint UserControl.
4. Place it on one of the wheels.
Do the same for the other wheel.
If you hold down the alt key you can drag a copy of the first joint.
Now to hook them up.
Select the PhysicsJoint and set the properties as shown. Make sure you set the CollisionGroup to something other than zero. Set the other PhysicsJoint to connect the other wheel and use the same CollisionGroup.
Now try it out. You can drag the cart back and forth. The wheels rotate and unless you’re really precise the cart rocks up and down because the joints aren’t centered. You can even make it do wheelies.
Now let’s look at another use for the PhysicsJoint, namely as a hinge with a spring.
Create a support and platform as shown. Add a PhysicsJoint and set the properties. This time set AngleSpringEnabled on and set the collision group to 2. Run it and see what happens.
There’s a whole lot more you can do. It’s fascinating, usually hilarious, and sometimes disturbing to watch your mistakes writhe around the screen. Or flail as you yank them back and forth, back and forth… Check out the demos that come with the download, then monkey with them and watch what happens. Hours of demented fun and no one gets hurt, or calls the cops. Also see the tutorial videos, which cover a previous version but are still valid. In particular they cover the PhysicsJoint behavior, Fluid Container, and accessing objects in code-behind.
I’m just getting started with Physics Helper. I hope to share more as I start applying it to ooNaWorld.
Thanks Andy, glad you like it. My name is Richard however - Michael is my mentor and the reason Silverlight has become my 'thing'. <br><br>Cool on the behaviors, I'll use them from now on. I went with the user control because that's what all the demos used. I actually used the PhysicsJoint behavior when I created the project you walthrough in your video with the truck.
Looks good Michael!<br><br>I noticed that you're using the UserControls instead of the newer Behaviors. These work fine but you might want to use the Behaviors for any newer stuff because "eventually" I will probably phase out the UserControls.<br><br>As a side note, the Physics Helper was created in Silverlight 2, before Behaviors were created, and it used UserControls. But since Silverlight 3, they've had Behaviors as well which are now more in vogue :)