Groovy’s projectile motion

projectileToday I needed more than a decent crossword. So, I had a think about subjects that I had been fascinated by, way back when I was a student.

So what might I try to renegotiate in Groovy – the latest enhancement to Java / JVM programming and my preferred tool for Perl’ifing the JVM ;-) . Well, in general OOP or Object Orientated Programming, does not beautify formulas (and thinking of Java specifically, no operator overloading was a disaster), but Groovy does give the JVM something of what has been sorely missing.

Now before you get carried away, the Achilles heal of Groovy is speed. If I had to do something seriously numerically intensive, like FEA (Finite Element Analysis) on a supercomputer platform, then I might look elsewhere. But for a quick dip into calculating a projectile’s trajectory, I think we will be fine.

Well whilst I was doing my Masters (Comp Sci) some 20+ years ago, I landed a placement with the British Ministry of Defence. Not just any site,  but the Proof and Experimental establishment.

What did they do? Well they studied projectiles. And a noisy firing range they had too. There were some very bright mathematicians there whose job it was to simulate and later analyse projectile performance. Sadly you have me. However, due to my lower calibre within this department, you will at least have a chance of understanding the code ;-)

Ok, here we go. A quick play with Mathematica and then some coding in Groovy is the result I offer you here. So, don’t shy away from Groovy. I know Python is the in thing for Numerical experimentation these days, but why follow the herd.

// PROJECTILES
//
// formulas - Thank you Mathematica
//
// h = (v^2 sin^2(alpha))/(2 g)    // maximum height
// x = (v^2 sin(2 alpha))/g        // distance traveled
// T = (2 v sin(alpha))/g          // travel time

// v | initial speed
// alpha | release angle
// g | acceleration due to gravity (~~ 9.807 m/s^2)

class Cannon {
  def final g = 9.807F    // acceleration due to gravity (9.81 m/(s^2)
  def deg                 // angle projectile is launched (deg)
  def v                   // initial velocity of projectile (m/s)
  def y0                  // initial height of projectile (m)
  def d                   // total "horizontal" distance traveled by projectile (m)
  def h                   // max height achieved
  def T                   // travel time
                         // radians = deg*(Math.PI/180)
  def distance() {
    // x = (v^2 sin(2 alpha))/g
    d = (v**2 * Math.sin(2 * Math.toRadians(deg)))/g
  }

  def height() {
    // h = (v^2 sin^2(alpha))/(2 g)
    h = (v**2 * (Math.sin(Math.toRadians(deg))**2)) / (2 * g)
  }

  def time() {
    // T = (2 v sin(alpha))/g
    T = (2 * v * Math.sin(Math.toRadians(deg))) / g
  }

  @Override
   public String toString() {
   """Cannon angle($deg degrees) Launch Velocity($v m/s) Initial height($y0 metres)
      Distance: ${String.format("%.1f", distance())} metres
      Height:${String.format("%.2f", height())} metres
      Time:${String.format("%.3f", time())} seconds"""
   }
} // Cannon

// go
println new Cannon(deg: -90.0F, v:45.0F, y0:1.0F) // into ground

println new Cannon(deg: 0.0F, v:45.0F, y0:1.0F) // level

                                                // at an angle
println new Cannon(deg: 25.0F, v:45.0F, y0:1.0F)
println new Cannon(deg: 30.0F, v:45.0F, y0:1.0F)
println new Cannon(deg: 35.0F, v:45.0F, y0:1.0F)
println new Cannon(deg: 40.0F, v:45.0F, y0:1.0F)
println new Cannon(deg: 45.0F, v:45.0F, y0:1.0F)

println new Cannon(deg: 90.0F, v:45.0F, y0:1.0F) // straight up

which gives some results which Mathematica confirms to be correct

Cannon angle(-90.0 degrees) Launch Velocity(45.0 m/s) Initial height(1.0 metres)
           Distance: -0.0 metres
           Height:103.24 metres
           Time:-9.177 seconds
Cannon angle(0.0 degrees) Launch Velocity(45.0 m/s) Initial height(1.0 metres)
           Distance: 0.0 metres
           Height:0.00 metres
           Time:0.000 seconds
Cannon angle(25.0 degrees) Launch Velocity(45.0 m/s) Initial height(1.0 metres)
           Distance: 158.2 metres
           Height:18.44 metres
           Time:3.878 seconds
Cannon angle(30.0 degrees) Launch Velocity(45.0 m/s) Initial height(1.0 metres)
           Distance: 178.8 metres
           Height:25.81 metres
           Time:4.589 seconds
Cannon angle(35.0 degrees) Launch Velocity(45.0 m/s) Initial height(1.0 metres)
           Distance: 194.0 metres
           Height:33.97 metres
           Time:5.264 seconds
Cannon angle(40.0 degrees) Launch Velocity(45.0 m/s) Initial height(1.0 metres)
           Distance: 203.3 metres
           Height:42.66 metres
           Time:5.899 seconds
Cannon angle(45.0 degrees) Launch Velocity(45.0 m/s) Initial height(1.0 metres)
           Distance: 206.5 metres
           Height:51.62 metres
           Time:6.489 seconds
Cannon angle(90.0 degrees) Launch Velocity(45.0 m/s) Initial height(1.0 metres)
           Distance: 0.0 metres
           Height:103.24 metres
           Time:9.177 seconds

Advertisement

One Response to Groovy’s projectile motion

  1. Pingback: Clojure step 1 « LEXECORP

Leave a Reply

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

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com 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 )

Connecting to %s