我的小遊戲引擎基本上有一個3D數組Cubes [x] [y] [z](其實它是一個很大的一維數組,但我做了一些重載)。我知道玩家站在X Y Z的哪個立方體。玩家將能夠拍攝立方體來摧毀它,這就是爲什麼我需要弄清楚如何找到鼠標所在的立方體。我發現了一些關於採摘的OpenGL文檔,但是這種方法很慢。由於我的立方體是有組織的,並且我知道玩家在哪個立方體上,並且相機在X和Y上的角度(相機不在Z上旋轉),並且鼠標總是處於screenwidth/2,screenheight/2我確定theres比GL採摘技術更快的方式。鼠標下的對象?
這裏是相機的設置方式:
void CCubeGame::SetCameraMatrix()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(Camera.rotx,1,0,0);
glRotatef(Camera.roty,0,1,0);
glRotatef(Camera.rotz,0,0,1);
glTranslatef(-Camera.x , -Camera.y,-Camera.z);
}
void CCubeGame::MouseMove(int x, int y)
{
if(!isTrapped)
return;
int diffx = x-lastMouse.x;
int diffy = y-lastMouse.y;
lastMouse.x = x;
lastMouse.y = y;
Camera.rotx += (float) diffy * 0.2;
Camera.roty += (float) diffx * 0.2;
if(Camera.rotx > 90)
{
Camera.rotx = 90;
}
if(Camera.rotx < -90)
{
Camera.rotx = -90;
}
if(isTrapped)
if (fabs(ScreenDimensions.x/2 - x) > 1 || fabs(ScreenDimensions.y/2 - y) > 1) {
resetPointer();
}
}
Vertex3f CCubeGame::MoveCamera(int direction, float amount)
{
float xrotrad, yrotrad;
Vertex3f result(0,0,0);
switch(direction)
{
case CAM_FORWARD:
yrotrad = (Camera.roty/180 * 3.141592654f);
xrotrad = (Camera.rotx/180 * 3.141592654f);
result.x = float(sin(yrotrad)) * amount;
result.z = -(float(cos(yrotrad)) * amount);
result.y = 0;
//Camera.y -= float(sin(xrotrad)) * amount;
break;
case CAM_BACKWARD:
yrotrad = (Camera.roty/180 * 3.141592654f);
xrotrad = (Camera.rotx/180 * 3.141592654f);
result.x = -(float(sin(yrotrad)) * amount);
result.z = float(cos(yrotrad)) * amount;
result.y = 0;
//Camera.y += float(sin(xrotrad)) * amount;
break;
case CAM_RIGHT:
yrotrad = (Camera.roty/180 * 3.141592654f);
result.x = float(cos(yrotrad)) * amount;
result.z += float(sin(yrotrad)) * amount;
result.y = 0;
break;
case CAM_LEFT:
yrotrad = (Camera.roty/180 * 3.141592654f);
result.x = -(float(cos(yrotrad)) * amount);
result.z = -(float(sin(yrotrad)) * amount);
result.y = 0;
break;
default:
break;
}
Camera.x += result.x;
Camera.y += result.y;
Camera.z += result.z;
return result;
}
感謝
實際上,關於Z高度,玩家可以上下看,槍在玩家臉部的中心位置,屏幕的中心也是,我的立方體已經Z排序了嗎?如立方體(5,5,5)旁邊的立方體(5,5,6)。 – jmasterx 2010-09-08 17:37:12