Chris Cavanagh’s Blog

Entries from June 2008

Silverlight soft-body physics

June 24, 2008 · 10 Comments

UPDATE 2: Updated for Silverlight 2 RTW.  It now uses the CompositionTarget.Rendering event (haven’t updated source yet, but it’s really easy :) ).

UPDATE: Source code now available here.

Here’s a simple soft body physics demo based on Walaber’s excellent JelloPhysics library.  It enjoys my trademark boring user interface (umm, so it doesn’t detract from the coolness of the physics):

image

Click the image above or follow this link (you’ll need Silverlight 2 beta 2).  If the platforms are scattered too much, just click the “Move platforms” button.

JelloPhysics is also the engine behind Walaber’s awesome Jellycar game (PC version here).

Source code will be available once I’ve made a few improvements :)

Enjoy!

Categories: .NET

LINQPad – Copy HTML to clipboard

June 23, 2008 · 1 Comment

Here’s a useful bit of C# you can paste into your LINQPad statements to quickly hack together some HTML (could be useful for quick ad-hoc database reports):

Action<string, string> HtmlToClipboard = delegate( string htmlFragment, string title )
{
    Func<int, string> To8DigitString = delegate(int x) { return String.Format("{0,8}", x); };

    if (title == null) title = "From Clipboard"; 

    var sb = new System.Text.StringBuilder();
    var header = @"Format:HTML Format
Version:1.0
StartHTML:<<<<<<<1
EndHTML:<<<<<<<2
StartFragment:<<<<<<<3
EndFragment:<<<<<<<4
StartSelection:<<<<<<<3
EndSelection:<<<<<<<3
";

    var pre = @"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">
<HTML><HEAD><TITLE>" + title + @"</TITLE></HEAD><BODY><!--StartFragment-->";

    var post = @"<!--EndFragment--></BODY></HTML>";

    sb.Append(header);
    int startHTML = sb.Length;

    sb.Append(pre);
    int fragmentStart = sb.Length;

    sb.Append(htmlFragment);
    int fragmentEnd = sb.Length;

    sb.Append(post);
    int endHTML = sb.Length;

    sb.Replace("<<<<<<<1", To8DigitString(startHTML));
    sb.Replace("<<<<<<<2", To8DigitString(endHTML));
    sb.Replace("<<<<<<<3", To8DigitString(fragmentStart));
    sb.Replace("<<<<<<<4", To8DigitString(fragmentEnd));

    string data = sb.ToString();
    System.Windows.Forms.Clipboard.Clear();
    System.Windows.Forms.Clipboard.SetText(data, System.Windows.Forms.TextDataFormat.Html);
};

Note you’ll need to add a reference in LINQPad (Query -> Advanced properties) to System.Windows.Forms.dll (it uses the Clipboard class). For more detail on why it jumps through so many hoops, read this post by Mike Stall.

Categories: .NET

Silverlight 2 2D Physics updated

June 16, 2008 · Leave a Comment

UPDATE 2: Updated for Silverlight 2 RTW.  It now uses the CompositionTarget.Rendering event (haven’t updated source yet, but it’s really easy :) ).

I’ve updated my Silverlight 2D Physics demo again, this time for Silverlight 2 Beta 2. You can preview it here and get the source here.  Although all I did was recompile it, at some point in the past I exposed a bug in the BulletX physics engine (previously it had a workaround [hack] to prevent it blowing up).  You might get the occasional “Index out of bounds” error and weird behavior (the wheels/heads might fall through the platforms, then throw you to a blank page!).  I’ll update here when it’s fixed :)

Categories: .NET

SkinBuilder and data binding

June 3, 2008 · Leave a Comment

I’ve updated my WPF SkinBuilder utility to fix a couple issues it had with data binding.  The fix involved WPF’s equivalent of “Application.DoEvents”.  This might be the most compact version of it yet:


Dispatcher.CurrentDispatcher.Invoke( DispatcherPriority.Background, (Action)delegate {} );

Basically it just blocks operation until all important processing is complete (anything with a DispatcherPriority higher than ‘Background’).

Categories: .NET