Posted to tcl by hypnotoad at Thu Jun 19 17:09:28 GMT 2014view pretty

source [file normalize [file dirname [info script]]]/../../modules/tao-physics/index.tcl
package require Tk
package require Plotchart
source ~/odie/sandbox/tklib/modules/plotchart/xyplot.tcl
tao::class rocket {
  superclass physics::base

  property physics mass {
    default 200
    units kg
  }
  
  property physics fuel {
    default 10
    units kg
  }
  
  property physics fuel_burn_rate {
    default 1
    units kg/s
  }

  property physics fuel_energy_release {
    default 10000
    units N/kg
  }
  
  option world {
    default {}
  }
  
  constructor {objects args} {
    foreach {item obj} $objects {
      my graft $item $obj
    }
    my configurelist [::tao::args_to_options {*}$args]
    my initialize
  }
  
}

::physics::world create world

set n [rocket new {world ::world}]
$n physics heading [::vector::scale {0 0 45} [::odiemath::m_pi_180]]
#
#exit
set pscript {
  set g [my <world> physics property gravity]
  set thrust {0 0 0}
  if {$fuel > 0} {
    set dfuel [expr {$::dT * $fuel_burn_rate}]
    if {$dfuel > $fuel} {
      set dfuel $fuel
      set fuel 0
    } else {
      set fuel [expr {$fuel-$dfuel}]
    }
    if { $dfuel <= 0} {
      set thrust_mag 0.0
      set fuel 0.0
    } else {
      set mass [expr {$mass-$dfuel}]
      set thrust [::vector::scale $direction [expr {$dfuel * $fuel_energy_release/$mass}] 0]
    }
  }
  #puts [list f: $fuel $fuel_burn_rate $thrust]
  set acceleration [vector::add [vector::scale $g $::dT] $thrust]
  set velocity [vector::add $velocity $acceleration]
  set position [vector::add $position [::vector::scale $velocity $::dT]]
}

set ::dT 0.1
for {set step 0} {$step < 2000} {incr step} {
  set state [$n physics with $pscript]
  dict with state {}
  set t [expr {$step*$::dT}]
  #puts [list [expr {$t/10.0}] x $position v $velocity a $acceleration m $mass m/f $fuel t $thrust $direction]
  set plot_position($t) $position
  if {[lindex $position 1] < 0} break
}

set minx 1e10
set maxx -1e10
set miny 1e10
set maxy -1e10

foreach {t pos} [array get plot_position] {
  foreach {x y z} $pos {}
  if { $x > $maxx } { set maxx $x }
  if { $x < $minx } { set minx $x }
  if { $y > $maxy } { set maxy $y }
  if { $y < $miny } { set miny $x }
}

canvas .plot -width 500 -height 500
pack .plot 


set p [::Plotchart::createXYPlot .plot [list $minx $maxx 10] [list $miny $maxy 10]]
foreach t [lsort -real [array names plot_position]] {
  # If you want to crash tcl use:
  #lappend plotlist {*}[lrange $plot_position($t) 0 1]
  
  lappend plotlist $t {*}[set lrange lrange; $lrange $plot_position($t) 0 1]
  #lappend plotlist {*}[lrange [::vector::to_list $plot_position($t)] 0 1]
  #lappend plotlist {*}[::vector::index $plot_position($t) 0 1]
}

# HERE Prints
puts HERE

foreach {t x y} $plotlist {
  puts [list $p $t plot main [expr {int($x)}] [expr int($y)]]
  $p plot main [expr {int($x)}] [expr int($y)]
  puts [list /$p plot main [expr {int($x)}] [expr int($y)]]

}
puts done

if 0 {

foreach {lx ly lx} $plot_position(0.0) {}

set scalex [expr {500/($maxx-$minx)}]
set scaley [expr {500/($maxy-$miny)}]
if { $scalex > $scaley } {
  set scale $scaley
} else {
  set scale $scalex
}



foreach t [lsort -real [array names plot_position]] {
  puts $t
  puts [list $t $plot_position($t)]
  foreach {x y z} $plot_position($t) {}
  
  .plot create oval [expr {($x-$minx)*$scale-1}] [expr {($maxy-$y)*$scale-1}] [expr {($x-$minx)*$scale+1}] [expr {($maxy-$y)*$scale+1}]
}
}