Thursday, April 14, 2011

Silverlight 5: 3D Physics Demo

Silverlight 5 introduces new support for hardware accelerated 3D which makes it possible to create truly unique experiences. Unfortunately, the 3D API provided is very low-level, and probably not easily picked up by the average developer.

In this blog post, I’ll show you how to leverage existing libraries and tools to (relatively) easily create a 3D Physics based game or simulation using the new 3D features of Silverlight 5.
[DOWNLOAD SOURCE] [VIEW DEMO] * REQUIRES SILVERLIGHT 5 BETA 1 *

To accomplish our 3d scene and physics, we’ll be using two open source libraries:
1.       Balder 3D Engine: this engine was created by Einar Ingebrigtsen and allows Silverlight to load and display 3D models. Balder has been around for quite some time, but because of the lack of native 3D support in previous versions of Silverlight, performance was very limited. With the addition of native 3D support in Silverlight 5, much more is possible with Balder. 
2.       JigLibX Physics library: this 3D physics engine has had many incarnations. It started as a C++ physics library, and was later ported to C# and XNA. Since there is no official Silverlight version of JigLibX, I created a slightly modified version of JigLibX with thanks to the great start on this work item.
Creating the 3D Models

There are so many different 3D model formats out there, it can be quite overwhelming. 3D Studio (.3DS), Wavefront (.OBJ) and Lightwave (.MDD) are just a few. Balder requires a model be in an ASE format (ASCII Scene Exporter), which is a popular format for 3D game frameworks.
Many tools can convert to ASE format, including the free Blender modeling software. You can download Blender 2.5 Beta here, and there is a Python script for Blender which enables ASE exports here. Also, MilkShape exports to ASE and can import a bunch of different formats and is just $35 US.
Balder also supports texture mapping of ASE models, but just be sure the texture is in either JPG or PNG format (often, modeling software uses a BMP format which Balder cannot display). You also need to make sure the reference inside the ASE file is correct for the texture file.  ASE files are just plain text, so you can open up the file in an editor and look for any BITMAP references like this:
                     *BITMAP "crate.jpg"

Read more: Andybeaulieu.com