我怎样才能让在我的方向移动相机我面对 [英] How can I make my camera move in the direction I'm facing

查看:124
本文介绍了我怎样才能让在我的方向移动相机我面对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个自由漫游的相机在OpenGL。目前摄像头向前,向后,向左和向右。我的下一个目标是,以允许它在寻找方向移动。

I'm trying to make a free roam camera in OpenGL. Currently the camera moves forward, backwards, left and right. My next aim is to allow it to move in the direction your looking at.

我的继承人code迄今:

Heres my code so far:

float yawRad = Rotation -> y * (3.1415f/180);
float pitchRad = Rotation -> x * (3.1415f/180);

if(myInput.Keys[VK_W]) //Forward
{
    curPos->x += sin(yawRad) * myInput.Sensitivity;
    curPos->z -= cos(yawRad) * myInput.Sensitivity;
}
else if(myInput.Keys[VK_S]) //Backward
{
    curPos->x -= sin(yawRad) * myInput.Sensitivity;
    curPos->z += cos(yawRad) * myInput.Sensitivity;
}

if(myInput.Keys[VK_A]) //Left
{
    curPos->x -= cos(yawRad) * myInput.Sensitivity;
    curPos->z -= sin(yawRad) * myInput.Sensitivity;
}
else if(myInput.Keys[VK_D]) //Right
{
    curPos->x += cos(yawRad) * myInput.Sensitivity;
    curPos->z += sin(yawRad) * myInput.Sensitivity;
}

if(myInput.Keys[VK_E]) //Up
{
    curPos->y += myInput.Sensitivity;
}
else if(myInput.Keys[VK_Q]) //Down
{
    curPos->y -= myInput.Sensitivity;
}

myInput.Sensitivity 是鼠标滚轮控制的浮动,其值增加/减少0.0005。

myInput.Sensitivity is a float controlled by the mouse wheel, with values increasing/decreasing 0.0005.

轮换是一个存储载体类的x,y,z值(浮点)。

Rotation is a vector class storing x,y,z values (float).

我的问题是,我该如何修改我的code实现自由漫游?

My question is, how can I modify my code to achieve free roam?

推荐答案

您可以从模型视图矩阵的第一,第二和第三列获得左侧,摄像头并将其向前载体 - 它的配置,当然之后。

You can obtain the left, up and forward vectors of the camera from the first, second and third column of the modelview matrix - after it's been configured of course.

float mview[16];
float front[4], up[4], left[4];

glGetFloatv(GL_MODELVIEW_MATRIX, mview);
left[0] = mview[0]; left[1] = mview[1]; left[2] = mview[2]; left[3] = 1.0;
up[0] = mview[4]; up[1] = mview[5]; up[2] = mview[6]; up[3] = 1.0;
front[0] = mview[8]; front[1] = mview[9]; front[2] = mview[10]; front[3] = 1.0;

从此,你只需要合适的载体加入到运动的位置。

From then, you just need to add the appropriate vector to the position for movement.

if(myInput.Keys[VK_A]) //Left
{
    curPos->x += left[0] * myInput.Sensitivity;
    curPos->z += left[z] * myInput.Sensitivity;
}
else if(myInput.Keys[VK_D]) //Right
{
    curPos->x -= left[0] * myInput.Sensitivity;
    curPos->z -= left[2] * myInput.Sensitivity;
}
// etc.

请参阅进一步的细节此页面
在同一网站上,这个其他页面介绍了如何使用旋转角度,以获得模型视图矩阵,因此列向量,如上所示。相同的技术(但具有稍微不同的坐标系)也被用来例如在<一href=\"http://gamedev.stackexchange.com/questions/43266/what-does-anglevectors-method-in-quake-3-source-$c$c-does\">quake游戏引擎意甲。

See this page for further details. On the same site, this other page describes how to use rotation angles to obtain the model view matrix, hence the column vectors as shown above. The same technique (but with a slightly different coordinate system) is also used for example in the quake game engines serie.

这篇关于我怎样才能让在我的方向移动相机我面对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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