//pass in the amount of time you want to simulate
//in real time, this is the delta time between called to update
//in simulation time, this can be any amount -- but this function won't return until all updates are processed
//so it holds up the thread
int IESoRWorld::updateWorld(double msUpdate)
{
	msUpdate = 4*msUpdate;

	//we'll keep track of the number of times we simulated during this update
    int stepCount = 0;

    //# of seconds since last time
    double frameTime = msUpdate/1000.0f;

    //maximum frame time, to prevent what is called the spiral of death
    if (frameTime > .35)
        frameTime = .35;

    //we accumulate all the time we haven't rendered things in
    this->accumulator += frameTime;

	//as long as we have a chunk of time to simulate, we need to do the simulation
    while (accumulator >= simulationRate)
    {
		//how many times did we run this loop, we shoudl keep track --
		//that's the number of times we ran the physics engine
        stepCount++;

        //move the muscles quicker using this toggle
        float speedup = 3;

		//we loop through all our muscles, and update the lengths associated with the connectionsj
		for (std::vector<Muscle*>::iterator it = muscleList.begin() ; it != muscleList.end(); ++it)
		{
			//grab our muscle pointer from the list
			Muscle* muscle = *it;
			
			//get our distance joint -- holder of physical joint info
			b2DistanceJoint* dJoint = (b2DistanceJoint*)muscle->GetJoint();

			//Javascript version
			//muscle.SetLength(muscle.m_length + muscle.amplitude/this.scale*Math.cos(rad + muscle.phase*2*Math.PI));
			//double lengthCalc = (dJoint->GetLength() + muscle->GetAmplitude()*cos(radians + muscle->GetPhase() * 2 * M_PI));

			//fetch the original length of the distance joint, and add some fraction of that amount to the length
			//depending on the current location in the muscle cycle
            double lengthCalc = (1.0 + muscle->GetAmplitude() * cos(radians + muscle->GetPhase() * 2 * M_PI)) * muscle->GetOriginalLength();
			
			//we set our length as the calculate value
			dJoint->SetLength(lengthCalc);
		}

        //step the physics world
        this->world->Step(
            this->simulationRate   //frame-rate
            , 10       //velocity iterations
            , 10       //position iterations
        );

		//manually clear forces when doing fixed time steps, 
		//NOTE: that we disabled auto clear after step inside of the b2World
		this->world->ClearForces();

        //increment the radians for the muscles
        radians += speedup * this->simulationRate;

        //decrement the accumulator - we ran a chunk just now!
        accumulator -= this->simulationRate;
    }
            
	//interpolation is basically a measure of how far into the next step we should have simulated
	//it's meant to ease the drawing stages 
	//(you linearly interpolate the last frame and the current frame using this value)
    this->interpolation = accumulator / this->simulationRate;

	//how many times did we run the simulator?
    return stepCount;
}