Physics on Surface

While the ScatterView has some interesting effects that somewhat resemble the way things work in real life its missing a pretty big feature: you can’t bang stuff into each other!

So I decided to solve that problem by wiring up a real physics engine to a Surface app.

I used the WPF version of the Farseer Physics Engine and setup a simple solution where I could throw around a couple Rectangle Geometries.

Usually with a single mouse pointer, you need to simply track a single Spring but because I want to support n number of people coming up to the table and flicking and throwing these things around my problem is slightly more complex.

ScreenshotThe Surface SDK provides a very rich event system which resembles the Mouse event system so much that it would insert joke here. The main difference, logistically speaking, between a mouse and contact events is identity. We can refer to the mouse pointer as The [one and only] Mouse but contact points as a contact point [of many]. So it makes a little trickier because we need to create a new Spring for each new contact recognized, update its spring whenever it moves, and release the spring whenever the contact disappears.

Luckily, the Surface SDK does the hard part for you. When a ContactEvent is fired the ContactEventArgs object has a variety of properties on it. One of which is a Contact object which tells us some important things like whether it is a finger or a tag, what the bounding rectangle is and what element it is directly over on the visual tree. But most importantly it tells us its Id. The Id is an integer that is randomly generated each time a new contact is recognized on the Surface. As long as that finger stays on the Surface it will always have that Id. As I’m sure you may have guessed, as soon as it is removed, the same finger will generate a new Id for obvious reasons.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s