/**
 * go(): This is in fact the main game loop.
 * For the moment it computes physics and output sounds on each frame
 * but this could be optimized further. It also computes IA on each frame...
 */
bool HOO::Application::go(){
	startup();
	while(keepRunning()){
		renderOneFrame();
		computePhysics();
		computeIA();
		outputSound();
	}
	shutDown();
	return 0;
}
/* Computes acceleration to every control point of the chain, 
 which is in state given by 'chain'.
 Returns result in array 'a'. */
void computeAcceleration(struct chain * chain, struct point a[])
{
    for(int i=1; i<=chain->number; i++)
    {
        chain->f[i].x = 0;
        chain->f[i].y = 0;
    }
    
    
    if(isRandomforce)
        for(int i=1; i<=chain->number; i++)
        {
            chain->f[i].x = PN.PerlinNoise1D(tempCount++, 1000, 0.7) * 500;
            chain->f[i].y = PN.PerlinNoise1D(tempCount, 1000, 0.7) * 500;
        }
    
    
    
    
    //Gravity Force
    if(isGravity)
        for(int i=1; i<=chain->number; i++)
            chain->f[i].y = -GRAVITY;
    
    //Damping Force
    if(isDamping)
        for(int i=1; i<=chain->number; i++)
        {
            chain->f[i].x += - (chain->v[i].x * DAMPINGCOEF);
            chain->f[i].y += - (chain->v[i].y * DAMPINGCOEF);
        }

    
    //User Force
    for(int i=1; i<=chain->number; i++)
        chain->f[i].x += g_vDiffPos[0] * 0.5;
    for(int i=1; i<=chain->number; i++)
        chain->f[i].y += g_vDiffPos[1] * 0.5;
    
    if(pushUp)
        for(int i=1; i<=chain->number; i++)
            chain->f[i].y += USERFORCE;
    if(pushDown)
        for(int i=1; i<=chain->number; i++)
            chain->f[i].y -= USERFORCE;
    if(pushLeft)
        for(int i=1; i<=chain->number; i++)
            chain->f[i].x -= USERFORCE;
    if(pushRight)
        for(int i=1; i<=chain->number; i++)
            chain->f[i].x += USERFORCE;
    
    //Plane Collision
    if(isPlane)
        for(int i=1; i<=chain->number; i++)
            if(chain->p[i].y > 0)
            {
                chain->f[i].y += -1.0 * chain->p[i].y * KCOLLISION;
            }
    
    computePhysics(chain, a);
    
}