简体   繁体   English

根据 3D 对象的位置旋转相机

[英]Rotating Camera according to 3D Object's position

I'm using Opengl as the foundations for a small car simulation.我使用 Opengl 作为小型汽车模拟的基础。

I've imported a model car and it drives around fine and the camera follows it.我进口了一辆模型车,它开得很好,相机也跟着它。 Unfortunately I can't seem to get the camera's position to move (or stay behind the car) when the car turns.不幸的是,当汽车转弯时,我似乎无法让相机的位置移动(或留在汽车后面)。 It just rotates on the spot.它只是在原地旋转。

eg if the car turns left, the camera rotates to follow it but doesn't move to directly behind the car.例如,如果汽车向左转,相机会旋转以跟随它,但不会移动到汽车的正后方。

I've looked at these questions here and here but still can't seem to get it working.我已经在这里这里查看了这些问题,但似乎仍然无法使其正常工作。

This is how I calculate the position of the car (it just drives on the XZ plane for now):这就是我计算汽车位置的方式(它现在只是在 XZ 平面上行驶):

    velocity.x = sin(rotX_rad);
    velocity.z = cos(rotX_rad);

    globalPos.x + velocity.x*delta;
    globalPos.z + velocity.z*delta;

    glTranslatef(globalPos.x,globalPos.y,globalPos.z);
    glRotatef(rotX, 0.0, 1.0, 0.0);

And this is currently how I have my lookAt function:这就是我目前拥有 lookAt 功能的方式:

    gluLookAt(camX+globalPos.x,
              camY+globalPos.y,
              camZ+globalPos.z,
              lookX+globalPos.x+sin(rotX_rad),
              lookY+globalPos.y+cos(rotX_rad),
              lookZ+globalPos.z-cos(rotX_rad),
              camNX, camNY, camNZ);

I would like the camera to be like this: (where Y is Z)我希望相机是这样的:(Y 是 Z)

在此处输入图片说明

The way I see it, it should be:在我看来,它应该是:

    camX + globalPos.x+ sin(rotX_rad)*(distFromCar),
    camY,
    camZ + globalPos.z- cos(rotX_rad)*(distFromCar),

but it behaves strangely....但它的行为很奇怪......

What am I not doing?我不做什么?

Give this a shot, it makes sense in my head but it might not in reality :)试一试,这在我的脑海中是有道理的,但在现实中可能并非如此:)

Ok, so we can represent your car's position and velocity as 2-element vectors (in the case of position it is a point, and in the case of velocity it is a true vector.)好的,所以我们可以将您的汽车的位置和速度表示为 2 元素向量(在位置的情况下它是一个点,在速度的情况下它是一个真实的向量。)

Now that we have that, to calculate the position of the camera we can just take the negative of your velocity vector (this means just make all elements of the vector negative, yielding a vector of the same magnitude but exactly opposite direction) and add that vector to your car's position.现在我们有了这个,为了计算相机的位置,我们可以只取速度向量的负值(这意味着只需让向量的所有元素都为负,产生一个大小相同但方向完全相反的向量)并添加矢量到您的汽车的位置。

For example, say your car is at position (1,1) and your speed vector is (1,2).例如,假设您的汽车在位置 (1,1) 并且您的速度向量是 (1,2)。 The negative speed vector would be (-1,-2) and the position of your camera would be (1,1) + (-1,-2) to be (0,-1).负速度向量将是 (-1,-2) 并且您的相机的位置将是 (1,1) + (-1,-2) 是 (0,-1)。 You will probably want to normalize the negative speed vector so that your camera stays a constant distance from the car, otherwise the faster you go the further the camera will get :)您可能希望对负速度向量进行归一化,以便您的相机与汽车保持恒定距离,否则您走得越快,相机就越远:)

Now that you have the camera's position, just call gluLookAt:现在您有了相机的位置,只需调用 gluLookAt:

gluLookAt(camPos.x,camPos.y,camPos.z,carPos.x,carPos.y,carPos.z,0,1,0);

As you can see, we are passing in the camera's position (which we just calculated in the previous step) and telling the camera to look at the car.如您所见,我们传入了相机的位置(我们刚刚在上一步中计算过)并告诉相机看汽车。 You may want to tweak some values (for example, have the camera be floating a few units off the ground, instead of directly behind the car, or maybe have the camera look at a point a few units above the car, etc.)您可能想要调整一些值(例如,让相机漂浮在离地面几个单位的地方,而不是直接在汽车后面,或者让相机看着汽车上方几个单位的点,等等)

Let me know how this goes!让我知道这是怎么回事! Again, this is just something I thought might work for you, no guarantees that my math is right though :) But this should be faster and easier to understand than messing around with angles and such.同样,这只是我认为可能对你有用的东西,虽然不能保证我的数学是正确的 :) 但这应该比乱搞角度等更快更容易理解。 One thing to keep in mind: this will not work if the car is completely stationary (no velocity vector)要记住的一件事:如果汽车完全静止(没有速度矢量),这将不起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM