我在我的 opengl 游戏中实现子弹物理时遇到了一些问题。问题是它不想持续更新我的 translatef 值,而只是在最后。项目符号的代码如下所示:
void CGL::initPhysics( void ) {
broadphase = new btDbvtBroadphase();
collisionConfiguration = new btDefaultCollisionConfiguration();
dispatcher = new btCollisionDispatcher(collisionConfiguration);
solver = new btSequentialImpulseConstraintSolver;
dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,collisionConfiguration);
dynamicsWorld->setGravity(btVector3(0,-10,0));
ballShape = new btSphereShape(1);
pinShape = new btCylinderShape(btVector3(1,1,1));
pinShape->setMargin(0.04);
fallMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,10,0)));
btScalar mass = 1;
btVector3 fallInertia(0,0,0);
ballShape->calculateLocalInertia(mass,fallInertia);
btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),1);
btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,-1,0)));
btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0,groundMotionState,groundShape,btVector3(0,0,0));
btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI);
dynamicsWorld->addRigidBody(groundRigidBody);
btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass,fallMotionState,ballShape,fallInertia);
btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI);
dynamicsWorld->addRigidBody(fallRigidBody);
for (int i=0 ; i<300 ; i++) {
dynamicsWorld->stepSimulation(1/60.f,10);
btTransform trans;
fallRigidBody->getMotionState()->getWorldTransform(trans);
fallY = trans.getOrigin().getY();
}
state_list.remove( STATE_FALL_BALL );
printf("stoped\n");
}
开头调用的绘图函数如下所示:
void CGL::fallingBall( void ) {
glPushMatrix();
float colBall2[4] = { 0.0f, 0.0f, 1.0f, 1.0f };
glMaterialfv( GL_FRONT, GL_AMBIENT, colBall2);
glTranslatef(0.0f,fallY,0.0f);
printf("fallY: %f\n",fallY);
glutSolidSphere(1.0f,20,20);
glPopMatrix();
}
问题是它在这个函数的 printf 中显示了正确的值,但是翻译只在开始时被调用,我的意思是我只能看到最后一个状态。
编辑
这是改变的功能和循环。收集我认为它现在应该工作的所有信息,但它没有。它不画任何东西。
initPhysics(){
for (int i=0 ; i<500 ; i++)
{
draw();
}
state_list.remove( STATE_FALL_BALL );
printf("stoped\n");
}
void CGL::draw(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
current_time = getTime();
since_update = current_time - last_update;
printf("time: %f\n",since_update);
if(since_update>timestep)
{
dynamicsWorld->stepSimulation(timestep,10);
last_update = current_time;
}
btTransform trans;
fallRigidBody->getMotionState()->getWorldTransform(trans);
fallY = trans.getOrigin().getY();
fallingBall();
printf("fallY: %f\n",fallY);
glFlush();
}
开始变量声明看起来像这样:
last_update = 0;
timestep = 100;
current_time = 0;
since_update = 0;