2012-01-06 6 views
0

내 OpenGL 게임에 총알 물리를 구현하는 데 문제가 있습니다. 것은 내 translatef 값을 지속적으로 업데이트하지 않고 끝에서 만 업데이트하려는 것입니다.OpenGL을 사용한 Bullet 물리

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로 개발되었으므로 사용해야합니다. 괜찮아 보이는데, 한가지 질문이 있습니다 -이 drwa 함수는이 for 루프 또는 어디에 구현 되었습니까? – sebap123

+0

그렇습니다. 물리학을 계산하기 위해 별도의 스레드를 사용하지 않는 한, 물리학을 발전시켜야하고 충분한 시간이 경과하면 전진해야하는 모든 프레임 (루프를 그리는 안쪽)을 확인해야합니다. – PeterT

+0

@ sebap123 당신은 draw-loops가 무엇이고 어떻게 사용하는지에 대한 근본적인 오해가 있다고 생각합니다. 'glClear (...)'와'glFlush()'또는'glSwapBuffers()'는 어디에서 사용 했습니까? 물리학을 통합하기 전에 어디에서 호출 했습니까? – PeterT