做一个对象按照相机在OpenGL [英] Make an object follow the camera in OpenGL

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

问题描述

我做在OpenGL赛车游戏(只是一个小项目),我有问题,试图让我的车跟着摄影机视图。

这是不难,使其遵循相机作为相机前进和/或后退,但我旋转摄像头(看右侧或左侧)汽车remais依然。我的意思是,它仍然向前和向后的,但它不是在照相机(它的侧面)。

前面

下面是我的code(这里我尽量实现它的部分):

无效updateCam(){     gluLookAt(POSX,波西+ 0.025 *的std :: ABS(SIN(headPosAux * PI / 180)),posZ,         POSX +罪(ROTY * PI / 180),波西+ 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;     //使得汽车遵循相机     modelPOR.Translate(POSX,波西,distD); }

解决方案

我认为这个问题是您在gluLookAt,而不是在modelPOR.Translate更改中心位置

gluLookAt的3中间的参数设置视图的中心,因此该车应该是在准确的同一位置,但修改的中心,而无需修改汽车的位置。

  gluLookAt(POSX,波西+ 0.025 *的std :: ABS(SIN(headPosAux * PI / 180)),posZ,

    //中心X
    POSX +罪(ROTY * PI / 180),
    //中心ÿ
    波西+ 0.025 *的std :: ABS(SIN(headPosAux * PI / 180))+ COS(ROTX * PI / 180),
    //中央z
    posZ -cos(ROTY * PI / 180),

    0.0,1.0,0.0);
 

您也许应该被同一价值观转化的车。那么它将被居中。

另一个问题,这似乎是在code是您旋转相机而不是汽车,所以汽车可能不会留指向同一方向的照相机。 (是,希望还是你elswhere办呢?)

但你真的确定你想车子跟着相机?它很可能是更好的其他方式。

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);

}

解决方案

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

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天全站免登陆