OpenGL的子弹物理

Bullet physics with OpenGL

本文关键字:子弹 OpenGL      更新时间:2023-10-16

我在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("stopedn");

}

一开始调用的绘图函数是这样的:

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: %fn",fallY);
glutSolidSphere(1.0f,20,20);
glPopMatrix();

}

问题是它在这个函数的printf中显示了正确的值,但翻译只在开始时调用,我的意思是我只能看到最后一个状态。

编辑

这是更改函数和循环。收集所有的信息,我认为它现在应该工作,但它没有。它什么都没画。

initPhysics(){
    for (int i=0 ; i<500 ; i++) 
    {
        draw();
    }
    state_list.remove( STATE_FALL_BALL );
    printf("stopedn");
}
void CGL::draw(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            current_time = getTime();
            since_update = current_time - last_update;
            printf("time: %fn",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: %fn",fallY);
glFlush();
    }

开始的变量声明如下:

last_update = 0;
timestep = 100;
current_time = 0;
since_update = 0;

这看起来有点杂乱无章。我想,在不深入研究多线程的情况下解决眼前问题的基本错误是这样做:

//pseudo-code to get the idea
void draw(){
    // clear Buffers and setup matricies
    ....
    //calculate the time that has pased since the last time that the physics have been calculated
    time_t current_time = getCurrentTime();
    time_t since_update = current_time - last_update;
    //if enough time has passed since the last redraw calculate physics
    if(since_update>timestep)
    {
        dynamicsWorld->stepSimulation(timestep,10);
        last_update = current_time;
    }
    btTransform trans;
    fallRigidBody->getMotionState()->getWorldTransform(trans);
    fallY = trans.getOrigin().getY();
    //draw various object
    ....
    fallingBall();
    //swap buffers or flush()
    glSwapBuffers();
}

除非有明确的理由直接使用OpenGL,否则我建议您使用更高级别的工具包。还有一个常见的免责声明,即您当前使用的是旧版本的OpenGL。