Chris Cavanagh’s Blog

Entries from November 2007

Physics demos source code

November 20, 2007 · 1 Comment

Most of the source code for my physics demos is available on CodePlex (except for the WPF 2D one; I’ll get that moved at some point).  To save you from having to hunt around I’m listing the main ones here.  Enjoy! smile_teeth

Silverlight 1.1 2D (wheels)

One of my first Silverlight efforts, using the BulletX library; as soon as I get some time I’ll fix the irritating performance problem (stuttering frame rate), I promise!

Demo: http://www.chriscavanagh.com/Chris/Silverlight/Physics2D-1/TestPage.html
Source: http://www.codeplex.com/SilverlightPhysics

WPF/XBAP 3D (beachballs)

One of my favorites (I like the specular highlights smile_regular) using the BulletX library.  Don’t forget you can rotate the whole scene by dragging, or zoom in/out by right-dragging.

Demo: http://www.chriscavanagh.com/Chris/CJC.Bullet/BulletDemo1.xbap
Source: http://www.codeplex.com/XBAP3DPhysics

WPF / Flade (controllable car)

The already awesome Flade engine demo, running as an XBAP.

Demo: http://www.cubpack99.com/pack99/cjc.dynamicsengine.xbap
Source: http://www.chriscavanagh.com/chris/CJC.DynamicsEngine.zip

WPF 2D (car, eggs)

Based on the Newton Dynamics engine.  This would be sweet as an XBAP, but would need to use another (.NET based) physics engine such as BulletX

Demo (EXE): http://www.chriscavanagh.com/Chris/Source/WPFPhysics1_exe.zip
Source: http://www.chriscavanagh.com/Chris/Source/WPFPhysics1.zip, http://www.chriscavanagh.com/Chris/Source/NewtonDynamics.zip

Categories: .NET · WPF

"Xbox 5ive" all-nighter

November 11, 2007 · Leave a Comment

Check out what’s happening as part of Xbox Live’s 5th birthday celebrations.  I’m all set for a late night of kicking ass and talking trash; let me know if you’ll be online! smile_teeth  My gamertag is Bob Strogg USA; you can get Return to Castle Wolfenstein from Gamestop for $4.99 so no excuses! smile_wink (it runs great on the ‘360 too).

Categories: Games

Ask your doctor if AJAX is right for you

November 7, 2007 · 3 Comments

AJAX is not for everybody.  May cause dizziness, strong urge to curse and punch colleagues, and brain death.

Categories: .NET

Convert.ChangeType extension

November 5, 2007 · 1 Comment

Back in 2006 Peter Johnson discovered .NET 2.0’s Convert.ChangeType didn’t support nullable types.  He wrote a nice wrapper to help out.  To make it a little more “3.5″ I figured it deserved an extension method (note you’ll need Peter’s code too):

    public static class ExtensionMethods
    {
        public static T ChangeType<T>( this object value )
        {
            return (T)ChangeType( value, typeof( T ) );
        }
    }

In addition I tweaked Peter’s code slightly to make his non-generic ChangeType an extension method too (note I removed the comments here for brevity; you should get Peter’s code instead as it’s more readable):

    public static object ChangeType( this object value, Type conversionType )
    {
        if ( conversionType == null )
        {
            throw new ArgumentNullException( "conversionType" );
        }

        if ( conversionType.IsGenericType &&
          conversionType.GetGenericTypeDefinition().Equals( typeof( Nullable<> ) ) )
        {
            if ( value == null ) return null;

            NullableConverter nullableConverter = new NullableConverter( conversionType );
            conversionType = nullableConverter.UnderlyingType;
        }

        return Convert.ChangeType( value, conversionType );
    }

Now you can be really lazy with code like this:

    var myInt = myStringOrWhatever.ChangeType<int>();

    int myOther = myStringOrWhatever.ChangeType( typeof( int ) );

    Type myType = ...;
    object myTypedThing = myStringOrWhatever.ChangeType( myType );

If you’ll always know the type you’re converting to at compile time, you might prefer to use the conversion methods in System.ComponentModel such as DecimalConverter.

Categories: .NET

LINQ Expression Visitor

November 4, 2007 · Leave a Comment

For a recent LINQ-based project I needed to merge branches of multiple expression trees into one.  Normally this doesn’t cause any issues, but I wanted to dynamically merge multiple ‘Where’ predicates together with arbitrary logical operators (hope that’s the right term; it sounds about right).  I found a great base class called ExpressionVisitor on Matt Warren’s blog.  Always keen to reduce the amount of typing (and thinking) I need to do, I couldn’t resist making a couple of helpers.  The first allows you to use syntax like this (don’t worry, it gets better…):

    MethodCallExpression call = ...;

    call = ExpressionVisitor<ParameterExpression>.Visit(
        call,
        p => p.Type == typeof( T ) ? elementParam : p ) as MethodCallExpression;

Basically it lets you specify a delegate to visit (and change) a single expression type in the tree.  Here’s the code (you’ll also need Matt’s ExpressionVisitor source):

    public class ExpressionVisitor<T> : ExpressionVisitor where T : Expression
    {
        Func<T, Expression> visitor;

        public ExpressionVisitor( Func<T, Expression> visitor )
        {
            this.visitor = visitor;
        }

        public static Expression Visit(
            Expression exp,
            Func<T, Expression> visitor )
        {
            return new ExpressionVisitor<T>( visitor ).Visit( exp );
        }

        public static Expression<TDelegate> Visit<TDelegate>(
            Expression<TDelegate> exp,
            Func<T, Expression> visitor )
        {
            return (Expression<TDelegate>)new ExpressionVisitor<T>( visitor ).Visit( exp );
        }

        protected override Expression Visit( Expression exp )
        {
            if ( exp is T && visitor != null ) exp = visitor( (T)exp );

            return base.Visit( exp );
        }
    }

As a refinement I added a couple of extension methods for more compact syntax.  For non-generic types you can do this:

    MethodCallExpression call = ...;

    call = call.Visit<ParameterExpression, MethodCallExpression>(
        p => p.Type == typeof( T ) ? elementParam : p );

For generic types you can do this:

    Expression<Func<Profile, bool>> call = ...;
    call = call.Visit<ParameterExpression, Func<Profile, bool>>(
        p => p.Type == typeof( T ) ? elementParam : p );

(However I’d rather the Func<Profile, bool> wasn’t needed in the call to Visit<>() here…  I’d hoped it could infer the TDelegate type and let me just specify the ‘T’ type, but it seems it can’t do both together smile_sad.  Maybe I’m missing something obvious, or perhaps it’s a beta 2 bug?  Feedback welcome!).

Here’s the code for the extension methods:

    public static class ExpressionExtensions
    {
        public static Expression Visit<T>(
            this Expression exp,
            Func<T, Expression> visitor ) where T : Expression
        {
            return ExpressionVisitor<T>.Visit( exp, visitor );
        }

        public static TExp Visit<T, TExp>(
            this TExp exp,
            Func<T, Expression> visitor )
            where T : Expression
            where TExp : Expression
        {
            return (TExp)ExpressionVisitor<T>.Visit( exp, visitor );
        }

        public static Expression<TDelegate> Visit<T, TDelegate>(
            this Expression<TDelegate> exp,
            Func<T, Expression> visitor ) where T : Expression
        {
            return ExpressionVisitor<T>.Visit<TDelegate>( exp, visitor );
        }
    }

I’m sure there’s scope for other enhancements too… smile_regular

Categories: .NET