2012-01-06 13 views
0

私は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; 
+1

これはopenglとは関係がありません。forループと関係があります。ループを300回実行しますが、弾丸を再描画するように指示することはありませんか? – deanWombourne

+0

@deanWombourne fallingBall()関数は常に実行されているため、少なくとも300個のボールを隣り合わせに描画する必要があります。しかし、それはしません。 – sebap123

+0

これは、同じスレッドで「常に実行中」ですか?バックグラウンドスレッドで計算を行っていない場合、ループが終了するまで待ってから実行するチャンスが与えられます。 – deanWombourne

答えて

1

。私はあなたがより高いレベルのツールキットを使用することをお勧めしたい、直接OpenGLを使用するための明確な理由がない限り

//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の古いバージョンを使用しているという一般的な免責事項もあります。

+0

プロジェクトはOpenGLで開発されていますので、私はそれを使用しなければなりません。それはうまく見えますが、私は1つの質問があります - このdrwa関数は、このforループまたはどこで使用されていますか? – sebap123

+0

はい、物理を計算するために別のスレッドを使用しない限り、物理を進めて十分な時間が経過したら前進させる必要がある場合は、すべてのフレーム(ループを描きます)をチェックする必要があります。 – PeterT

+0

@ sebap123私はドローイングが何であるか、そしてドローイングを使う方法について何か根本的な誤解があると思いますか? 'glClear(...)'と 'glFlush()'や 'glSwapBuffers()'はどこで使っていますか? – PeterT

関連する問題