OpenGL的子弹物理
Bullet physics with OpenGL
我在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。
相关文章:
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 了解 GLM- openGL 中的相机转换
- 如何在全屏模式下(在OpenGL中)使背景透明
- 为什么我不能使用 EGL 创建无头 OpenGl 上下文?
- OpenGL大的3D纹理(>2GB)非常慢
- OpenGl glm rotate
- C++atioglxx.pdb未加载错误glBufferData OpenGL
- OpenGL在启用深度测试时不会丢弃我的碎片
- OpenGL相机和相机空间转型的困惑
- OpenGL将纹理四边形渲染为(0,0)
- OpenGL 和 GLM 矩阵无法正确扩展,总是按比例缩小
- 发布旋转矩阵(openGL/glm)
- 如何在Visual Basic中使用矩形函数OpenGL绘制矩形
- 无法使用VAO和EBO(openGL)绘制多个对象
- 为什么我的点没有在 OpenGL 中绘制鼠标所在的位置?
- OpenGL 16 位模板缓冲区?
- 将QOpenGLWidget子类转换为使用Metal而不是OpenGL的子类是否可行?
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 在 openGL 中多次绑定缓冲区
- OpenGL的子弹物理