在 OpenGL 中使对象跟随相机 [英] Make an object follow the camera in OpenGL

查看:26
本文介绍了在 OpenGL 中使对象跟随相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 OpenGL 中制作赛车游戏(只是一个小项目),但在尝试让我的汽车跟随相机视图时遇到问题.

I'm making a racecar game in OpenGL (just a small project) and I'm having problems trying to make my car follow the camera view.

当相机向前和/或向后移动时,让它跟随相机并不难,但是当我旋转相机(向右或向左看)时,汽车仍然静止不动.我的意思是,它仍然向前和向后移动,但它不在相机前面(在侧面).

It wasn't hard to make it follow the camera as the camera moves forward and/or backward, but as I rotate the camera (look right or left) the car remais still. I mean, it still moves forward and backward but it's not in front of the camera (it's on the side).

这是我的代码(我尝试实现它的部分):

Here is my code (the part where I try to implement it):

void updateCam() {
    gluLookAt(posX,posY + 0.025 * std::abs(sin(headPosAux*PI/180)),posZ,
        posX + sin(roty*PI/180),posY + 0.025 * std::abs(sin(headPosAux*PI/180)) + cos(rotx*PI/180),posZ -cos(roty*PI/180),
        0.0,1.0,0.0);

    listenerPos[0] = posX;
    listenerPos[1] = posY;
    listenerPos[2] = posZ;

    source0Pos[0] = posX;
    source0Pos[1] = posY;
    source0Pos[2] = posZ;

    GLfloat distD;

    distD = posZ - 3.3;


    //makes the car "follow" the camera
    modelPOR.Translate(posX,posY,distD);

}

推荐答案

我认为问题在于您在 gluLookAt 中而不是在模型中更改了中心位置POR.Translate

I think the problem is that you change the center location in gluLookAt and not in the modelPOR.Translate

gluLookAt 中间的 3 个参数设置了视图的中心,所以汽车应该在完全相同的位置,但是你修改了中心而不修改汽车的位置.

the 3 middle parameters of gluLookAt set the center of the view, so the car should be at the exact same position but you modify the center without modifying the car's position.

gluLookAt(posX,posY + 0.025 * std::abs(sin(headPosAux*PI/180)),posZ,

    // center x
    posX + sin(roty*PI/180),
    // center y
    posY + 0.025 * std::abs(sin(headPosAux*PI/180)) + cos(rotx*PI/180),
    // center z
    posZ -cos(roty*PI/180),

    0.0,1.0,0.0);

您可能应该用这些相同的值来翻译汽车.然后它会居中.

you should probably translate the car by those same values. then it will be centered.

该代码中似乎存在的另一个问题是您旋转相机而不是汽车,因此汽车可能不会与相机指向同一方向.(这是你想要的还是你在别处做的?)

another problem that seems to be in that code is that you rotate the camera and not the car, so the car probably will not stay pointing in the same direction as the camera. (is that desired or do you do it elswhere?)

但是您确定要让汽车跟随摄像头吗?反过来可能会更好.

but are you really sure you want to make the car follow the camera? it would probably be better the other way around.

这篇关于在 OpenGL 中使对象跟随相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆