-1
我有一個三維地形環境中,像這樣:如何將相機對準我角色面前的細胞z-索引?
我試圖爬山時,爲了獲得字符(攝像機)查找,並看不起下降時,就像在真正的攀登生活。
這是它目前在做什麼:
眼下相機上下移動的山丘就好了,但我不能拿相機角度才能正常工作。我可以根據地形思考瞄準的唯一方法是獲取角色目前所面對的單元格的z-index,並將其作爲焦點,但我真的不知道該怎麼做。
這是無可爭議的一項任務,我們故意不使用對象,所以事情的組織有點奇怪。
這裏是如何我目前做的事情:
const int M = 100; // width
const int N = 100; // height
double zHeights[M+1][N+1]; // 2D array containing the z-indexes of terrain cells
double gRX = 1.5; // x position of character
double gRY = 2.5; // y position of character
double gDirection = 45; // direction of character
double gRSpeed = 0.05; // move speed of character
double getZ(double x, double y) // returns the height of the current cell
{
double z = .5*sin(x*.25) + .4*sin(y*.15-.43);
z += sin(x*.45-.7) * cos(y*.315-.31)+.5;
z += sin(x*.15-.97) * sin(y*.35-8.31);
double amplitute = 5;
z *= amplitute;
return z;
}
void generateTerrain()
{
glBegin(GL_QUADS);
for (int i = 0; i <= M; i++)
{
for (int j = 0; j <= N; j++)
{
zHeights[i][j] = getZ(i,j);
}
}
}
void drawTerrain()
{
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
glColor3ub((i*34525+j*5245)%256, (i*3456345+j*6757)%256, (i*98776+j*6554544)%256);
glVertex3d(i, j, getZ(i,j));
glVertex3d(i, j+1, getZ(i,j+1));
glVertex3d(i+1, j+1, getZ(i+1,j+1));
glVertex3d(i+1, j, getZ(i+1,j));
}
}
}
void display() // callback to glutDisplayFunc
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
double radians = gDirection /180.*3.141592654; // converts direction to radians
double z = getZ((int)gRX, (int)gRY); // casts as int to find z-index in zHeights[][]
double dx = cos(radians)*gRSpeed;
double dy = sin(radians)*gRSpeed;
double at_x = gRX + dx;
double at_y = gRY + dy;
double at_z = z; // source of problem, no idea what to do
gluLookAt(gRX, gRY, z + 2, // eye position
at_x, at_y, at_z + 2, // point to look at, also wrong
0, 0, 1); // up vector
drawTerrain();
glEnd();
}
void init()
{
generateTerrain();
}
您向向量的積和向上向量給你一個側面載體。您的側向矢量與當前單元的法線矢量的交叉積將爲您提供想要「跟隨斜率」的方向。您可以通過標準化單元的斜率向量的叉積來計算法向量。由於您的高度函數是可導的,您可以使用該導數在任意點查找您的斜率向量。儘管如此,從曲線中順暢地看起來並不是一個問題。在這種情況下,你可以簡單地比較附近的高度來找到你的斜坡。 –
附錄:無論如何,如果你想做任何陰影,你都會想要計算法向量。 –