//"Landersim simulates moon lander physics
//written by: Paul van Dinther
//            Dinther Product Design
//            Software development and specialists in simulation
//            email: vandinther@gmail.com
//Landersim is used in Moon lander.
//Copy right 2009 Dinther Product Design Ltd
   
    var metersToLocalLat = 0.000032964014284406189909348960717883; //multiply a meter with this to get a delta latitude (1/60 of a degree is 1852 meters)
    var degreesToRad = 0.017453292519943; //multiply with this to get angle in radians

    function landerSim(latitude, longitude, altitude, latspeed, lonspeed, vspeed, roll){
        this.radLatitude = latitude * degreesToRad;
        this.cosLatitude = Math.cos(this.radLatitude);
        this.metersToLocalLon = metersToLocalLat/this.cosLatitude;
        //inputs

		this.thrust = 0;
		this.latPower = 0;
		this.lonPower = 0;
		this.mainPower = 0;
		this.mass = 14696;
		this.gravity = 1.6; //moon gravity
		this.roll = new smooth(roll,roll,1000,0.0,512,4);
		this.pitch = new smooth(0,0,1000,0.0,512,4);
		//limits
		this.maxMainThrust = 44400;
		this.fuel = 0; //seconds of fuel at max thrust
		this.maxFuel = 30; //max seconds of fuel at max trust (when tanks are full)

        //outputs
        this.speed = 0;
        this.altitude = altitude;
		this.height = 0;
        this.latitude = latitude;
        this.longitude = longitude;
		this.latSpeed = latspeed;
		this.lonSpeed = lonspeed;
		this.horizontalSpeed = 0;
		this.vSpeed = vspeed;
		this.groundAltitude = 0;
		
		//events
		this.onGround = null; //event fired when touching ground
               
        this.update = function(deltaTime){
            this.radLatitude = this.latitude * degreesToRad;
            this.cosLatitude = Math.cos(this.radLatitude);
            this.metersToLocalLon = metersToLocalLat/this.cosLatitude;
			this.roll.update(deltaTime);
			this.pitch.update(deltaTime);
            this.simulateStep(deltaTime);
        }
    
        this.simulateStep = function (deltaTime){
			if (this.fuel <= 0){
				this.thrust = 0;
				this.fuel = 0;
			}
			this.lonPower = Math.sin(-this.roll.current * degreesToRad)* this.thrust;
			this.latPower = Math.sin(this.pitch.current * degreesToRad)* this.thrust;
			this.mainPower = Math.cos(this.roll.current * degreesToRad)* this.thrust;
			this.mainPower = Math.cos(this.pitch.current * degreesToRad)* this.mainPower;
			this.fuel -= (this.thrust/this.maxMainThrust)* deltaTime;
			
			this.latSpeed += this.latPower/this.mass * deltaTime;
			this.lonSpeed += this.lonPower/this.mass * deltaTime;
							
			this.vSpeed += (this.mainPower/this.mass - this.gravity) * deltaTime;
			this.groundAltitude = ge.getGlobe().getGroundAltitude(this.latitude, this.longitude);

			if (this.altitude < this.groundAltitude) {
				this.altitude = this.groundAltitude;
				if (this.onGround != null){ this.onGround(); }
			}
			this.height = this.altitude - this.groundAltitude;
			if (this.height == 0 && this.vSpeed < 0) this.vSpeed = 0;
            this.longitude += this.lonSpeed * deltaTime * this.metersToLocalLon;
            this.latitude += this.latSpeed * deltaTime * metersToLocalLat;
			this.altitude += this.vSpeed * deltaTime;
        }                    
    }

