Search code examples
c++game-physicsbulletbulletphysicsrigid-bodies

btCompoundShape added extra shape


Hi I have a problem with btCompoundShape, I create shape from 2 cylinders, but when i run my app, it seems to be 3 items in compundshape. Anyone have an idea why? I add picture and code: enter image description here

This little thing bellow shape destroy everything...

This is my code where I add shape :

btRigidBody* Okno::addBolw(float x,float y,float z,float mass)
{
btTransform t;  //position and rotation
t.setIdentity();
t.setOrigin(btVector3(x,y,z));  //put it to x,y,z coordinates
btCylinderShape * cylinder1 = new btCylinderShape(btVector3(1,1.7,1));

btCylinderShape * cylinder2 = new btCylinderShape(btVector3(0.5, 1, 0.5));
btCompoundShape * bolw = new btCompoundShape();
bolw->addChildShape(t,cylinder1);
t.setIdentity();
t.setOrigin(btVector3(x,y+2.7,z));
bolw->addChildShape(t, cylinder2);
btVector3 inertia(0,0,0);
btScalar masses[2] = { mass,mass/2};
bolw->calculatePrincipalAxisTransform(masses,t,inertia);
t.setIdentity();

btMotionState* motion=new btDefaultMotionState(t);  //set the position (and motion)
btRigidBody::btRigidBodyConstructionInfo info(mass*2,motion,bolw,inertia);  //create the constructioninfo, you can create multiple bodies with the same info
btRigidBody* body=new btRigidBody(info);    //let's create the body itself
body->setFriction(1);
body->setRestitution(0);
world->addRigidBody(body);  //and let the world know about it
bodies.push_back(body); //to be easier to clean, I store them a vector
return body;
}

I don't load graphics model on purpose to see exactly physics shape.


Solution

  • I get it, you want to know why there is a third set of axis. It's the centre of the compound shape. Try not using x y z in the transforms until you are creating the default motion state.

    This way the sub objects positions are relative to the compound and the compounds axis will be near the object. In fact because one of the objects will now be at 0,0,0 I would expect you to not see the third axis. But if you do it will at least be a consistent distance to the others.

    btTransform t;  //position and rotation
    t.setIdentity();
    //edit here (1/3):
    t.setOrigin(btVector3(0, 0, 0));
    btCylinderShape * cylinder1 = new btCylinderShape(btVector3(1,1.7,1));
    
    btCylinderShape * cylinder2 = new btCylinderShape(btVector3(0.5, 1, 0.5));
    btCompoundShape * bolw = new btCompoundShape();
    bolw->addChildShape(t,cylinder1);
    t.setIdentity();
    //edit here (2/3):
    t.setOrigin(btVector3(0, 2.7, 0));
    bolw->addChildShape(t, cylinder2);
    btVector3 inertia(0,0,0);
    btScalar masses[2] = { mass,mass/2};
    bolw->calculatePrincipalAxisTransform(masses,t,inertia);
    t.setIdentity();
    //edit here (3/3):
    t.setOrigin(btVector3(x, y, z));  //put it to x,y,z coordinates
    
    btMotionState* motion=new btDefaultMotionState(t);  //set the position (and motion)
    btRigidBody::btRigidBodyConstructionInfo info(mass*2,motion,bolw,inertia);  //create the constructioninfo, you can create multiple bodies with the same info
    btRigidBody* body=new btRigidBody(info);    //let's create the body itself
    body->setFriction(1);
    body->setRestitution(0);
    world->addRigidBody(body);  //and let the world know about it
    bodies.push_back(body); //to be easier to clean, I store them a vector
    return body;