내 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;
OpenGL과 아무 관련이 없습니다. for 루프와 관련이 있습니다. 300 번 루프를 돌리지 만 총알을 다시 그리라고 말하지 않습니까? – deanWombourne
@deanWombourne fallingBall() 함수는 항상 실행 중이므로 적어도 옆에 300 개의 공을 그려야합니다. 그러나 그렇지 않습니다. – sebap123
같은 스레드에서 항상 실행되고 있습니까? 백그라운드 스레드에서 계산을 수행하지 않으면 루프가 끝날 때까지 기다렸다가 실행 기회가 주어집니다. – deanWombourne