Ejemplo n.º 1
0
void TSRODERigidBody::AddCylinderGeometry( TSRPhysicsWorld* _pWorldInterface, const TSRMatrix4& _bodyToGeomTransform, float _fRadius,float _fLength, float _fDensity )
{
	TSRODEPhysicsWorld* _pWorld = ( TSRODEPhysicsWorld* ) _pWorldInterface;
    dMass totalMass;
    dBodyGetMass( m_BodyID, &totalMass );
    if ( m_GeomIDs.size() == 0 )
    {
        dMassSetZero( &totalMass );
    }
    dMatrix4 R;
    dVector3 P;
    Matrix4ToODE( _bodyToGeomTransform, R, P );
    dGeomID geomTransform = dCreateGeomTransform( _pWorld->m_SpaceID );
    dGeomID encapsulatedGeom = 0;
    dMass currMass;
    dMassSetZero( &currMass );

    encapsulatedGeom = dCreateCylinder( 0, _fRadius, _fLength );

    dMassSetCylinder( &currMass, _fDensity, 0, _fRadius, _fLength );
    dMassRotate( &currMass, R );
    //dMassTranslate(&currMass,P[0],P[1],P[2]);
    dMassAdd( &totalMass, &currMass );
    dGeomSetPosition( encapsulatedGeom, P[ 0 ], P[ 1 ], P[ 2 ] );
    dGeomSetRotation( encapsulatedGeom, R );
    dGeomTransformSetCleanup( geomTransform, 1 );
    dGeomTransformSetGeom( geomTransform, encapsulatedGeom );
    dGeomSetBody( geomTransform, m_BodyID );

    m_GeomIDs.push_back( geomTransform );
    dBodySetMass( m_BodyID, &totalMass );

}
Ejemplo n.º 2
0
dMass ColCylinder::GetMass() const
{ 
    dMass m; 
    Convert( m.c, Vec4( 0, 0, 0, 1 ) );
    float scale = PhysicsServer::s_pInstance->GetWorldScale();
    dMassSetCylinder( &m, GetDensity(), 3, m_Radius*scale, m_Height*scale );
    return m; 
}
Ejemplo n.º 3
0
void Cylinder::computeMassProperties(dMass& m)
{
  if(capped)
    dMassSetCapsule(&m, 1, 3, dReal(height), dReal(height));
  else
    dMassSetCylinder(&m, 1, 3, dReal(height), dReal(height));
  dMassAdjust(&m, dReal(mass));
}
Ejemplo n.º 4
0
void PCylinder::setMass(float mass)
{
  m_mass = mass;
  dMass m;
  dMassSetCylinder (&m,1,1,m_radius,m_length);
  dMassAdjust (&m,m_mass);
  dBodySetMass (body,&m);
}
Ejemplo n.º 5
0
 void Cylinder::setMass(double mass, bool density){
   if(body){
     dMass m;
     dMassSetCylinder(&m, mass, 3 , osgcylinder->getRadius(), osgcylinder->getHeight());
     if(!density)
       dMassAdjust (&m, mass);
     dBodySetMass (body,&m); //assign the mass to the body
   }
 }
Ejemplo n.º 6
0
//===========================================================================
void cODEGenericBody::createDynamicCapsule(const double a_radius,
                                           const double a_length,
                                           bool a_staticObject,
										   const cVector3d& a_offsetPos,
										   const cMatrix3d& a_offsetRot)
{
    // create ode dynamic body if object is non static
    if (!a_staticObject)
    {
        m_ode_body = dBodyCreate(m_ODEWorld->m_ode_world);

	    // store pointer to current object
	    dBodySetData (m_ode_body, this);
    }
    m_static = a_staticObject;

    // build box
    m_ode_geom = dCreateCCylinder(m_ODEWorld->m_ode_space, a_radius, a_length);

	// adjust position offset
	dGeomSetPosition (m_ode_geom, a_offsetPos.x, a_offsetPos.y, a_offsetPos.z);

	// adjust orientation offset
	dMatrix3 R;
	R[0]  = a_offsetRot.m[0][0];
	R[1]  = a_offsetRot.m[0][1];
	R[2]  = a_offsetRot.m[0][2];
	R[4]  = a_offsetRot.m[1][0];
	R[5]  = a_offsetRot.m[1][1];
	R[6]  = a_offsetRot.m[1][2];
	R[8]  = a_offsetRot.m[2][0];
	R[9]  = a_offsetRot.m[2][1];
	R[10] = a_offsetRot.m[2][2];
	dGeomSetRotation (m_ode_geom, R);

    // set inertia properties
    if (!m_static)
    {
        dMassSetCylinder (&m_ode_mass, 1.0, 3, a_radius, a_length);  // 3 = x-axis direction
	    dMassAdjust(&m_ode_mass, m_mass);
	    dBodySetMass(m_ode_body,&m_ode_mass);

        // attach body and geometry together
        dGeomSetBody(m_ode_geom, m_ode_body);
    }

    // store dynamic model type
    m_typeDynamicCollisionModel = ODE_MODEL_CYLINDER;

    // store dynamic model parameters
    m_paramDynColModel0 = a_radius;
    m_paramDynColModel1 = a_length;
    m_paramDynColModel2 = 0.0;
    m_posOffsetDynColModel = a_offsetPos;
    m_rotOffsetDynColModel = a_offsetRot;
}
Ejemplo n.º 7
0
void CCylinderGeom::get_mass(dMass& m)
{
	dMassSetCylinder(&m,1.f,2,m_cylinder.m_radius,m_cylinder.m_height);
	dMatrix3 DMatx;
	Fmatrix33 m33;
	m33.j.set(m_cylinder.m_direction);
	Fvector::generate_orthonormal_basis(m33.j,m33.k,m33.i);
	PHDynamicData::FMX33toDMX(m33,DMatx);
	dMassRotate(&m,DMatx);
}
Ejemplo n.º 8
0
int main (int argc, char **argv)
{
    // set for drawing
    dsFunctions fn;
    fn.version = DS_VERSION;
    fn.start   = &start;
    fn.step    = &simLoop;
    fn.command = NULL;
    fn.stop    = NULL;
    fn.path_to_textures = "../textures";


    dInitODE();              // init ODE
    world = dWorldCreate();  // create a dynamic world
    dWorldSetGravity(world,0,0,-0.1);

    dMass m;                 // a parameter for mass
    dMassSetZero (&m);       // initialize the parameter

    //@a sphere
    sphere.body = dBodyCreate (world);     // create a rigid body
    dReal radius = 0.5;                    // radius [m]
    dMassSetSphere (&m,DENSITY,radius);    // calculate a mass parameter for a sphere
    dBodySetMass (sphere.body,&m);         // set the mass parameter to the body
    dBodySetPosition (sphere.body,0,1, 1); // set the position of the body


    //@a box
    box.body = dBodyCreate (world);
    dMassSetBox (&m,DENSITY,sides[0],sides[1],sides[2]);
    dBodySetMass (box.body,&m);
    dBodySetPosition (box.body,0,2,1);

    // a capsule
    capsule.body = dBodyCreate (world);
    dMassSetCapsule(&m,DENSITY,3,radius,length);
    dBodySetMass (capsule.body,&m);
    dBodySetPosition (capsule.body,0,4,1);

    // a cylinder
    cylinder.body = dBodyCreate (world);
    dMassSetCylinder(&m,DENSITY,3,radius,length);
    dBodySetMass (cylinder.body,&m);
    dBodySetPosition (cylinder.body,0,3,1);

    // do the simulation
    dsSimulationLoop (argc,argv,960,480,&fn);

    dWorldDestroy (world); // destroy the world
    dCloseODE();           // close ODE

    return 0;
}
Ejemplo n.º 9
0
void Cylinder::createBody()
{
  if(body == 0)
  {
    body = dBodyCreate(*simulation->getPhysicalWorld());
    dMass m;
    if(capped)
      dMassSetCapsule(&m, 1, 3, dReal(height), dReal(height));
    else
      dMassSetCylinder(&m, 1, 3, dReal(height), dReal(height));
    dMassAdjust(&m, dReal(mass));
    dBodySetMass(body, &m);
    dBodySetPosition(body, dReal(position.v[0]), dReal(position.v[1]), dReal(position.v[2]));
    dMatrix3 rotationMatrix;
    ODETools::convertMatrix(rotation, rotationMatrix);
    dBodySetRotation(body, rotationMatrix);
  }
}
Ejemplo n.º 10
0
void SkidSteeringVehicle::create() {
    this->vehicleBody = dBodyCreate(this->environment->world);
    this->vehicleGeom = dCreateBox(this->environment->space, this->vehicleBodyLength, this->vehicleBodyWidth, this->vehicleBodyHeight);
    this->environment->setGeomName(this->vehicleGeom, name + ".vehicleGeom");
    dMassSetBox(&this->vehicleMass, this->density, this->vehicleBodyLength, this->vehicleBodyWidth, this->vehicleBodyHeight);
    dGeomSetCategoryBits(this->vehicleGeom, Category::OBSTACLE);
    dGeomSetCollideBits(this->vehicleGeom, Category::OBSTACLE | Category::TERRAIN);
    dBodySetMass(this->vehicleBody, &this->vehicleMass);
    dBodySetPosition(this->vehicleBody, this->xOffset, this->yOffset, this->zOffset);
    dGeomSetBody(this->vehicleGeom, this->vehicleBody);
    dGeomSetOffsetPosition(this->vehicleGeom, 0, 0, this->wheelRadius);

    dReal w = this->vehicleBodyWidth + this->wheelWidth + 2 * this->trackVehicleSpace;
    for(int fr = 0; fr < 2; fr++) {
        for(int lr = 0; lr < 2; lr++) {
            this->wheelGeom[fr][lr] = dCreateCylinder(this->environment->space, this->wheelRadius, this->wheelWidth);
            this->environment->setGeomName(this->wheelGeom[fr][lr], this->name + "." + (!fr ? "front" : "rear") + (!lr ? "Left" : "Right") + "Wheel");
            dGeomSetCategoryBits(this->wheelGeom[fr][lr], Category::TRACK_GROUSER);
            dGeomSetCollideBits(this->wheelGeom[fr][lr], Category::TERRAIN);
            dMassSetCylinder(&this->wheelMass[fr][lr], this->density, 3, this->wheelRadius, this->wheelWidth);
            this->wheelBody[fr][lr] = dBodyCreate(this->environment->world);
            dBodySetMass(this->wheelBody[fr][lr], &this->wheelMass[fr][lr]);
            dGeomSetBody(this->wheelGeom[fr][lr], this->wheelBody[fr][lr]);
            dBodySetPosition(this->wheelBody[fr][lr], this->xOffset + (fr - 0.5) * this->wheelBase, this->yOffset + w * (lr - 0.5), this->zOffset);
            dMatrix3 wheelR;
            dRFromZAxis(wheelR, 0, 2 * lr - 1, 0);
            dBodySetRotation(this->wheelBody[fr][lr], wheelR);
            this->wheelJoint[fr][lr] = dJointCreateHinge(this->environment->world, 0);
            dJointAttach(this->wheelJoint[fr][lr], this->vehicleBody, this->wheelBody[fr][lr]);
            dJointSetHingeAnchor(this->wheelJoint[fr][lr], this->xOffset + (fr - 0.5) * this->wheelBase, this->yOffset + this->vehicleBodyWidth * (lr - 0.5), this->zOffset);
            dJointSetHingeAxis(this->wheelJoint[fr][lr], 0, 1, 0);
            dJointSetHingeParam(this->wheelJoint[fr][lr], dParamFMax, 5.0);
        }
    }
    
    this->bodyArray = dRigidBodyArrayCreate(this->vehicleBody);
    for(int fr = 0; fr < 2; fr++) {
        for(int lr = 0; lr < 2; lr++) {
            dRigidBodyArrayAdd(this->bodyArray, this->wheelBody[fr][lr]);
        }
    }
}
void Robots::construirRodas(dWorldID world)
{
    for (int i=0; i < 2; i++)
    {
        // Cria objeto e geometria
        this->wheel[i] = dBodyCreate(world);
        this->cylinder[i] = dCreateCylinder(0,RADIUS,wTHICK);
			
        // Define a posição e orientação do objeto
        dQuaternion q;
        dQFromAxisAndAngle(q,1,0,0,M_PI*0.5);
        dBodySetQuaternion(this->wheel[i],q);
        dBodySetPosition(this->wheel[i],this->pegarX(),this->pegarY()+(1-2*i)*((WIDTH/2)+(wTHICK/2)),STARTZ);

        // Define a massa do objeto
        dMass m;
        dMassSetCylinder(&m,1,3,RADIUS,wTHICK);
        dMassAdjust(&m,WMASS);
        dBodySetMass(this->wheel[i],&m);
			
        // Associa o objeto à sua geometria
        dGeomSetBody(this->cylinder[i],this->wheel[i]);
    }
}
Ejemplo n.º 12
0
static void command (int cmd)
{
    int i,j,k;
    dReal sides[3];
    dMass m;
    bool setBody = false;

    cmd = locase (cmd);
    if (cmd == 'b' || cmd == 's' || cmd == 'c' || cmd == 'x' || cmd == 'm' || cmd == 'y' || cmd == 'v') {
        if (num < NUM) {
            i = num;
            num++;
        }
        else {
            i = nextobj;
            nextobj++;
            if (nextobj >= num) nextobj = 0;

            // destroy the body and geoms for slot i
            dBodyDestroy (obj[i].body);
            for (k=0; k < GPB; k++) {
                if (obj[i].geom[k]) dGeomDestroy (obj[i].geom[k]);
            }
            memset (&obj[i],0,sizeof(obj[i]));
        }

        obj[i].body = dBodyCreate (world);
        for (k=0; k<3; k++) sides[k] = dRandReal()*0.5+0.1;

        dMatrix3 R;
        if (random_pos) {
            dBodySetPosition (obj[i].body,
                              dRandReal()*2-1,dRandReal()*2-1,dRandReal()+3);
            dRFromAxisAndAngle (R,dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
                                dRandReal()*2.0-1.0,dRandReal()*10.0-5.0);
        }
        else {
            dReal maxheight = 0;
            for (k=0; k<num; k++) {
                const dReal *pos = dBodyGetPosition (obj[k].body);
                if (pos[2] > maxheight) maxheight = pos[2];
            }
            dBodySetPosition (obj[i].body, 0,0,maxheight+1);
            dRFromAxisAndAngle (R,0,0,1,dRandReal()*10.0-5.0);
        }
        dBodySetRotation (obj[i].body,R);
        dBodySetData (obj[i].body,(void*)(size_t)i);

        if (cmd == 'b') {
            dMassSetBox (&m,DENSITY,sides[0],sides[1],sides[2]);
            obj[i].geom[0] = dCreateBox (space,sides[0],sides[1],sides[2]);
        }
        else if (cmd == 'c') {
            sides[0] *= 0.5;
            dMassSetCapsule (&m,DENSITY,3,sides[0],sides[1]);
            obj[i].geom[0] = dCreateCapsule (space,sides[0],sides[1]);
        } else if (cmd == 'v') {

            dMassSetBox (&m,DENSITY,0.25,0.25,0.25);
            obj[i].geom[0] = dCreateConvex(space,
                                           planes,
                                           planecount,
                                           points,
                                           pointcount,
                                           polygons);
        }
        else if (cmd == 'y') {
            sides[1] *= 0.5;
            dMassSetCylinder (&m,DENSITY,3,sides[0],sides[1]);
            obj[i].geom[0] = dCreateCylinder (space,sides[0],sides[1]);
        }
	else if (cmd == 's') {
            sides[0] *= 0.5;
            dMassSetSphere (&m,DENSITY,sides[0]);
            obj[i].geom[0] = dCreateSphere (space,sides[0]);
        }
        else if (cmd == 'm') {
            dTriMeshDataID new_tmdata = dGeomTriMeshDataCreate();
            dGeomTriMeshDataBuildSingle(new_tmdata, &Vertices[0], 3 * sizeof(float), VertexCount, 
                                        (dTriIndex*)&Indices[0], IndexCount, 3 * sizeof(dTriIndex));
            dGeomTriMeshDataPreprocess2(new_tmdata, (1U << dTRIDATAPREPROCESS_BUILD_FACE_ANGLES), NULL);


            obj[i].geom[0] = dCreateTriMesh(space, new_tmdata, 0, 0, 0);

            // remember the mesh's dTriMeshDataID on its userdata for convenience.
            dGeomSetData(obj[i].geom[0], new_tmdata);

            dMassSetTrimesh( &m, DENSITY, obj[i].geom[0] );
            printf("mass at %f %f %f\n", m.c[0], m.c[1], m.c[2]);
            dGeomSetPosition(obj[i].geom[0], -m.c[0], -m.c[1], -m.c[2]);
            dMassTranslate(&m, -m.c[0], -m.c[1], -m.c[2]);
        }
        else if (cmd == 'x') {

            setBody = true;
            // start accumulating masses for the composite geometries
            dMass m2;
            dMassSetZero (&m);

            dReal dpos[GPB][3];	// delta-positions for composite geometries
            dMatrix3 drot[GPB];
      
            // set random delta positions
            for (j=0; j<GPB; j++)
                for (k=0; k<3; k++)
                    dpos[j][k] = dRandReal()*0.3-0.15;
    
            for (k=0; k<GPB; k++) {
                if (k==0) {
                    dReal radius = dRandReal()*0.25+0.05;
                    obj[i].geom[k] = dCreateSphere (space,radius);
                    dMassSetSphere (&m2,DENSITY,radius);
                } else if (k==1) {
                    obj[i].geom[k] = dCreateBox(space,sides[0],sides[1],sides[2]);
                    dMassSetBox(&m2,DENSITY,sides[0],sides[1],sides[2]);
                } else {
                    dReal radius = dRandReal()*0.1+0.05;
                    dReal length = dRandReal()*1.0+0.1;
                    obj[i].geom[k] = dCreateCapsule(space,radius,length);
                    dMassSetCapsule(&m2,DENSITY,3,radius,length);
                }

                dRFromAxisAndAngle(drot[k],dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
                                   dRandReal()*2.0-1.0,dRandReal()*10.0-5.0);
                dMassRotate(&m2,drot[k]);
		
                dMassTranslate(&m2,dpos[k][0],dpos[k][1],dpos[k][2]);

                // add to the total mass
                dMassAdd(&m,&m2);

            }
            for (k=0; k<GPB; k++) {
                dGeomSetBody(obj[i].geom[k],obj[i].body);
                dGeomSetOffsetPosition(obj[i].geom[k],
                                       dpos[k][0]-m.c[0],
                                       dpos[k][1]-m.c[1],
                                       dpos[k][2]-m.c[2]);
                dGeomSetOffsetRotation(obj[i].geom[k], drot[k]);
            }
            dMassTranslate(&m,-m.c[0],-m.c[1],-m.c[2]);
            dBodySetMass(obj[i].body,&m);

        }

        if (!setBody) { // avoid calling for composite geometries
            for (k=0; k < GPB; k++)
                if (obj[i].geom[k])
                    dGeomSetBody(obj[i].geom[k],obj[i].body);

            dBodySetMass(obj[i].body,&m);
        }
    }

    if (cmd == ' ') {
        selected++;
        if (selected >= num) selected = 0;
        if (selected < 0) selected = 0;
    }
    else if (cmd == 'd' && selected >= 0 && selected < num) {
        dBodyDisable (obj[selected].body);
    }
    else if (cmd == 'e' && selected >= 0 && selected < num) {
        dBodyEnable (obj[selected].body);
    }
    else if (cmd == 'a') {
        show_aabb ^= 1;
    }
    else if (cmd == 't') {
        show_contacts ^= 1;
    }
    else if (cmd == 'r') {
        random_pos ^= 1;
    }
}
Ejemplo n.º 13
0
static void command (int cmd)
{
  size_t i;
  int j,k;
  dReal sides[3];
  dMass m;
  int setBody;
  
  cmd = locase (cmd);
  if (cmd == 'b' || cmd == 's' || cmd == 'c' || cmd == 'x' || cmd == 'y' || cmd == 'v')
  {
    setBody = 0;
    if (num < NUM) {
      i = num;
      num++;
    }
    else {
      i = nextobj;
      nextobj++;
      if (nextobj >= num) nextobj = 0;

      // destroy the body and geoms for slot i
      dBodyDestroy (obj[i].body);
      for (k=0; k < GPB; k++) {
	if (obj[i].geom[k]) dGeomDestroy (obj[i].geom[k]);
      }
      memset (&obj[i],0,sizeof(obj[i]));
    }

    obj[i].body = dBodyCreate (world);
    for (k=0; k<3; k++) sides[k] = dRandReal()*0.5+0.1;

    dMatrix3 R;
    if (random_pos) 
      {
	dBodySetPosition (obj[i].body,
			  dRandReal()*2-1,dRandReal()*2-1,dRandReal()+2);
	dRFromAxisAndAngle (R,dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
			    dRandReal()*2.0-1.0,dRandReal()*10.0-5.0);
      }
    else 
      {
	dReal maxheight = 0;
	for (k=0; k<num; k++) 
	  {
	    const dReal *pos = dBodyGetPosition (obj[k].body);
	    if (pos[2] > maxheight) maxheight = pos[2];
	  }
	dBodySetPosition (obj[i].body, 0,0,maxheight+1);
	dRSetIdentity (R);
	//dRFromAxisAndAngle (R,0,0,1,/*dRandReal()*10.0-5.0*/0);
      }
    dBodySetRotation (obj[i].body,R);
    dBodySetData (obj[i].body,(void*) i);

    if (cmd == 'b') {
      dMassSetBox (&m,DENSITY,sides[0],sides[1],sides[2]);
      obj[i].geom[0] = dCreateBox (space,sides[0],sides[1],sides[2]);
    }
    else if (cmd == 'c') {
      sides[0] *= 0.5;
      dMassSetCapsule (&m,DENSITY,3,sides[0],sides[1]);
      obj[i].geom[0] = dCreateCapsule (space,sides[0],sides[1]);
    }
    //<---- Convex Object    
    else if (cmd == 'v') 
      {
	dMassSetBox (&m,DENSITY,0.25,0.25,0.25);
#if 0
	obj[i].geom[0] = dCreateConvex (space,
					planes,
					planecount,
					points,
					pointcount,
					polygons);
#else
	obj[i].geom[0] = dCreateConvex (space,
					Sphere_planes,
					Sphere_planecount,
					Sphere_points,
					Sphere_pointcount,
					Sphere_polygons);
#endif
      }
    //----> Convex Object
    else if (cmd == 'y') {
      dMassSetCylinder (&m,DENSITY,3,sides[0],sides[1]);
      obj[i].geom[0] = dCreateCylinder (space,sides[0],sides[1]);
    }
    else if (cmd == 's') {
      sides[0] *= 0.5;
      dMassSetSphere (&m,DENSITY,sides[0]);
      obj[i].geom[0] = dCreateSphere (space,sides[0]);
    }
    else if (cmd == 'x' && USE_GEOM_OFFSET) {
      setBody = 1;
      // start accumulating masses for the encapsulated geometries
      dMass m2;
      dMassSetZero (&m);

      dReal dpos[GPB][3];	// delta-positions for encapsulated geometries
      dMatrix3 drot[GPB];
      
      // set random delta positions
      for (j=0; j<GPB; j++) {
		for (k=0; k<3; k++) dpos[j][k] = dRandReal()*0.3-0.15;
      }
    
      for (k=0; k<GPB; k++) {
		if (k==0) {
		  dReal radius = dRandReal()*0.25+0.05;
		  obj[i].geom[k] = dCreateSphere (space,radius);
		  dMassSetSphere (&m2,DENSITY,radius);
		}
		else if (k==1) {
		  obj[i].geom[k] = dCreateBox (space,sides[0],sides[1],sides[2]);
		  dMassSetBox (&m2,DENSITY,sides[0],sides[1],sides[2]);
		}
		else {
		  dReal radius = dRandReal()*0.1+0.05;
		  dReal length = dRandReal()*1.0+0.1;
		  obj[i].geom[k] = dCreateCapsule (space,radius,length);
		  dMassSetCapsule (&m2,DENSITY,3,radius,length);
		}

		dRFromAxisAndAngle (drot[k],dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
					dRandReal()*2.0-1.0,dRandReal()*10.0-5.0);
		dMassRotate (&m2,drot[k]);
		
		dMassTranslate (&m2,dpos[k][0],dpos[k][1],dpos[k][2]);

		// add to the total mass
		dMassAdd (&m,&m2);
		
	}
      for (k=0; k<GPB; k++) {
		dGeomSetBody (obj[i].geom[k],obj[i].body);
		dGeomSetOffsetPosition (obj[i].geom[k],
			  dpos[k][0]-m.c[0],
			  dpos[k][1]-m.c[1],
			  dpos[k][2]-m.c[2]);
		dGeomSetOffsetRotation(obj[i].geom[k], drot[k]);
      }
      dMassTranslate (&m,-m.c[0],-m.c[1],-m.c[2]);
	  dBodySetMass (obj[i].body,&m);
		
    }
    else if (cmd == 'x') {
      dGeomID g2[GPB];		// encapsulated geometries
      dReal dpos[GPB][3];	// delta-positions for encapsulated geometries

      // start accumulating masses for the encapsulated geometries
      dMass m2;
      dMassSetZero (&m);

      // set random delta positions
      for (j=0; j<GPB; j++) {
	for (k=0; k<3; k++) dpos[j][k] = dRandReal()*0.3-0.15;
      }

      for (k=0; k<GPB; k++) {
	obj[i].geom[k] = dCreateGeomTransform (space);
	dGeomTransformSetCleanup (obj[i].geom[k],1);
	if (k==0) {
	  dReal radius = dRandReal()*0.25+0.05;
	  g2[k] = dCreateSphere (0,radius);
	  dMassSetSphere (&m2,DENSITY,radius);
	}
	else if (k==1) {
	  g2[k] = dCreateBox (0,sides[0],sides[1],sides[2]);
	  dMassSetBox (&m2,DENSITY,sides[0],sides[1],sides[2]);
	}
	else {
	  dReal radius = dRandReal()*0.1+0.05;
	  dReal length = dRandReal()*1.0+0.1;
	  g2[k] = dCreateCapsule (0,radius,length);
	  dMassSetCapsule (&m2,DENSITY,3,radius,length);
	}
	dGeomTransformSetGeom (obj[i].geom[k],g2[k]);

	// set the transformation (adjust the mass too)
	dGeomSetPosition (g2[k],dpos[k][0],dpos[k][1],dpos[k][2]);
	dMatrix3 Rtx;
	dRFromAxisAndAngle (Rtx,dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
			    dRandReal()*2.0-1.0,dRandReal()*10.0-5.0);
	dGeomSetRotation (g2[k],Rtx);
	dMassRotate (&m2,Rtx);

	// Translation *after* rotation
	dMassTranslate (&m2,dpos[k][0],dpos[k][1],dpos[k][2]);

	// add to the total mass
	dMassAdd (&m,&m2);
      }

      // move all encapsulated objects so that the center of mass is (0,0,0)
      for (k=0; k<GPB; k++) {
	dGeomSetPosition (g2[k],
			  dpos[k][0]-m.c[0],
			  dpos[k][1]-m.c[1],
			  dpos[k][2]-m.c[2]);
      }
      dMassTranslate (&m,-m.c[0],-m.c[1],-m.c[2]);
    }

    if (!setBody)
     for (k=0; k < GPB; k++) {
      if (obj[i].geom[k]) dGeomSetBody (obj[i].geom[k],obj[i].body);
     }

    dBodySetMass (obj[i].body,&m);
  }

  if (cmd == ' ') {
    selected++;
    if (selected >= num) selected = 0;
    if (selected < 0) selected = 0;
  }
  else if (cmd == 'd' && selected >= 0 && selected < num) {
    dBodyDisable (obj[selected].body);
  }
  else if (cmd == 'e' && selected >= 0 && selected < num) {
    dBodyEnable (obj[selected].body);
  }
  else if (cmd == 'a') {
    show_aabb ^= 1;
  }
  else if (cmd == 't') {
    show_contacts ^= 1;
  }
  else if (cmd == 'r') {
    random_pos ^= 1;
  }
  else if (cmd == '1') {
    write_world = 1;
  }
  else if (cmd == 'p'&& selected >= 0)
  {
    const dReal* pos = dGeomGetPosition(obj[selected].geom[0]);
    const dReal* rot = dGeomGetRotation(obj[selected].geom[0]);
    printf("POSITION:\n\t[%f,%f,%f]\n\n",pos[0],pos[1],pos[2]);
    printf("ROTATION:\n\t[%f,%f,%f,%f]\n\t[%f,%f,%f,%f]\n\t[%f,%f,%f,%f]\n\n",
           rot[0],rot[1],rot[2],rot[3],
           rot[4],rot[5],rot[6],rot[7],
           rot[8],rot[9],rot[10],rot[11]);
  }
  else if (cmd == 'f' && selected >= 0 && selected < num) {
          if (dBodyIsEnabled(obj[selected].body))
            doFeedback = 1;
  }
}
Ejemplo n.º 14
0
int main (int argc, char **argv)
{
  dMass m;

  // setup pointers to drawstuff callback functions
  dsFunctions fn;
  fn.version = DS_VERSION;
  fn.start = &start;
  fn.step = &simLoop;
  fn.command = &command;
  fn.stop = 0;
  fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;

  // create world
  dInitODE2(0);
  world = dWorldCreate();
  space = dHashSpaceCreate (0);
  contactgroup = dJointGroupCreate (0);
  dWorldSetGravity (world,0,0,-9.8);
  dWorldSetQuickStepNumIterations (world, 32);

  dCreatePlane (space,0,0,1, 0.0);

  cylbody = dBodyCreate (world);
  dQuaternion q;
#if 0
  dQFromAxisAndAngle (q,1,0,0,M_PI*0.5);
#else
//  dQFromAxisAndAngle (q,1,0,0, M_PI * 1.0);
  dQFromAxisAndAngle (q,1,0,0, M_PI * -0.77);
#endif
  dBodySetQuaternion (cylbody,q);
  dMassSetCylinder (&m,1.0,3,CYLRADIUS,CYLLENGTH);
  dBodySetMass (cylbody,&m);
  cylgeom = dCreateCylinder(0, CYLRADIUS, CYLLENGTH);
  dGeomSetBody (cylgeom,cylbody);
  dBodySetPosition (cylbody, 0, 0, 3);
  dSpaceAdd (space, cylgeom);

  sphbody = dBodyCreate (world);
  dMassSetSphere (&m,1,SPHERERADIUS);
  dBodySetMass (sphbody,&m);
  sphgeom = dCreateSphere(0, SPHERERADIUS);
  dGeomSetBody (sphgeom,sphbody);
  dBodySetPosition (sphbody, 0, 0, 5.5);
  dSpaceAdd (space, sphgeom);

  // run simulation
  dsSimulationLoop (argc,argv,352,288,&fn);

  dJointGroupEmpty (contactgroup);
  dJointGroupDestroy (contactgroup);

  dGeomDestroy(sphgeom);
  dGeomDestroy (cylgeom);

  dSpaceDestroy (space);
  dWorldDestroy (world);
  dCloseODE();
  return 0;
}
Ejemplo n.º 15
0
static void command (int cmd)
{
    size_t i;
    int k;
    dReal sides[3];
    dMass m;
    int setBody;
  
    cmd = locase (cmd);
    if (cmd == 'b' || cmd == 's' || cmd == 'c' || cmd == 'y')
        {
            setBody = 0;
            if (num < NUM) {
                i = num;
                num++;
            }
            else {
                i = nextobj;
                nextobj++;
                if (nextobj >= num) nextobj = 0;

                // destroy the body and geoms for slot i
                if (obj[i].body) {
                  dBodyDestroy (obj[i].body);
                }
                for (k=0; k < GPB; k++) {
                    if (obj[i].geom[k]) {
                      dGeomDestroy (obj[i].geom[k]);
                    }
                }
                memset (&obj[i],0,sizeof(obj[i]));
            }

            obj[i].body = dBodyCreate (world);
            for (k=0; k<3; k++) sides[k] = dRandReal()*0.5+0.1;

            dMatrix3 R;
            if (random_pos) 
                {
                    dBodySetPosition (obj[i].body,
                                      dRandReal()*2-1 + platpos[0],
                                      dRandReal()*2-1 + platpos[1],
                                      dRandReal()+2 + platpos[2]);
                    dRFromAxisAndAngle (R,dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
                                        dRandReal()*2.0-1.0,dRandReal()*10.0-5.0);
                }
            else 
                {
                    dBodySetPosition (obj[i].body, 
                                      platpos[0],
                                      platpos[1],
                                      platpos[2]+2);
                    dRSetIdentity (R);
                }
            dBodySetRotation (obj[i].body,R);
            dBodySetData (obj[i].body,(void*) i);

            if (cmd == 'b') {
                dMassSetBox (&m,DENSITY,sides[0],sides[1],sides[2]);
                obj[i].geom[0] = dCreateBox (space,sides[0],sides[1],sides[2]);
            }
            else if (cmd == 'c') {
                sides[0] *= 0.5;
                dMassSetCapsule (&m,DENSITY,3,sides[0],sides[1]);
                obj[i].geom[0] = dCreateCapsule (space,sides[0],sides[1]);
            }
            else if (cmd == 'y') {
                dMassSetCylinder (&m,DENSITY,3,sides[0],sides[1]);
                obj[i].geom[0] = dCreateCylinder (space,sides[0],sides[1]);
            }
            else if (cmd == 's') {
                sides[0] *= 0.5;
                dMassSetSphere (&m,DENSITY,sides[0]);
                obj[i].geom[0] = dCreateSphere (space,sides[0]);
            }

            if (!setBody)
                for (k=0; k < GPB; k++) {
                    if (obj[i].geom[k]) {
                      dGeomSetBody (obj[i].geom[k],obj[i].body);
                    }
                }

            dBodySetMass (obj[i].body,&m);
        }
    else if (cmd == 'a') {
        show_aabb ^= 1;
    }
    else if (cmd == 't') {
        show_contacts ^= 1;
    }
    else if (cmd == 'r') {
        random_pos ^= 1;
    }
    else if (cmd == '1') {
        write_world = 1;
    }
    else if (cmd == ' ') {
        mov_time = 0;
    }
    else if (cmd == 'm') {
        mov_type = mov_type==1 ? 2 : 1;
        mov_time = 0;
    }
}
Ejemplo n.º 16
0
State* init() {
    State* state = new State();
    dInitODE();

    SDL_Init(SDL_INIT_EVERYTHING);

    state->screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL);
    SDL_WM_SetCaption("Physics", NULL);
    SDL_Flip(state->screen);

    SDL_ShowCursor(SDL_DISABLE);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(100, (float)WIDTH/HEIGHT, 0.5, 1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    GLfloat light_ambient[] = { 1, 1, 1, 1 };
    glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
    GLfloat lightpos[] = {0, 4, 0, 1};
    glLightfv(GL_LIGHT0, GL_POSITION, lightpos);

    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
    glShadeModel(GL_SMOOTH);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glEnable(GL_DEPTH_TEST);

    glEnable(GL_CULL_FACE);

    glClearColor(0, 0, 0, 1);

    state->posx = 0;//21;
    state->posy = 4;//8;
    state->posz = 5;//21;
    state->rotx = 0;
    state->roty = 0;//-40;
    state->rotz = 0;

    state->wkey = false;
    state->akey = false;
    state->skey = false;
    state->dkey = false;
    state->gkey = false;

    state->simSpeed = 60;

    state->carcam = false;

    state->carbodydrawable = new Drawable("objs/carbody.obj");
    state->carwheeldrawable = new Drawable("objs/carwheel.obj");
    state->map = new Drawable("objs/jump2.obj");
    state->cube = new Drawable("objs/cube.obj");
//    state->monkey = new Drawable("objs/monkey.obj");

    state->world = dWorldCreate();
    dWorldSetGravity(state->world, 0, -9.8, 0);

    state->worldSpace = dHashSpaceCreate(0);

    const double carHeight = 0.65;
    const double carZ = 90;
    const double carX = 0;
    const float speed = -1000;
    const float force = 200;

    state->carbodybody = dBodyCreate(state->world);
    dBodySetPosition(state->carbodybody, carX, carHeight, carZ);
    dMass bodymass;
    dMassSetBoxTotal(&bodymass, 100, 2, 4, 1);
    dBodySetMass(state->carbodybody, &bodymass);
    dGeomID carbodygeom = dCreateBox(state->worldSpace, 2, 1, 4);
    dGeomSetBody(carbodygeom, state->carbodybody);

    state->flcarwheelbody = dBodyCreate(state->world);
    dBodySetPosition(state->flcarwheelbody, carX-1.5, carHeight - 0.5, carZ+2);
    const dMatrix3 m = { 0, 0, 1, 0
                       , 0, 1, 0, 0
                       , 0, 0, 1, 0 };
    dBodySetRotation(state->flcarwheelbody, m);
    dMass wheelmass;
    dMassSetCylinder(&wheelmass, 0.1, 2, 0.5, 0.2);
    dBodySetMass(state->flcarwheelbody, &wheelmass);
    dJointID joint = dJointCreateHinge(state->world, 0);
    dJointAttach(joint, state->carbodybody, state->flcarwheelbody);
    dJointSetHingeAnchor(joint, carX-1.5, carHeight - 0.5, carZ+2);
    dJointSetHingeAxis(joint, 1, 0, 0);
    dGeomID flcarwheelgeom = dCreateCylinder(state->worldSpace, 0.5, 0.2);
    dGeomSetBody(flcarwheelgeom, state->flcarwheelbody);

    dJointID motor = dJointCreateAMotor(state->world, 0);
    dJointAttach(motor, state->flcarwheelbody, state->carbodybody);
    dJointSetAMotorNumAxes(motor, 1);
    dJointSetAMotorAxis(motor, 0, 1, 1, 0, 0);
    dJointSetAMotorParam(motor, dParamVel, speed);
    dJointSetAMotorParam(motor, dParamFMax, force);

    state->frcarwheelbody = dBodyCreate(state->world);
    dBodySetPosition(state->frcarwheelbody, carX+1.5, carHeight - 0.5, carZ+2);
    dBodySetRotation(state->frcarwheelbody, m);
    dBodySetMass(state->frcarwheelbody, &wheelmass);
    joint = dJointCreateHinge(state->world, 0);
    dJointAttach(joint, state->carbodybody, state->frcarwheelbody);
    dJointSetHingeAnchor(joint, carX+1.5, carHeight - 0.5, carZ+2);
    dJointSetHingeAxis(joint, 1, 0, 0);
    dGeomID frcarwheelgeom = dCreateCylinder(state->worldSpace, 0.5, 0.2);
    dGeomSetBody(frcarwheelgeom, state->frcarwheelbody);

    motor = dJointCreateAMotor(state->world, 0);
    dJointAttach(motor, state->frcarwheelbody, state->carbodybody);
    dJointSetAMotorNumAxes(motor, 1);
    dJointSetAMotorAxis(motor, 0, 1, 1, 0, 0);
    dJointSetAMotorParam(motor, dParamVel, speed);
    dJointSetAMotorParam(motor, dParamFMax, force);

    state->blcarwheelbody = dBodyCreate(state->world);
    dBodySetPosition(state->blcarwheelbody, carX-1.5, carHeight - 0.5, carZ-2);
    dBodySetRotation(state->blcarwheelbody, m);
    dBodySetMass(state->blcarwheelbody, &wheelmass);
    joint = dJointCreateHinge(state->world, 0);
    dJointAttach(joint, state->carbodybody, state->blcarwheelbody);
    dJointSetHingeAnchor(joint, carX-1.5, carHeight - 0.5, carZ-2);
    dJointSetHingeAxis(joint, 1, 0, 0);
    dGeomID blcarwheelgeom = dCreateCylinder(state->worldSpace, 0.5, 0.2);
    dGeomSetBody(blcarwheelgeom, state->blcarwheelbody);

    motor = dJointCreateAMotor(state->world, 0);
    dJointAttach(motor, state->blcarwheelbody, state->carbodybody);
    dJointSetAMotorNumAxes(motor, 1);
    dJointSetAMotorAxis(motor, 0, 1, 1, 0, 0);
    dJointSetAMotorParam(motor, dParamVel, speed);
    dJointSetAMotorParam(motor, dParamFMax, force);

    state->brcarwheelbody = dBodyCreate(state->world);
    dBodySetPosition(state->brcarwheelbody, carX+1.5, carHeight - 0.5, carZ-2);
    dBodySetRotation(state->brcarwheelbody, m);
    dBodySetMass(state->brcarwheelbody, &wheelmass);
    joint = dJointCreateHinge(state->world, 0);
    dJointAttach(joint, state->carbodybody, state->brcarwheelbody);
    dJointSetHingeAnchor(joint, carX+1.5, carHeight - 0.5, carZ-2);
    dJointSetHingeAxis(joint, 1, 0, 0);
    dGeomID brcarwheelgeom = dCreateCylinder(state->worldSpace, 0.5, 0.2);
    dGeomSetBody(brcarwheelgeom, state->brcarwheelbody);

    motor = dJointCreateAMotor(state->world, 0);
    dJointAttach(motor, state->brcarwheelbody, state->carbodybody);
    dJointSetAMotorNumAxes(motor, 1);
    dJointSetAMotorAxis(motor, 0, 1, 1, 0, 0);
    dJointSetAMotorParam(motor, dParamVel, speed);
    dJointSetAMotorParam(motor, dParamFMax, force);

    state->var = new double[3];

    state->var = dBodyGetPosition(state->carbodybody);
//    cout << state->var[0] << " " << state->var[1] << " " << state->var[2] << endl;
    //TODO check if translation matrix from dBody can be used.

    dSpaceID cubespace = dHashSpaceCreate(state->worldSpace);

    for(int i = 0; i < NUM_CUBES/10; i++) {
        for(int k = 0; k < 10; k++) {
            state->cubebody[i*10+k] = dBodyCreate(state->world);
            dBodySetAutoDisableFlag(state->cubebody[i*10+k], 1);
            dBodySetPosition(state->cubebody[i*10+k], (i*2.01)-4, 0.9 + 2.01*k, -70);
            dGeomID cubegeom = dCreateBox(cubespace, 2, 2, 2);
            dGeomSetBody(cubegeom, state->cubebody[i*10+k]);
        }
    }

    {
        int indexSize = state->map->vertices.size()/3;
        unsigned int* index = new unsigned int[indexSize];
        for(int i = 0; i < indexSize; i++)
            index[i] = i;

        dTriMeshDataID triMeshData = dGeomTriMeshDataCreate();
        dGeomTriMeshDataBuildSingle(triMeshData, state->map->vertices.data(), 12, state->map->vertices.size()/3, index, indexSize, 12);
        dGeomID mapGeom = dCreateTriMesh(state->worldSpace, triMeshData, NULL, NULL, NULL);
        dGeomSetPosition(mapGeom, 0, 0, 0);
    }
//    state->monkeyBody = dBodyCreate(state->world);
//    {
//        int indexSize = state->monkey->vertices.size()/3;
//        unsigned int* index = new unsigned int[indexSize];
//        for(int i = 0; i < indexSize; i++)
//            index[i] = i;

//        dTriMeshDataID triMeshData = dGeomTriMeshDataCreate();
//        dGeomTriMeshDataBuildSingle(triMeshData, state->monkey->vertices.data(), 12, state->monkey->vertices.size()/3, index, indexSize, 12);
//        dGeomID monkeyGeom = dCreateTriMesh(state->worldSpace, triMeshData, NULL, NULL, NULL);
//        dGeomSetPosition(monkeyGeom, 0, 2, 0);
//        dGeomSetBody(monkeyGeom, state->monkeyBody);
//    }

    state->physicsContactgroup = dJointGroupCreate(0);

    return state;
}
Ejemplo n.º 17
0
void SCylinderParts::set(dWorldID w, dSpaceID space)
{
	double radius = m_cmpnt.radius();
	double length = m_cmpnt.length();

// konao
	//LOG_MSG(("[SCylinderParts::set] ODE geom created (r, l)=(%f, %f) [%s:%d]\n", radius, length, __FILE__, __LINE__))
	// TODO: Ideally, cylinder should be constructed here. However, collision detection
	// between two cylinders could not be realized. So, Capsule is required
	// by okamoto@tome on 2011-10-12

	//dGeomID geom = dCreateCapsule(0, radius, length);
	dGeomID geom = dCreateCylinder(0, radius, length);

	m_odeobj = ODEObjectContainer::getInstance()->createODEObj
	(
		w,
		geom,
		0.9,
		0.01,
		0.5,
		0.5,
		0.8,
		0.001,
		0.0
	);


	dBodyID body = m_odeobj->body();
	dMass m;
	dMassSetZero(&m);
	//dMassSetCapsule(&m, DENSITY, 1, radius, length);
	dMassSetCylinder(&m, DENSITY, 1, radius, length); //TODO: mass of the cylinder should be configurable

	dMassAdjust(&m, m_mass);
	dBodySetMass(body, &m);
	dGeomSetOffsetPosition(geom, m_posx, m_posy, m_posz); // gap between ODE shape and body

	// set the long axis as y axis
	dReal offq[4] = {0.707, 0.707, 0.0, 0.0};
	dReal offq2[4] = {m_inirot.qw(), m_inirot.qx(), m_inirot.qy(), m_inirot.qz()};

	dQuaternion qua;
	dQMultiply2(qua, offq2, offq);
	dGeomSetOffsetQuaternion(geom, qua);
	//dGeomSetOffsetQuaternion(geom, offq2);

	//dReal tmpq[4] = {0.707, 0.0, 0.0, 0.707};
	//dGeomSetQuaternion(geom, tmpq);
	//dBodySetQuaternion(body, tmpq);
	
	/*TODO: Consideration is required whether this procedure is needed
	 * Reflection of orientation of the cylinder
	 * dMatrix3 R;
	 * dRFromAxisAndAngle(dMatrix3 R, dReal rx, dReal ry, dReal rz,  dReal angle)
	 * dRFromAxisAndAngle(R,x_axis,y_axis,z_axis,angleData);
	 * dBodySetRotation(body,R);  // Request of actual rotation
	*/

	// Not used, deleted by inamura
	// real part of the quaternion
	// double q = cos(angleData/2.0);
	// imaginary part of the quaternion
	// double i,j,k;
	// i = x_axis * sin(angleData/2.0);
	// j = y_axis * sin(angleData/2.0);
	// k = z_axis * sin(angleData/2.0);

	m_rot.setQuaternion(1.0, 0.0, 0.0, 0.0);

	dSpaceAdd(space, geom);
	dBodySetData(body, this);
}
static void command (int cmd)
{
  int i,j,k;
  dReal sides[3];
  dMass m;

  cmd = locase (cmd);
  if (cmd == 'b' || cmd == 's' || cmd == 'c' || cmd == 'x' || cmd == 'm' || cmd == 'y' ) {
    if (num < NUM) {
      i = num;
      num++;
    }
    else {
      i = nextobj;
      nextobj++;
      if (nextobj >= num) nextobj = 0;

      // destroy the body and geoms for slot i
      dBodyDestroy (obj[i].body);
      for (k=0; k < GPB; k++) {
	if (obj[i].geom[k]) dGeomDestroy (obj[i].geom[k]);
      }
      memset (&obj[i],0,sizeof(obj[i]));
    }

    obj[i].body = dBodyCreate (world);
    for (k=0; k<3; k++) sides[k] = dRandReal()*0.5+0.1;

    dMatrix3 R;
    if (random_pos) {
      dBodySetPosition (obj[i].body,
			dRandReal()*2-1,dRandReal()*2-1,dRandReal()+3);
      dRFromAxisAndAngle (R,dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
			  dRandReal()*2.0-1.0,dRandReal()*10.0-5.0);
    }
    else {
      dReal maxheight = 0;
      for (k=0; k<num; k++) {
	const dReal *pos = dBodyGetPosition (obj[k].body);
	if (pos[2] > maxheight) maxheight = pos[2];
      }
      dBodySetPosition (obj[i].body, 0,0,maxheight+1);
      dRFromAxisAndAngle (R,0,0,1,dRandReal()*10.0-5.0);
    }
    dBodySetRotation (obj[i].body,R);
    dBodySetData (obj[i].body,(void*)(size_t)i);

    if (cmd == 'b') {
      dMassSetBox (&m,DENSITY,sides[0],sides[1],sides[2]);
      obj[i].geom[0] = dCreateBox (space,sides[0],sides[1],sides[2]);
    }
    else if (cmd == 'c') {
      sides[0] *= 0.5;
      dMassSetCapsule (&m,DENSITY,3,sides[0],sides[1]);
      obj[i].geom[0] = dCreateCapsule (space,sides[0],sides[1]);
    }
    else if (cmd == 'y') {
      sides[1] *= 0.5;
      dMassSetCylinder (&m,DENSITY,3,sides[0],sides[1]);
      obj[i].geom[0] = dCreateCylinder (space,sides[0],sides[1]);
    }
	else if (cmd == 's') {
      sides[0] *= 0.5;
      dMassSetSphere (&m,DENSITY,sides[0]);
      obj[i].geom[0] = dCreateSphere (space,sides[0]);
    }
    else if (cmd == 'm') {
      dTriMeshDataID new_tmdata = dGeomTriMeshDataCreate();
      dGeomTriMeshDataBuildSingle(new_tmdata, &Vertices[0], 3 * sizeof(float), VertexCount, 
		  (dTriIndex*)&Indices[0], IndexCount, 3 * sizeof(dTriIndex));

      obj[i].geom[0] = dCreateTriMesh(space, new_tmdata, 0, 0, 0);

      // remember the mesh's dTriMeshDataID on its userdata for convenience.
      dGeomSetData(obj[i].geom[0], new_tmdata);

      dMassSetTrimesh( &m, DENSITY, obj[i].geom[0] );
      printf("mass at %f %f %f\n", m.c[0], m.c[1], m.c[2]);
      dGeomSetPosition(obj[i].geom[0], -m.c[0], -m.c[1], -m.c[2]);
      dMassTranslate(&m, -m.c[0], -m.c[1], -m.c[2]);
    }
    else if (cmd == 'x') {
      dGeomID g2[GPB];		// encapsulated geometries
      dReal dpos[GPB][3];	// delta-positions for encapsulated geometries

      // start accumulating masses for the encapsulated geometries
      dMass m2;
      dMassSetZero (&m);

      // set random delta positions
      for (j=0; j<GPB; j++) {
	for (k=0; k<3; k++) dpos[j][k] = dRandReal()*0.3-0.15;
      }

      for (k=0; k<GPB; k++) {
	obj[i].geom[k] = dCreateGeomTransform (space);
	dGeomTransformSetCleanup (obj[i].geom[k],1);
	if (k==0) {
	  dReal radius = dRandReal()*0.25+0.05;
	  g2[k] = dCreateSphere (0,radius);
	  dMassSetSphere (&m2,DENSITY,radius);
	}
	else if (k==1) {
	  g2[k] = dCreateBox (0,sides[0],sides[1],sides[2]);
	  dMassSetBox (&m2,DENSITY,sides[0],sides[1],sides[2]);
	}
	else {
	  dReal radius = dRandReal()*0.1+0.05;
	  dReal length = dRandReal()*1.0+0.1;
	  g2[k] = dCreateCapsule (0,radius,length);
	  dMassSetCapsule (&m2,DENSITY,3,radius,length);
	}
	dGeomTransformSetGeom (obj[i].geom[k],g2[k]);

	// set the transformation (adjust the mass too)
	dGeomSetPosition (g2[k],dpos[k][0],dpos[k][1],dpos[k][2]);
	dMassTranslate (&m2,dpos[k][0],dpos[k][1],dpos[k][2]);
	dMatrix3 Rtx;
	dRFromAxisAndAngle (Rtx,dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
			    dRandReal()*2.0-1.0,dRandReal()*10.0-5.0);
	dGeomSetRotation (g2[k],Rtx);
	dMassRotate (&m2,Rtx);

	// add to the total mass
	dMassAdd (&m,&m2);
      }

      // move all encapsulated objects so that the center of mass is (0,0,0)
      for (k=0; k<2; k++) {
	dGeomSetPosition (g2[k],
			  dpos[k][0]-m.c[0],
			  dpos[k][1]-m.c[1],
			  dpos[k][2]-m.c[2]);
      }
      dMassTranslate (&m,-m.c[0],-m.c[1],-m.c[2]);
    }

    for (k=0; k < GPB; k++) {
      if (obj[i].geom[k]) dGeomSetBody (obj[i].geom[k],obj[i].body);
    }

    dBodySetMass (obj[i].body,&m);
  }

  if (cmd == ' ') {
    selected++;
    if (selected >= num) selected = 0;
    if (selected < 0) selected = 0;
  }
  else if (cmd == 'd' && selected >= 0 && selected < num) {
    dBodyDisable (obj[selected].body);
  }
  else if (cmd == 'e' && selected >= 0 && selected < num) {
    dBodyEnable (obj[selected].body);
  }
  else if (cmd == 'a') {
    show_aabb ^= 1;
  }
  else if (cmd == 't') {
    show_contacts ^= 1;
  }
  else if (cmd == 'r') {
    random_pos ^= 1;
  }
}
void CManipulator::init()
{	
	BORDER=0.01;
	colorManipulator.B=(float)1.0;
	colorManipulator.R=(float)0.5;
	colorManipulator.G=(float)0.3;
	int i;
	double density=1;////////////////////////////////////
	i=0;
	if(i<NUMOFLEGJOINTS)
	{
		jointManipulator[i].paraJoint.modeAxis=ZZ;
		jointManipulator[i].paraJoint.typeJoint=BOX;
		jointManipulator[i].paraJoint.paraManipulatorBox.angle=0;
		jointManipulator[i].paraJoint.paraManipulatorBox.x=0.4;
		jointManipulator[i].paraJoint.paraManipulatorBox.y=0.4;
		jointManipulator[i].paraJoint.paraManipulatorBox.z=0.585;
		//		jointManipulator[i].paraJoint.typeJoint=CYLINDER;
		//jointManipulator[i].paraJoint.paraManipulatorCylinder.angle=0;
		//jointManipulator[i].paraJoint.paraManipulatorCylinder.length=0.585;////////////修改过
		//jointManipulator[i].paraJoint.paraManipulatorCylinder.r=0.2;////////////
		if(jointManipulator[i].paraJoint.typeJoint==CYLINDER)
			dMassSetCylinder(&jointManipulator[i].paraJoint.massBody,density,ZZ,jointManipulator[i].paraJoint.paraManipulatorCylinder.r,jointManipulator[i].paraJoint.paraManipulatorCylinder.length);
		else if(jointManipulator[i].paraJoint.typeJoint==BOX)
			dMassSetBox(&jointManipulator[i].paraJoint.massBody,density,jointManipulator[i].paraJoint.paraManipulatorBox.x,jointManipulator[i].paraJoint.paraManipulatorBox.y,jointManipulator[i].paraJoint.paraManipulatorBox.z);
	}

	i=1;
	if(i<NUMOFLEGJOINTS)
	{
		jointManipulator[i].paraJoint.modeAxis=XX;
		jointManipulator[i].paraJoint.typeJoint=BOX;
		jointManipulator[i].paraJoint.paraManipulatorBox.angle=0;
		jointManipulator[i].paraJoint.paraManipulatorBox.x=0.4;
		jointManipulator[i].paraJoint.paraManipulatorBox.y=0.4;
		jointManipulator[i].paraJoint.paraManipulatorBox.z=0.6;
		if(jointManipulator[i].paraJoint.typeJoint==CYLINDER)
			dMassSetCylinder(&jointManipulator[i].paraJoint.massBody,density,ZZ,jointManipulator[i].paraJoint.paraManipulatorCylinder.r,jointManipulator[i].paraJoint.paraManipulatorCylinder.length);
		else if(jointManipulator[i].paraJoint.typeJoint==BOX)
			dMassSetBox(&jointManipulator[i].paraJoint.massBody,density,jointManipulator[i].paraJoint.paraManipulatorBox.x,jointManipulator[i].paraJoint.paraManipulatorBox.y,jointManipulator[i].paraJoint.paraManipulatorBox.z);
	}

	i=2;
	if(i<NUMOFLEGJOINTS)
	{
		jointManipulator[i].paraJoint.modeAxis=XX;
		jointManipulator[i].paraJoint.typeJoint=BOX;
		jointManipulator[i].paraJoint.paraManipulatorBox.angle=0;
		jointManipulator[i].paraJoint.paraManipulatorBox.x=0.2;
		jointManipulator[i].paraJoint.paraManipulatorBox.y=0.2;
		jointManipulator[i].paraJoint.paraManipulatorBox.z=0.115;
		//		jointManipulator[i].paraJoint.typeJoint=CYLINDER;
		//jointManipulator[i].paraJoint.paraManipulatorCylinder.angle=0;
		//jointManipulator[i].paraJoint.paraManipulatorCylinder.length=0.115;
		//jointManipulator[i].paraJoint.paraManipulatorCylinder.r=0.1;
		if(jointManipulator[i].paraJoint.typeJoint==CYLINDER)
			dMassSetCylinder(&jointManipulator[i].paraJoint.massBody,density,ZZ,jointManipulator[i].paraJoint.paraManipulatorCylinder.r,jointManipulator[i].paraJoint.paraManipulatorCylinder.length);
		else if(jointManipulator[i].paraJoint.typeJoint==BOX)
			dMassSetBox(&jointManipulator[i].paraJoint.massBody,density,jointManipulator[i].paraJoint.paraManipulatorBox.x,jointManipulator[i].paraJoint.paraManipulatorBox.y,jointManipulator[i].paraJoint.paraManipulatorBox.z);
	}

	i=3;
	if(i<NUMOFLEGJOINTS)
	{
		jointManipulator[i].paraJoint.modeAxis=XX;
		jointManipulator[i].paraJoint.typeJoint=BOX;
		jointManipulator[i].paraJoint.paraManipulatorBox.angle=Pi/2;
		jointManipulator[i].paraJoint.paraManipulatorBox.x=0.2;
		jointManipulator[i].paraJoint.paraManipulatorBox.y=0.2;
		jointManipulator[i].paraJoint.paraManipulatorBox.z=0.385;
		if(jointManipulator[i].paraJoint.typeJoint==CYLINDER)
			dMassSetCylinder(&jointManipulator[i].paraJoint.massBody,density,ZZ,jointManipulator[i].paraJoint.paraManipulatorCylinder.r,jointManipulator[i].paraJoint.paraManipulatorCylinder.length);
		else if(jointManipulator[i].paraJoint.typeJoint==BOX)
			dMassSetBox(&jointManipulator[i].paraJoint.massBody,density,jointManipulator[i].paraJoint.paraManipulatorBox.x,jointManipulator[i].paraJoint.paraManipulatorBox.y,jointManipulator[i].paraJoint.paraManipulatorBox.z);
	}

	i=4;
	if(i<NUMOFLEGJOINTS)
	{
		jointManipulator[i].paraJoint.modeAxis=YY;
		jointManipulator[i].paraJoint.typeJoint=BOX;
		jointManipulator[i].paraJoint.paraManipulatorBox.angle=Pi/2;
		jointManipulator[i].paraJoint.paraManipulatorBox.x=0.2;
		jointManipulator[i].paraJoint.paraManipulatorBox.y=0.2;
		jointManipulator[i].paraJoint.paraManipulatorBox.z=0.385;
		if(jointManipulator[i].paraJoint.typeJoint==CYLINDER)
			dMassSetCylinder(&jointManipulator[i].paraJoint.massBody,density,ZZ,jointManipulator[i].paraJoint.paraManipulatorCylinder.r,jointManipulator[i].paraJoint.paraManipulatorCylinder.length);
		else if(jointManipulator[i].paraJoint.typeJoint==BOX)
			dMassSetBox(&jointManipulator[i].paraJoint.massBody,density,jointManipulator[i].paraJoint.paraManipulatorBox.x,jointManipulator[i].paraJoint.paraManipulatorBox.y,jointManipulator[i].paraJoint.paraManipulatorBox.z);
	}

	i=5;
	if(i<NUMOFLEGJOINTS)
	{
		jointManipulator[i].paraJoint.modeAxis=XX;
		jointManipulator[i].paraJoint.typeJoint=BOX;
		jointManipulator[i].paraJoint.paraManipulatorBox.angle=Pi/2;
		jointManipulator[i].paraJoint.paraManipulatorBox.x=0.2;
		jointManipulator[i].paraJoint.paraManipulatorBox.y=0.2;
		jointManipulator[i].paraJoint.paraManipulatorBox.z=0.1;

		//jointManipulator[i].paraJoint.paraManipulatorCylinder.angle=Pi/2;
		//jointManipulator[i].paraJoint.paraManipulatorCylinder.length=0.1;
		//jointManipulator[i].paraJoint.paraManipulatorCylinder.r=0.1;
		if(jointManipulator[i].paraJoint.typeJoint==CYLINDER)
			dMassSetCylinder(&jointManipulator[i].paraJoint.massBody,density,ZZ,jointManipulator[i].paraJoint.paraManipulatorCylinder.r,jointManipulator[i].paraJoint.paraManipulatorCylinder.length);
		else if(jointManipulator[i].paraJoint.typeJoint==BOX)
			dMassSetBox(&jointManipulator[i].paraJoint.massBody,density,jointManipulator[i].paraJoint.paraManipulatorBox.x,jointManipulator[i].paraJoint.paraManipulatorBox.y,jointManipulator[i].paraJoint.paraManipulatorBox.z);
	}

	i=6;
	if(i<NUMOFLEGJOINTS)
	{
		jointManipulator[i].paraJoint.modeAxis=YY;
		jointManipulator[i].paraJoint.typeJoint=BOX;
		jointManipulator[i].paraJoint.paraManipulatorBox.angle=Pi/2;
		jointManipulator[i].paraJoint.paraManipulatorBox.x=0.1;
		jointManipulator[i].paraJoint.paraManipulatorBox.y=0.1;
		jointManipulator[i].paraJoint.paraManipulatorBox.z=0.6;
		if(jointManipulator[i].paraJoint.typeJoint==CYLINDER)
			dMassSetCylinder(&jointManipulator[i].paraJoint.massBody,density,ZZ,jointManipulator[i].paraJoint.paraManipulatorCylinder.r,jointManipulator[i].paraJoint.paraManipulatorCylinder.length);
		else if(jointManipulator[i].paraJoint.typeJoint==BOX)
			dMassSetBox(&jointManipulator[i].paraJoint.massBody,density,jointManipulator[i].paraJoint.paraManipulatorBox.x,jointManipulator[i].paraJoint.paraManipulatorBox.y,jointManipulator[i].paraJoint.paraManipulatorBox.z);
	}
	baseManipulator.paraBase.typeJoint=BOX;
	baseManipulator.paraBase.paraManipulatorCylinder.angle=0;
	baseManipulator.paraBase.paraManipulatorCylinder.length=0.1;
	baseManipulator.paraBase.paraManipulatorCylinder.r=1;

	for(i=0;i<NUMOFLEGJOINTS;i++)
	{
		stopparaJoint[i].lowstop=0;
		stopparaJoint[i].histop=0;
		stopparaJoint[i].bounce=1;
		stopparaJoint[i].cfm=0;
	}
}