btCompoundShape 添加了额外的形状

btCompoundShape added extra shape

您好,我对 btCompoundShape 有疑问, 我从 2 个圆柱体创建形状,但是当我 运行 我的应用程序时,它似乎是复合形状中的 3 个项目。 任何人都知道为什么? 我添加图片和代码:

这个波纹状的小东西摧毁一切...

这是我添加形状的代码:

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;
}

我不是故意加载图形模型来查看物理形状的。

我明白了,你想知道为什么会有第三组轴。它是复合形状的中心。在创建默认运动状态之前,请尽量不要在变换中使用 x y z。

这样子对象的位置是相对于复合的,复合轴将靠近对象。事实上,因为其中一个对象现在位于 0,0,0,所以我希望您看不到第三个轴。但如果你这样做,至少与其他人保持一致的距离。

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;