I’ve implemented a simple dynamics engine in C# that simulates 2D physics using Verlet integration. It’s based on a project called Flade, written in Actionscript (for use in Flash/Flex) by Alec Cove.
This implementation adds some simple particle/particle collision (Flade does particle/fixed surface collision right now) and uses WPF for rendering. It uses the same ItemsControl and data binding approach as my earlier stuff.
Because it’s all in C# it was easy to build it into an XBAP for deployment. Click the image below to try it out!
Press left and right arrows to move the “car”. Pressing the up arrow can sometimes get you out of a jam (you can also try driving up the wall)… Try not to hit F5 (refresh) too often as it seems to slow the whole thing down (I’ve not looked into why yet).
Obviously this is a work in progress. If you look closely when the wheels collide you’ll notice a green marking appear; that’s just showing the location and depth of the collision for debugging (the deeper the collision, the longer the green mark).
I’ll post updates (and source code) as I go along
)



14 comments
Comments feed for this article
February 21, 2007 at 9:21 am
Josh Smith
Chris,
Your physics simulation XBAP has been entered into The WPF Challenge! Thanks for submitting it. Here’s write-up about it… http://joshsmithonwpf.wordpress.com/2007/02/21/new-submission-to-wpf-challenge/
Josh Smith
February 23, 2007 at 8:15 am
Rob Relyea
Fun simulation. Does your code work with arbitrary obstacles?
If so, it would be interesting to allow people to paste in a course (perhaps via xaml).
Would love feedback on things that were difficult…places you’d like to see WPF do better.
-Rob
February 23, 2007 at 9:27 am
Chris Cavanagh
Thanks Rob. Yes it could easily be done. It’s a very basic engine though. Right now it can handle lines (any angle), circles and rectangles (not angled) as static sutfaces. Collision detection and response between those and the moving particles/primitives is pretty robust. The ‘particle to particle’ collisions I implemented are really pretty raw though.
One area I’d like to see improvement in WPF (although still just a ‘nice to have’) is a way to use an ItemsControl with a Model3DGroup or Viewport3D. Right now it seems the only way to databind an arbitrary number of models in a Viewport is in code. Any comments on that?
)
February 24, 2007 at 4:34 pm
New submission to WPF Challenge « Josh Smith on WPF
[...] which demonstrates a simple physics engine, written in C#. You can read more about it here: http://chriscavanagh.wordpress.com/2007/02/21/xbap-2d-physics/ Try it out on this page: [...]
February 27, 2007 at 4:36 am
Martin Bennedik
Hi Chris,
just saw in the transcript of the latest WPF chat that Tim Sneath apparently would like you to work for Microsoft:
Tim Sneath [MS] (Moderator):
Q: New featrures for Tim: Add a physics engine to the framework :>
A: You’ve seen this, right? http://chriscavanagh.wordpress.com/2007/02/21/xbap-2d-physics/
(Or is it Chris that’s asking the question, in which case, would you like to come and work for us to implement it?
)
February 27, 2007 at 9:40 am
Chris Cavanagh
Thanks for that Martin — I’ll check it out
March 3, 2007 at 5:04 am
Rob Relyea
Chris-
Thanks for the feedback about itemscontrol and 3d…I know that Kevin Moore (and perhaps others) have done work/thinking in this area.
I will pass it on to them…
Thanks, Rob
March 8, 2007 at 10:25 am
Joe
Nice work Chris – very impressive. Any chance you will submit it CodePlex?
About the ItemsControl and 3D. ItemsControls are very much tied to Panels. So, if you derive a Panel to house your 3D models you can databind through that. Problem is that a Panel is natively 2D in nature, so I’ve only used it to contain multiple items which are viewports. This solution also means you have to be careful about z-ordering, becuase of clipping between 3D objects.
I’m looking at a better solution that just hooks into the contents of a panel being used through an ItemsControl and reflecting the visibility and positioning onto a separate viewport3D. I have this working fine for a Canvas for Layout2Layout animation – should be straight forward enough to reflect that back to a Viewport3D.
March 9, 2007 at 6:32 pm
George
Hi Chris! First of all, congratulation for your amazing work on WPF!
I am amazed by the concept to manipulate visual little things as simply as the mouse moves on the screen.
But, I have also a question. Should be possible to make this WPF physic engine portable on WPF/E? To be executable on a Firefox browser (or Safari, etc…)?
My feeling says me “yes” (but my feeling is a noobe’s feeling).
Lot of fun from Belgium!
Thanks.
March 10, 2007 at 12:42 am
Chris Cavanagh
Joe – Good idea to submit it to CodePlex! Think I’ll do just that. Could you send me some examples of what you’re talking about with ItemsControl and Viewport3D? Thanks!
March 10, 2007 at 12:45 am
Chris Cavanagh
George – Actually it would probably be quite easy. My demo is based on Flade which is an Actionscript physics library. Actionscript is as close to Javascript as you could hope for; I bet it’d be relatively simple to port and control some WPF/E elements with it
) Let me know if you’re planning to do this and I’ll be more than happy to help you out (or I might get to it myself after my upcoming 3D physics demo ;o] [fingers crossed] ).
March 10, 2007 at 8:04 am
Jon Harrop
Great stuff!
March 10, 2007 at 9:14 am
George
Mmm… Great idea!
I’m a beginner but the idea to try to port some parts of the FLADE/APE’s code in javascript to use them in WPF/E should be realy useful (and powerful)!
If the work is not too hard for me, I’ll try to port some first elements.
Some news as soon as possible.
>> 3D physics? Wow!
Good luck! ;o]
May 21, 2007 at 9:22 am
benhadi
very good