【发布时间】:2013-12-24 13:51:31
【问题描述】:
我正在使用 C++ 在 OpenGL 中制作游戏。我的地形有山丘等,我希望角色能够在山上上下行走。为此,我创建了一个函数,试图找到最近的坐标并返回相应的 y 坐标,但它不起作用,我的角色只是保持在相同的高度。这是我的功能:
float ViewPort::comparePosition(float xPos, float zPos) {
int closestValSoFar = 0;
for (int unit = 0; unit < sizeof(desert_scene_plainVerts)/sizeof(desert_scene_plainVerts[0]); unit++){
int xDifference = terrainxPos[unit] - xPos;
int zDifference = terrainzPos[unit] - zPos;
int combinedDifferece = xDifference + zDifference;
if (unit == 0) {
closestValSoFar = unit;
}
if (combinedDifferece < (terrainxPos[unit-1] - xPos) + (terrainzPos[unit-1] - zPos)) {
closestValSoFar = unit - 1;
}
else {
closestValSoFar = unit;
}
if ((unit - 1) < sizeof(desert_scene_plainVerts)/sizeof(desert_scene_plainVerts[0])) {
return terrainyPos[closestValSoFar];
}
}
return terrainyPos[closestValSoFar];
}
我正在调用它并将其与此代码一起使用:
float yPos = ViewPort::comparePosition(Camera::position.x, Camera::position.z);
Camera::position.y = yPos+1.65;
有人知道如何修复我的代码吗?
【问题讨论】:
-
你有一个非常奇怪的使用
sizeof的方式...如果数组的大小是堆栈然后输入它而不是sizeof...如果大小不是静态的然后@ 987654325@ 不起作用(给出正确的“长度”)。 -
但我认为长度是sizeof(verts)/sizeof(verts[0])
-
如果您的
verts数组的大小是静态的,那么应该返回该大小,但如前所述,如果您的数组不是静态的,那么它会给您带来问题。 -
您能否详细说明“最近坐标”的含义?什么坐标?最接近什么?对你的角色?
-
距离角色最近的地形坐标