X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Fsimplephysics.cpp;fp=source%2Flibmarklin%2Fsimplephysics.cpp;h=3dc8e2ef93c2b61ee3c806252b81029aa78f5701;hb=9ddcd066e37e4c72685817c042c30897786ece05;hp=0000000000000000000000000000000000000000;hpb=e94014530a6e28a42bc5678e579ee07e0ee5329b;p=r2c2.git diff --git a/source/libmarklin/simplephysics.cpp b/source/libmarklin/simplephysics.cpp new file mode 100644 index 0000000..3dc8e2e --- /dev/null +++ b/source/libmarklin/simplephysics.cpp @@ -0,0 +1,61 @@ +/* $Id$ + +This file is part of the MSP Märklin suite +Copyright © 2010 Mikkosoft Productions, Mikko Rasa +Distributed under the GPL +*/ + +#include +#include +#include "simplephysics.h" + +using namespace std; +using namespace Msp; + +namespace Marklin { + +SimplePhysics::SimplePhysics(): + target_speed(TrainControl::continuous("speed", -1000, 1000)), + accel(0.07), + speed(0) +{ + target_speed.set(0); +} + +void SimplePhysics::set_control(const string &name, float v) +{ + if(name=="speed") + target_speed.set(v); +} + +const TrainControl &SimplePhysics::get_control(const string &name) const +{ + if(name=="speed") + return target_speed; + else + throw KeyError("Unknown control", name); +} + +float SimplePhysics::get_braking_distance() const +{ + return speed*speed/(2*accel); +} + +void SimplePhysics::tick(const Time::TimeDelta &dt) +{ + float secs = dt/Time::sec; + if(speedtarget_speed.value) + speed = target_speed.value; + } + else if(speed>target_speed.value) + { + speed -= secs*accel; + if(speed