/**
 * \brief Start-Funktion der Testumgebung
 *
 * Diese Funktion wird direkt von display() aufgerufen. Hier
 * sollte alles reingeschrieben werden, was für die einzelnen
 * Tests nötig ist.
 */
TEFUNC void displayLoop() {
	if (loop) {
		//! gravitation aufrechnen
		bodySystem.addGravity();
	
		geometrySystem.resolveCollisions(10);
	
		constraintSystem.step();
		if (postStabilization)
			constraintSystem.computePostStabilization ();
		
		//! Integriere so weit, wie der letzte simulations Schritt brauchte
		//bodySystem.integrateRungeKutta(getLastTime());
		//bodySystem.integrateEuler(getLastTime());
		
		bodySystem.integrateEulerVelocities(10);
		geometrySystem.resolveContacts(10);
		bodySystem.integrateEulerPositions(10);
					
		// für schweben : -1.170
		sph1acc->addForce(-1.10 * SimonState::exemplar()->getGravityVector()* sph1acc->getMass());
		
		if (move) {
			sph6acc->addForce(Vec3(5.0,5.0,0.0));
			sph7acc->addForce(Vec3(-5.0,5.0,0.0));
			sph8acc->addForce(Vec3(0.0,5.0,5.0));
			sph9acc->addForce(Vec3(0.0,5.0,-5.0));
		}
		//sph->addForce(50 * Vec3(1.0,0.0,0.0));
	
		
	}
	geometrySystem.drawGeometries();
	//cout << getLastTime() << endl;
}
/**
 * \brief Loop-Funktion der Testumgebung
 *
 * Diese Funktion wird direkt von display() aufgerufen. Hier
 * sollte alles reingeschrieben werden, was für die einzelnen
 * Tests nötig ist.
 */
TEFUNC void displayLoop() {
	const std::map<Id, RigidBodyPtr >* list = rbs.getMap();

	//Clock *zeit = new Clock();
	float interval = getLastTime();

	zeit += interval;

	if (doConstraints) {
		try {
			cs.step();
		} catch(std::exception& e) {
			std::cout << e.what() << std::endl;
		}
	}
	
	if (integrate) {
		//rbs.integrateEuler(interval);
		//rbs.integrateEuler(16);
                //rbs.integrateRungeKutta (20);
		rbs.integrateRungeKutta (10);
                //rbs.integrateVerletBaltman (10);
                //rbs.integrateEuler (10);
		if (postStabilization)
			cs.computePostStabilization ();
		//cout << interval << endl;
		rbs.addGravity();
		mrb->addForce(-1.0 * SimonState::exemplar()->getGravityVector()* mrb->getMass());
		
		if (doTorque) {
			mrb->addTorque(Vec3(20000.0,0.0,0.0));
		}
	}

	bool color = true;
	//bool posi = true;
	for (std::map<Id, SmartPointer<RigidBody> >::const_iterator p = list->begin(); p != list->end(); ++p) {
        
		Quaternion q = p->second->getOrientation();
		Vector3<float> axis;
		float angle;
		q.getAxisAngle(axis, angle);
		angle = RADTODEG(angle);
		Vector3<float> pos = p->second->getPosition();
		
		glLineWidth(1);
		
		glPushMatrix();
			glTranslatef(pos[0], pos[1], pos[2]);
			glRotatef(angle, axis[0], axis[1], axis[2]);
			
			
			
			if (color) {
				glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Graphics::red);
				color=false;
			} else {
				glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Graphics::green);
				
			}
			glutSolidCube(60.0);
				
				glPushMatrix();
				glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Graphics::yellow);
				glTranslatef(-30.0,0.0,0.0);
				glutSolidSphere(10.0,5,5);
				glPopMatrix();
				glPushMatrix();
					glBegin(GL_LINES);
						glVertex3f(-30,0,0);
						glVertex3f(-30,0,90);
					glEnd();	
				glPopMatrix();
				
				glPushMatrix();
				glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Graphics::blue);
				glTranslatef(30.0,0.0,0.0);
				glutSolidSphere(10.0,5,5);
				glPopMatrix();
				glPushMatrix();
					glBegin(GL_LINES);
						glVertex3f(30,0,0);
						glVertex3f(30,0,90);
					glEnd();	
				glPopMatrix();
				
			
		glPopMatrix();		
		
	}

	//sleep (1.0);
  
}