Zuerst , es wurde von der Schwerkraft(0, -10., 0) beeinflusst und konnte von vec(0, 10, 0) auf den Boden fallen, aber als ich die Reset-Taste R drückte, war es ist zum oberen vec(0, 10, 0) Position.
Logischerweise sollte es wie zuvor fallen, aber seltsamerweise blieb es an diesem Punkt stehen. Ich bin sicher, dass die Schwerkraft nicht ausgeschaltet ist. Ich weiß nicht, was schief gelaufen ist und dazu geführt hat, dass es jetzt keine Kraft mehr hat.
Hier sind einige relevante Codes:
< ol>
[*]Zuerst werden der Boden und der Ball erstellt
Code: Select all
dynamicsWorld->setGravity(btVector3(0, -10.f, 0));
{
// 创建地面
btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.), btScalar(0.1), btScalar(50.))); // 大地面
collisionShapes.push_back(groundShape);
btTransform groundTransform;
groundTransform.setIdentity();
groundTransform.setOrigin(btVector3(0.0f, -0.1f, 0.0f));
btScalar mass(0.f);
btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
btRigidBody::btRigidBodyConstructionInfo groundRbInfo(mass, myMotionState, groundShape);
btRigidBody* ground = new btRigidBody(groundRbInfo);
ground->setRestitution(0.9f); // 设置弹性
dynamicsWorld->addRigidBody(ground);
}
{
// 创建动态刚体(小球)
btCollisionShape* colShape = new btSphereShape(btScalar(0.2)); // 半径为 0.2
collisionShapes.push_back(colShape);
btTransform startTransform;
startTransform.setIdentity();
btScalar mass(0.001f);
bool isDynamic = (mass != 0.f);
btVector3 localInertia(0, 0, 0); // 惯性
if (isDynamic)
colShape->calculateLocalInertia(mass, localInertia);
startTransform.setOrigin(btVector3(huoyinMesh->getPosition().x, huoyinMesh->getPosition().y, huoyinMesh->getPosition().z)); // 设置小球的初始位置
btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, myMotionState, colShape, localInertia);
btRigidBody* body = new btRigidBody(rbInfo);
body->setDamping(0.1, 0.); // 设置较小的阻尼
body->setRestitution(0.9f); // 设置弹性
body->setLinearVelocity(btVector3(0, 15, 0)); // 设置初始下落速度
dynamicsWorld->addRigidBody(body);
}
- Simulieren und aktualisieren Sie dann die Position des Balls in der OpenGL-Schleife. Drücken Sie hier die R-Taste, um den Ball wieder in seine ursprüngliche Position zu bringen.
Code: Select all
dynamicsWorld->stepSimulation(1.f / 60.f, 10);
cameraControl->update();
renderer->setClearColor(clear_color);
// 按下 R 键,重置物体位置
if (resetPosition) {
btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[1];
btRigidBody* body = btRigidBody::upcast(obj);
btTransform resetTrans;
if (body && body->getMotionState()) {
btVector3 resetPos(0.0f, 10.0f, 0.0f); // 重置到某个新位置(例如 0, 10, 0)
resetTrans.setOrigin(resetPos);
body->getMotionState()->setWorldTransform(resetTrans);
body->setLinearVelocity(btVector3(0, -1, 0)); // 也可以将速度清零
body->setAngularVelocity(btVector3(0, 0, 0)); // 清除旋转速度
}
else {
// 对于没有刚体的物体,直接修改世界变换
resetTrans.setOrigin(btVector3(0.0f, 10.0f, 0.0f)); // 设置新位置
obj->setWorldTransform(resetTrans);
}
// 更新场景中的位置
scene->getChildren()[1]->setPosition(glm::vec3(resetTrans.getOrigin().getX(), resetTrans.getOrigin().getY(), resetTrans.getOrigin().getZ()));
// 一次重置后,重置标志位为 false
resetPosition = false;
}
// 每帧打印物体位置
btTransform trans;
for (int j = dynamicsWorld->getNumCollisionObjects() - 1; j >= 0; j--) {
btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[j];
btRigidBody* body = btRigidBody::upcast(obj);
if (body && body->getMotionState()) {
body->getMotionState()->getWorldTransform(trans);
}
else {
trans = obj->getWorldTransform();
}
scene->getChildren()[j]->setPosition(glm::vec3(trans.getOrigin().getX(), trans.getOrigin().getY(), trans.getOrigin().getZ()));
//printf("world pos object %d = %f,%f,%f\n", j, float(trans.getOrigin().getX()), float(trans.getOrigin().getY()), float(trans.getOrigin().getZ()));
}
renderer->render(scene, camera, dirLight, ambientLight);
renderIMGUI();
Es gibt nur sehr wenige Tutorials zu Bullet im Internet, daher weiß ich nicht, wie ich dieses Problem lösen kann. Wenn möglich, helfen Sie mir bitte