私は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
@deanWombourne fallingBall()関数は常に実行されているため、少なくとも300個のボールを隣り合わせに描画する必要があります。しかし、それはしません。 – sebap123
これは、同じスレッドで「常に実行中」ですか?バックグラウンドスレッドで計算を行っていない場合、ループが終了するまで待ってから実行するチャンスが与えられます。 – deanWombourne