Adding motors to my table shaped robot made me question the hinges functionality and the cylinders positions. So, I recorded the below 2 videos to share with you and get your feedback if we can attach the legs to the box to get a better looking and functioning table robot, because I think create cylinder function has some errors and I need your kind help to find these errors.
- Video 1: Robot with no motors - https://youtu.be/AJLMVVkmOo8
 - Video 2: Robot with motor - https://youtu.be/HzYo9We-MHA
 
CreateBox function:
Code: Select all
void RagdollDemo::CreateBox(int index, double x, double y, double z, double length, double width, double height)
{
    geom[index] = new btBoxShape(btVector3(width, height, length));
    
    //btDefaultMotionState* motionstate = new btDefaultMotionState();
    btDefaultMotionState* motionstate = new btDefaultMotionState(btTransform(
                                                                             btQuaternion(0, 0, 0, 1),
                                                                             btVector3(x, y, z)
                                                                             ));
    
    btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(
                                                         1.0,                  // mass, in kg. 0 -> Static object, will never move.
                                                         motionstate,
                                                         geom[index],  // collision shape of body
                                                         btVector3(0,0,0)    // local inertia
                                                         );
    
    body[index] = new btRigidBody(rigidBodyCI);
    
    m_dynamicsWorld->addRigidBody(body[index]);
    
}
Code: Select all
void RagdollDemo::CreateCylinder(int index, double x, double y, double z, double diameter, double sideLength, double angle)
{
    geom[index] = new btCylinderShape(btVector3(diameter, sideLength, diameter));
    
    //btDefaultMotionState* motionstate = new btDefaultMotionState();
    btDefaultMotionState* motionstate = new btDefaultMotionState(btTransform(
                                                                             btQuaternion(btVector3(0, 0, 1), angle),
                                                                             btVector3(x, y, z)
                                                                             ));
    
    btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(
                                                         1.0,                  // mass, in kg. 0 -> Static object, will never move.
                                                         motionstate,
                                                         geom[index],  // collision shape of body
                                                         btVector3(0,0,0.5)    // local inertia
                                                         );
    
//    glRotatef(90, 1, 0, 0);
    
    body[index] = new btRigidBody(rigidBodyCI);
    
    m_dynamicsWorld->addRigidBody(body[index]);
}
void RagdollDemo::CreateCylinder2(int index, double x, double y, double z, double diameter, double sideLength, double angle)
{
    geom[index] = new btCylinderShape(btVector3(diameter, sideLength, diameter));
    
    //btDefaultMotionState* motionstate = new btDefaultMotionState();
    btDefaultMotionState* motionstate = new btDefaultMotionState(btTransform(
                                                                             btQuaternion(btVector3(1, 0, 0), angle),
                                                                             btVector3(x, y, z)
                                                                             ));
    
    btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(
                                                         1.0,                  // mass, in kg. 0 -> Static object, will never move.
                                                         motionstate,
                                                         geom[index],  // collision shape of body
                                                         btVector3(0.5,0,0)    // local inertia
                                                         );
    
    //glRotatef(90, 1, 0, 0);
    
    body[index] = new btRigidBody(rigidBodyCI);
    
    m_dynamicsWorld->addRigidBody(body[index]);
}
Code: Select all
void RagdollDemo::CreateHinge(int index, int body1, int body2, double x, double y, double z, double ax, double ay, double az)
{
    btVector3 p(x, y, z);
    btVector3 a(ax, ay, az);
    
    btVector3 p1 = PointWorldToLocal(body1, p);
    btVector3 p2 = PointWorldToLocal(body2, p);
    
    btVector3 a1 = AxisWorldToLocal(body1, a);
    btVector3 a2 = AxisWorldToLocal(body2, a);
    
    // create
    joints[index] = new btHingeConstraint(*body[body1], *body[body2],
                                          p1, p2,
                                          a1, a2, false);
    
    // set limits
    joints[index]->setLimit( (-120. + 0.)*3.14159/180., (120. + 0.)*3.14159/180.);
    
    // Add to simulation
    m_dynamicsWorld->addConstraint( joints[index] , true );
    
Code: Select all
CreateBox(0, 0., 2., 0., 1., 1., 0.2); // Create the box
    // Create the top legs
    CreateCylinder(1, 2., 1., 0., 0.2, 1., -0.6981317008); // Create Leg 1
    CreateCylinder(2, -2., 1., 0., 0.2, 1., 0.6981317008); // Create Leg 2
    CreateCylinder2(3, 0., 1., 2., 0.2, 1., 0.6981317008); // Create Leg 3
    CreateCylinder2(4, 0., 1., -2., 0.2, 1., -0.6981317008); // Create Leg 4
    // Create the bottom legs
    CreateCylinder(5, 3.5, 1., 0., 0.2, 1., 0.6981317008); // Create Leg 5
    CreateCylinder(6, -3.5, 1., 0., 0.2, 1., -0.6981317008); // Create Leg 6
    CreateCylinder2(7, 0., 1., 3.5, 0.2, 1., -0.6981317008); // Create Leg 7
    CreateCylinder2(8, 0., 1., -3.5, 0.2, 1., 0.6981317008); // Create Leg 8
    
    CreateHinge(0, 1,5, 2.7,1.8,0, 0,0,1);
    CreateHinge(1, 2,6, -2.7,1.8,0, 0,0,1);
    CreateHinge(2, 3,7, 0,1.8,2.7, 1,0,0);
    CreateHinge(3, 4,8, 0,1.8,-2.7, 1,0,0);
    
    CreateHinge(4, 0,1, 1.2,2,0, 0,0,1);
    CreateHinge(5, 0,2, -1.2,2,0, 0,0,1);
    CreateHinge(6, 0,3, 0,2,1.2, 1,0,0);
    CreateHinge(7, 0,4, 0,2,-1.2, 1,0,0);