Chris Cavanagh’s Blog

Entries from October 2006

WPF Physics Source

October 30, 2006 · 34 Comments

You can download the source code for my WPF / Physics demo here.  Here’s a very brief overview…

Most of the action takes place in World.xaml and its codebehind file.  It uses an ItemsControl bound to an ObservableCollection, with a DataTemplate for each body class.  Here’s the ItemsControl:

<ItemsControl ItemsSource="{Binding}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
          <Canvas/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

The ItemsControl has its ItemsSource bound to the current DataContext.  The default item container is replaced with a Canvas to allow drawing objects to be positioned anywhere.  Here are a couple of the DataTemplates I’ve got:

<DataTemplate DataType="{x:Type local:RectangleBody}">
    <Rectangle Width="{Binding Width}" Height="{Binding Height}" Fill="#80FF0000" Stroke="#80000000" StrokeThickness="0.01" RenderTransformOrigin="0.5, 0.5">
        <Rectangle.RenderTransform>
            <MatrixTransform Matrix="{Binding DisplayMatrix}"/>
        </Rectangle.RenderTransform>
    </Rectangle>
</DataTemplate>

<DataTemplate DataType="{x:Type local:PolygonBody}">
    <Polygon Points="{Binding Points}" Fill="#80808000" Stroke="#80000000" StrokeThickness="0.01">
        <Polygon.RenderTransform>
            <MatrixTransform Matrix="{Binding DisplayMatrix}"/>
        </Polygon.RenderTransform>
    </Polygon>
</DataTemplate>

RectangleBody and PolygonBody are a couple of custom classes wrapping Newton primitives.  DisplayMatrix is a property that returns a 2D transformation matrix based on the 3D matrix calculated by the Newton engine.  Whenever Newton adjusts the matrix it raises an event.  I catch this and call my PropertyChanged delegate (see the INotifyPropertyChanged interface for details).

Why the ridiculously thin (0.01) StrokeThickness I hear you ask?  I’ve got a fairly good explanation for that (scaling from physical world coordinates to display coordinates), but I’ll wait until someone asks before I try to explain myself…

Anyway, take a look at the source and fire some questions at me.  I’m still amazed how little WPF code & markup it needed to get this going.

Thanks again to Flylio for writing a great C# Newton Dynamics wrapper, which is now even better since I ironed out some bugs and replaced its XNA dependency with a WPF one!

Categories: .NET

Windows Live Messenger – Bad contacts!

October 24, 2006 · 1 Comment

I’ve been getting an intermittent problem with my Windows Live Messenger where it’d get confused about which contacts were online.  It’s been bugging me since the beta of MSN 8, but now it seems there’s a solution of sorts.  Check out this page: http://www.eggheadcafe.com/software/aspnet/28362799/re-xyz-is-not-in-your.aspx.  It seems if you sign out of WLM, then download and open this .REG file (at your own risk of course) your problem will magically go away…

Categories: .NET

WPF 2D Physics!

October 23, 2006 · 36 Comments

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.

Categories: .NET

WPF can induce vomiting

October 12, 2006 · 1 Comment

Windows Presentation Foundation is a marvellous, powerful thing.  With it I have devised a new generation of user interface that can exercise your neck while you work:

Hold on to your breakfast! (.NET 3.0 or Vista only!)

This sick puppy will gently rotate while you type, easing away fatigue and relieving the stresses of the day.

If you’re running .NET Framework 3.0 or Windows Vista, you can see the future today by clicking right here.

Categories: .NET