OdeToCode IC Logo

Swinging In Silverlight

Tuesday, May 29, 2007

Note: this sample is now updated to work with Silverlight 1.0

I decided to catch up with the latest Silverlight release. With the WPF/E release last year I dabbled with Conway's Game Of Life, but instead of porting old code I decided to try something new. During my short stint in the video game industry I found Gamasutra to be a great source of technical articles, and the article "Advanced Character Physics" by Thomas Jakobsen gave me an idea. The article describes the verlet integration algorithm used in molecular dynamics models. The subject matter sounds intimidating, but the algorithm turns out to be relatively simple.

Verlet integration (pronounced vehr-LEY (think French)) doesn't track the velocities of each particle in a system. Instead, Verlet calculates a particle's next position using its current and previous positions. The nice feature of Verlet is that enforcing a constraint (like not allowing a particle to move outside of a wall, or not allowing a particle to move too far away from another, attached particle) is almost as easy as repositioning the particle – at least there is no need to calculate a new velocity.

I built a simple, swinging "rope" in Silverlight. The "rope" is composed of 12 Pluralsight logos. Gravity pulls the rope downward, while a left-to-right breeze can blow the rope across the screen. Credit Ian Griffiths for building the Pluralsight eye in XAML . A static screen shot doesn't show the realistic motion ...

... so if you have the Silverlight 1.0 beta install, try the sample here.

You can download the source code here.

Note to Karl Seguin: don't download the code. This was a late night hack on a holiday weekend. If you break into tears then it's not my fault! :)