Having just started getting into C#, I can already say that it’s a pleasure to work with – especially when used with its tightly coupled IDE, Visual Studio. Of course, being a physics/astronomy/computer science geek, I got straight to work doing some projectile physics in C# to see how it turned out. To get myself started on this blog, I’m going to write a couple of posts about doing physics in .net:
- Units of Measure: Doing Physics in .net Part I
- Physics Library: Doing Physics in .net Part II
- Vectors, Projectiles, and More: Doing Physics in .net Part III
In part 1 I went over the benefits of F#’s units of measure over doing dimensionless math with C# (and most other languages). But if your project isn’t already in F#, it’s a little hard to benefit from it. Luckily, it’s easy to work between languages in .net managed code. You can build the F# library using the standalone installer and Visual Studio 2010 Shell. This is the F# code I left off with:
#light namespace FsPhysicsDemo module Physics = [<Measure>] type m [<Measure>] type s let xf (xi : float<m>) (v : float<m/s>) (a : float<m/s^2>) (t : float<s>) = xi + v * t + 0.5 * a * t * t let v (a : float<m/s^2>) (t : float<s>) = a * t let deltaV (vi : float<m/s>) (vf : float<m/s>) = vf - vi
This works fine as a small library, but it’s a little annoying to work with. When the math was just done in C#, we had the benefit of it being object oriented—the parameters of the equation were all stored in the object—whereas with this code each parameter needs to be passed to a function. What makes it even worse is that we already have an object oriented projectile in C#, and then we go ahead and throw away the strengths of that setup: