我的一些问题在我的opengl游戏中实现子弹物理。问题是它不想不断更新我的translatef值,但只是在最后。 对子弹的代码如下所示:子弹物理与OpenGL
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中显示正确的值,但翻译只在开始时被调用,我的意思是我只能看到最后一个状态。
EDIT
此被改变功能和循环。收集所有信息,我现在应该可以工作,但它不会。它没有画任何东西。
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();
}
,并开始变declaratins看起来像:
last_update = 0;
timestep = 100;
current_time = 0;
since_update = 0;
这与opengl无关,它与for循环有关。你运行循环300次,但你永远不会告诉它重画子弹? – deanWombourne 2012-01-06 12:28:46
@deanWombourne fallingBall()函数一直在运行,所以它至少应该画出300个相邻的球。但事实并非如此。 – sebap123 2012-01-06 12:59:59
它在同一个线程上“一直在运行”吗?如果你没有在后台线程上进行计算,那么它会等到循环完成之后才有机会运行。 – deanWombourne 2012-01-06 13:01:44