btCompoundShape增加了额外的形状

时间:2015-01-04 00:07:19

标签: c++ game-physics bullet bulletphysics rigid-bodies

嗨我有btCompoundShape的问题, 我从2个圆柱体创建形状,但是当我运行我的应用程序时,它似乎是计算形状中的3个项目。 任何人都知道为什么? 我添加图片和代码: enter image description here

这个小小的东西形成了破坏一切......

这是我添加形状的代码:

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

我没有故意加载图形模型以确切地看到物理形状。

1 个答案:

答案 0 :(得分:2)

我明白了,你想知道为什么有第三组轴。它是复合形状的中心。在创建默认运动状态之前,请尝试不要在变换中使用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;