#.think.in
learn.create.enjoy

#.think.in infoDose #38 (20th July - 27th July)

July 27, 2009 21:53 by brodie

Architecture

Developer

Designer

Silverlight

Web/ASP.NET MVC

WPF

Utilities

Singularity Watch

Other

Fun


Tags:
Categories: Links
Comments (0)

Python and Farseer: 2d Physics in Silverlight

July 19, 2009 16:59 by tarn

I’ve got a tiny Acer Aspire “netbook” I use while commuting to work. It’s a very low spec’d machine but it has a very functional keyboard and I love programming on it!

I think I enjoy programming on it because it doesn’t have Visual Studios installed. I have the Windows SDK so I can build .NET projects with MsBuild, but I mainly use it for developing Python code. With some free tools and notepad++ plug-ins its a great light weight environment for coding simple Django and Google App Engine web applications and dynamic language .NET applications.

I’m not really into computer gaming, but I do like 2d physics games. I remember losing days work when I first found Magic Pen. I decided to try using the cool Farseer Physics Engine, which is described on the Codeplex page as; 

The Farseer Physics Engine is an easy to use 2D physics engine designed for Microsoft’s XNA and Silverlight platforms. The Farseer Physics Engine focuses on simplicity, useful features, and enabling the creation of fun, dynamic games.

Getting started

The Farseer engine is written in C# and I found once I’d installed the Silverlight SDK I could use MSBuild to build the Farseer engine that targets the Silverlight runtime. The Farseer engine doesn’t itself reference anything Silverlight specific, but it does need to be built against the limited .NET framework available in Silverlight.

I’ve previously blogged about writing dynamic language Silverlight applications; the Silverlight Dynamic Languages SDK contains the Silverlight IronPython assemblies and a command line application called Chiron. Chiron is a web server that runs from the command line and dynamically packages up XAPs as they are requested. 

A little about XAPs

XAPs are packaged Silverlight applications that are downloaded and run on the clients Silverlight runtime. It turns out the XAP is just zip files containing the assemblies, resources and in the case of dynamic language applications, some plain text source code!

Additionally there is an AppManifest.xaml included in the XAP which tells Silverlight about what is included in the XAP and where to find an entry point. Below is an example of an AppManifest.xaml automatically generated by Chiron.

<Deployment xmlns="http://schemas.microsoft.com/client/2007/deployment" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
RuntimeVersion="2.0.31005.0"
EntryPointAssembly="Microsoft.Scripting.Silverlight"
EntryPointType="Microsoft.Scripting.Silverlight.DynamicApplication">
<Deployment.Parts>
<AssemblyPart Source="Microsoft.Scripting.Silverlight.dll" />
<AssemblyPart Source="Microsoft.Scripting.ExtensionAttribute.dll" />
<AssemblyPart Source="Microsoft.Scripting.Core.dll" />
<AssemblyPart Source="Microsoft.Scripting.dll" />
<AssemblyPart Source="IronPython.dll" />
<AssemblyPart Source="IronPython.Modules.dll" />
<AssemblyPart x:Name="FarseerPhysics" Source="bin/FarseerPhysics.dll" />
</Deployment.Parts>
</Deployment>

This would probably be more interesting compared to a non dynamic language manifest file, but here are a few things I found interesting about it;
  • There is a whole lot of additional referenced assemblies required to compile Pyhton code in the Silverlight client, and yes, they are included in the XAP sent to the client.
  • The FarseerPhysics assembly is also included the XAP and is referenced in the manifest.
  • The EntryPointType is really the only difference between XAP manifest files generated for statically typed languages. For dynamic languages there are additional assemblies that compile the dynamic code, there doesn’t seem any reason you couldn’t feasibly write EntryPoint assemblies that interpreted lol-cat or brainfuck code.
  • And, I’m still targeting Silverlight 2! (Just haven’t got round to upgrading..) 

Writing an application

I started by having a play with the Farseer engine by writing some IronPython scripts and running them from the console. As it’s just a physics engine, it can be coupled with any user interface, even a console.

When I tried to get the same script working in Silverlight I did encounter some difficulties referencing the Farseer assembly. I had to use the assemblies strong name, which I found using Reflector, but seems unnecessary given it was sent in the same XAP as the code referencing it!

Instead of just hacking this demo together in one giant script I decided to structure my code a little, including separating the view and the simulation model over different namespaces. Generally I’m learning heaps about what is pythonic, what I carry over from C/C++/C# and what is good software design.

I’m not entirely happy with my view classes, which are just wrappers around Silverlight controls. Even though the Silverlight control can be accessed through a property, I feel this isn’t the elegant solution I should be able to build in a dynamic language. It seemed more right for the physics wrappers, but still overly verbose.   

Conclusion

I limited the example for this post to a very simple, but interactive 2d physics demo. Clicking any of the objects in the simulation creates a spring between the object and the mouse cursor until the mouse click is released or leaves the simulation view area. 

 

The Farseer physics engine is a lot of fun to use, it’s easy to get into and you can do some really cool stuff with it.

The source code is sent as plain text and compiled (or interpreted) on client. As the XAP is just a normal zip file, it can unzipped by almost any zip tool. Inside is all the plain text source code, compiled assemblies and manifest file.   

image 

It’s awesome to be able to develop Silverlight applications with physics libraries using a high level language and light weight development environment. Happy hacking..


Tags:
Categories:
Comments (3)

#.think.in infoDose #37 (12th July - 19th July)

July 19, 2009 10:41 by brodie

 

Win Stuff!?

Write a Win7 App, Win $17,777 (need to be a yank though)

Developer

Architecture

Silverlight

Utils

Games (as in board games)

Singularity Watch

Other


Tags:
Categories: Links
Comments (0)

#.think.in infoDose #36 (4th July - 11th July)

July 11, 2009 07:40 by brodie

Announcements

Architecture

Developer

UI

Code

Utilities

Singularity Watch

Other


Tags:
Categories: Links
Comments (0)

#.think.in infoDose #35 (25th June - 2nd July)

July 4, 2009 09:06 by brodie

Announcements

Architecture

Developer

Silverlight

Utilities

Singularity Watch

Books

alva-100x150

 

Other


Tags:
Categories: Links
Comments (0)