//"gExplorer to explore "Places"
//written by: Paul van Dinther
//            Dinther Product Design
//            Software development and specialists in simulation
//            email: vandinther@gmail.com
//class for smooth value changes

//Use this class for smoothly animated values. Call update in the main animation loop
//and pass the time that has passed since the last pass.


function smooth(scurrent, starget, smaxrate, srate, sgain, sloose){
    this.current = scurrent; //read your current value here
    this.target = starget; //set to what you want value to become
    this.maxRate = smaxrate; //controls max rate of change
    this.rate = srate; //current rate of change
    this.gain = sgain; //defines how acceleration is
    this.loose = sloose; //defines how fast deceleration is
    this.deltaValue = 0.0;
    this.freeze = function(){
        this.rate = 0;
        this.deltaValue = 0;
        this.target = this.current;
    }
    this.set = function(newValue){
        this.current = newValue;
        this.freeze();
    }    
    this.update = function(deltaTime){
        this.deltaValue = this.target - this.current;
        if (this.deltaValue < 0){
            var r1 = this.deltaValue * this.loose;
            var r2 = this.rate - this.gain * deltaTime;
            var r3 = -this.maxRate;
            this.rate = Math.max(Math.max(r1,r2),r3);   
        }
        else {
            var r1 = this.deltaValue * this.loose;
            var r2 = this.rate + this.gain * deltaTime;
            var r3 = this.maxRate;
            this.rate = Math.min(Math.min(r1,r2),r3);   
        }
        this.current += this.rate * deltaTime;  
    }   
}
