void RagdollDemo::initPhysics()
{
	// Setup the basic world

	setTexturing(true);
	setShadows(true);

	setCameraDistance(btScalar(5.));

	m_collisionConfiguration = new btDefaultCollisionConfiguration();

	m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);

	btVector3 worldAabbMin(-10000,-10000,-10000);
	btVector3 worldAabbMax(10000,10000,10000);
	m_broadphase = new btAxisSweep3 (worldAabbMin, worldAabbMax);

	m_solver = new btSequentialImpulseConstraintSolver;

	m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
	//m_dynamicsWorld->getDispatchInfo().m_useConvexConservativeDistanceUtil = true;
	//m_dynamicsWorld->getDispatchInfo().m_convexConservativeDistanceThreshold = 0.01f;



	// Setup a big ground box
	{
		btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(200.),btScalar(10.),btScalar(200.)));
		m_collisionShapes.push_back(groundShape);
		btTransform groundTransform;
		groundTransform.setIdentity();
		groundTransform.setOrigin(btVector3(0,-10,0));

#define CREATE_GROUND_COLLISION_OBJECT 1
#ifdef CREATE_GROUND_COLLISION_OBJECT
		btCollisionObject* fixedGround = new btCollisionObject();
		fixedGround->setCollisionShape(groundShape);
		fixedGround->setWorldTransform(groundTransform);
		m_dynamicsWorld->addCollisionObject(fixedGround);
#else
		localCreateRigidBody(btScalar(0.),groundTransform,groundShape);
#endif //CREATE_GROUND_COLLISION_OBJECT

	}

	// Spawn one ragdoll
	btVector3 startOffset(1,0.5,0);
	//spawnRagdoll(startOffset);
	startOffset.setValue(-1,0.5,0);
	//spawnRagdoll(startOffset);
    
    CreateBox(0, 0., 2., 0., 1., 0.2, 1.); // Create the box
    
    //leg components
    CreateCylinder(1, 2. , 2., 0., .2, 1., 0., 0., M_PI_2); //xleft horizontal
    CreateCylinder(2, -2., 2., 0., .2, 1., 0., 0., M_PI_2); //xright (negative) horizontal
    CreateCylinder(3, 0, 2., 2., .2, 1., M_PI_2, 0., 0.);    //zpositive (into screen)
    CreateCylinder(4, 0, 2., -2., .2, 1., M_PI_2, 0., 0.);   //znegative (out of screen)
    CreateCylinder(5, 3.0, 1., 0., .2, 1., 0., 0., 0.);       //xleft vertical
    CreateCylinder(6, -3.0, 1., 0., .2, 1., 0., 0., 0.);      //xright vertical
    CreateCylinder(7, 0., 1., 3.0, .2, 1., 0., 0., 0.);     //zpositive vertical
    CreateCylinder(8, 0., 1., -3.0, .2, 1., 0., 0., 0.);    //znegative vertical
    
    //The axisworldtolocal defines a vector perpendicular to the plane that you want the bodies to rotate in
    
    
    //hinge the legs together
    //xnegative -- right
    //two bodies should rotate in y-plane through x--give axisworldtolocal it a z vector
    btVector3 local2 = PointWorldToLocal(2, btVector3(-3., 2., 0));
    btVector3 local6 = PointWorldToLocal(6, btVector3(-3., 2., 0));
    btVector3 axis2 = AxisWorldToLocal(2, btVector3(0., 0., 1.));
    btVector3 axis6 = AxisWorldToLocal(6, btVector3( 0., 0., 1.));
    
    
    CreateHinge(0, *body[2], *body[6], local2, local6, axis2, axis6);
    joints[0]->setLimit(btScalar(0.), btScalar(M_PI_2+M_PI_4));
    
   
    //positive -- left
    //give axisworldtolocal a z vector
    btVector3 local1 = PointWorldToLocal(1, btVector3(3., 2., 0));
    btVector3 local5 = PointWorldToLocal(5, btVector3(3., 2., 0));
    btVector3 axis1 = AxisWorldToLocal(1, btVector3( 0., 0., 1.));
    btVector3 axis5 = AxisWorldToLocal(5, btVector3(0., 0., 1.));
    
    CreateHinge(1, *body[1], *body[5], local1, local5, axis1, axis5);
    joints[1]->setLimit(btScalar(M_PI_4), btScalar(M_PI_4));
    
    //zpositive -- back
    //rotates in y-plane through z--give it an x vector
    btVector3 local3 = PointWorldToLocal(3, btVector3(0, 2., 3.));
    btVector3 local7 = PointWorldToLocal(7, btVector3(0, 2., 3.));
    btVector3 axis3 = AxisWorldToLocal(3, btVector3(1., 0., 0.));
    btVector3 axis7 = AxisWorldToLocal(7, btVector3(1., 0., 0.));
    
    CreateHinge(2, *body[3], *body[7], local3, local7, axis3, axis7);
    joints[2]->setLimit(btScalar(0.), btScalar(M_PI_2+M_PI_4));
    
    //znegative -- front
    
    btVector3 local4 = PointWorldToLocal(4, btVector3(0, 2., -3.));
    btVector3 local8 = PointWorldToLocal(8, btVector3(0, 2., -3.));
    btVector3 axis4 = AxisWorldToLocal(4, btVector3(1., 0., 0.));
    btVector3 axis8 = AxisWorldToLocal(8, btVector3(1., 0., 0.));
    
    CreateHinge(3, *body[4], *body[8], local4, local8, axis4, axis8);
    joints[3]->setLimit(btScalar(M_PI_4), btScalar(M_PI_4));
    
    //hinge the legs to the body
    //xright to main
    btVector3 localHinge2 = PointWorldToLocal(2, btVector3(-1, 2., 0));
    btVector3 mainHinge2 = PointWorldToLocal(0, btVector3(-1, 2., 0));
    btVector3 localAxis2 = AxisWorldToLocal(2, btVector3(0., 0., 1.));
    btVector3 mainAxis2 = AxisWorldToLocal(0, btVector3( 0., 0., 1.));
    
    CreateHinge(4, *body[0], *body[2], mainHinge2, localHinge2, mainAxis2, localAxis2);
    //joints[4]->setLimit(btScalar(-M_PI_2-M_PI_4), btScalar(M_PI_4));
    
    
    //xleft to main
    btVector3 localHinge1 = PointWorldToLocal(1, btVector3(1, 2., 0));
    btVector3 mainHinge1 = PointWorldToLocal(0, btVector3(1, 2., 0));
    btVector3 localAxis1 = AxisWorldToLocal(1, btVector3(0., 0., 1.));
    btVector3 mainAxis1 = AxisWorldToLocal(0, btVector3( 0., 0., 1.));
    
    
    CreateHinge(5, *body[0], *body[1], mainHinge1, localHinge1, mainAxis1, localAxis1);
    //joints[5]->setLimit(btScalar(-M_PI_2), btScalar(M_PI_2));
    
    //zpositive (back) to main
    btVector3 localHinge3 = PointWorldToLocal(3, btVector3(0., 2., 1));
    btVector3 mainHinge3 = PointWorldToLocal(0, btVector3(0., 2., 1));
    btVector3 localAxis3 = AxisWorldToLocal(3, btVector3(1., 0., 0.));
    btVector3 mainAxis3 = AxisWorldToLocal(0, btVector3( 1., 0., 0.));
    
    CreateHinge(6, *body[0], *body[3], mainHinge3, localHinge3, mainAxis3, localAxis3);
    //joints[6]->setLimit(btScalar(-M_PI_2-M_PI_4), btScalar(M_PI_4));
    
    btVector3 localHinge4 = PointWorldToLocal(4, btVector3(0., 2., -1));
    btVector3 mainHinge4= PointWorldToLocal(0, btVector3(0., 2., -1));
    btVector3 localAxis4 = AxisWorldToLocal(4, btVector3(1., 0., 0. ));
    btVector3 mainAxis4 = AxisWorldToLocal(0, btVector3( 1., 0., 0.));
    
    
    CreateHinge(7, *body[0], *body[4], mainHinge4, localHinge4, mainAxis4, localAxis4);
    //joints[7]->setLimit(btScalar(-M_PI_2), btScalar(M_PI_2));
    
	clientResetScene();
}
void RagdollDemo::initPhysics()
{
	// Setup the basic world

	setTexturing(true);
	setShadows(true);

	setCameraDistance(btScalar(5.));

	m_collisionConfiguration = new btDefaultCollisionConfiguration();

	m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);

	btVector3 worldAabbMin(-10000,-10000,-10000);
	btVector3 worldAabbMax(10000,10000,10000);
	m_broadphase = new btAxisSweep3 (worldAabbMin, worldAabbMax);

	m_solver = new btSequentialImpulseConstraintSolver;

	m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
	//m_dynamicsWorld->getDispatchInfo().m_useConvexConservativeDistanceUtil = true;
	//m_dynamicsWorld->getDispatchInfo().m_convexConservativeDistanceThreshold = 0.01f;



	// Setup a big ground box
	{
		btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(200.),btScalar(10.),btScalar(200.)));
		m_collisionShapes.push_back(groundShape);
		btTransform groundTransform;
		groundTransform.setIdentity();
		groundTransform.setOrigin(btVector3(0,-10,0));

#define CREATE_GROUND_COLLISION_OBJECT 1
#ifdef CREATE_GROUND_COLLISION_OBJECT
		btCollisionObject* fixedGround = new btCollisionObject();
		fixedGround->setCollisionShape(groundShape);
		fixedGround->setWorldTransform(groundTransform);
		m_dynamicsWorld->addCollisionObject(fixedGround);
#else
		localCreateRigidBody(btScalar(0.),groundTransform,groundShape);
#endif //CREATE_GROUND_COLLISION_OBJECT

	}

	// Spawn one ragdoll
	btVector3 startOffset(1,0.5,0);
	//spawnRagdoll(startOffset);
	startOffset.setValue(-1,0.5,0);
	//spawnRagdoll(startOffset);

  CreateBox(0, 0., 0., 1., 1., 1., 0.2);
  // fore limbs
  CreateCylinder(1, 1., 0., 1., 0.1, 1., 1);
  CreateCylinder(2, -1., 0., 1., 0.1, 1., 1);
  CreateCylinder(3, 0., 1., 1., 0.1, 1., 2);
  CreateCylinder(4, 0., -1., 1., 0.1, 1., 2);

  CreateCylinder(5, 1.5,  0., .5, 0.1, 1., 3);
  CreateCylinder(6, -1.5, 0., .5, 0.1, 1., 3);
  CreateCylinder(7, 0.,  1.5, .5, 0.1, 1., 3);
  CreateCylinder(8, 0., -1.5, .5, 0.1, 1., 3);

  btVector3 v = PointWorldToLocal(0, btVector3(0,1,0)); 
  /* std::cerr << "v " << v[0] << " " << v[1] << " " << v[2] << std::endl; */
  printf("(%f, %f, %f)\n", v[0], v[1], v[2]);


  CreateHinge(0, 
              0, 1, 
              0.5, 0., 1., 
              0.,  -1., 0.);

  CreateHinge(1, 0, 2, 
              -.5, 0., 1., 
              0., 1., 0.);

  CreateHinge(2, 0, 3, 
              0., .5, 1., 
              1., 0., 0.);

  CreateHinge(3, 0, 4, 
              0., -.5, 1., 
              -1., 0., 0.);

  CreateHinge(4, 1, 5, 
              1.5, 0., 1., 
              0., -1., 0.);

  CreateHinge(5, 2, 6, 
              -1.5, 0., 1., 
              0., 1., 0.);

  CreateHinge(6, 3, 7, 
              0., 1.5, 1., 
              1., 0., 0.);

  CreateHinge(7, 4, 8, 
              0., -1.5, 1., 
              -1., 0., 0.);


	clientResetScene();		
}