UPDATES:

- ClickOnce demo available here! (.NET 3.5 SP1 required).
- VS2008 source on CodePlex.

Pavan Podila recently posted about a really cool video on YouTube.  It shows a guy using a SMART Board (or similar) to draw a 2D scene and visualize it in a simulated physics environment.

I’m as much of a geek as Pavan about this kind of stuff, so I thought I’d try to integrate a Physics engine with WPF.  After way too much googling and prototyping, I finally settled on using the Newton Game Dynamics engine.  It’s written in C++, but the demos and flexibility of the thing just blew me away.  I found a pretty good .NET wrapper for it by a guy called flylio (I’ve found & fixed a couple of bugs in it that I’ll need to tell him about).

So far I’ve got some randomly generated static platforms, with random shapes dropped from the top of the screen.  It’s really cool and disturbingly addictive to watch them bounce and roll about.

Some notes:

  • Newton is a 3D physics engine but I needed to get it to work well in 2D (I tried 2D engines but none of them came close to Newton for accuracy, speed or stability).  After some disappointing tests (involving an ‘invisible wall’ behind and in front of the scene) I settled instead on attaching a handler to Newton’s ‘body matrix changed’ event.  In it I just cleared the Z values and crossed my fingers.  It worked!
  • Newton can work with primitive objects like cylinders, cones, boxes etc. as well as arbitrary convex hulls (nicely represented as "vertex clouds").  This is great combined with WPF, because it means ‘real’ shapes can be used to represent round primitives (better to have a single-sided ellipse than a 30+ sided polygon).
  • WPF data binding rocks.  The whole scene is just an <ItemsControl> with a <Canvas> inside it (using <ItemsPanelTemplate>) bound to an ObservableCollection.  The actual bodies are defined as <DataTemplate> elements.
  • The shapes all look awesome thanks to WPF’s anti-aliasing!

 

Next I’ll try to add a block car with lumpy wheels, just like the video!

Obviously this is only half what I want to achieve.  Getting a designer for this stuff working in WPF should be an interesting ride!

UPDATE: Removed dependency on XNA Framework.  Now has a car!  Click the image above for demo.

About these ads