我正在尝试为我的游戏制作一个简单的地形碰撞系统,但是 它只是返回范围从0.3-1的较小值,没有更高或更低,而且我的角色停留在相同的高度。
我已经尝试修复了几个小时,但是我还没有找到解决方案。
以下是碰撞代码:
package terrain;
public float getHeightOfTerrain(float worldx,float worldz)
{
float terrainX = worldx - this.x;
float terrainZ = worldz - this.z;
float gridSquareSize = size - (float)(heights.length -1f);
int gridx = (int)Math.floor(terrainX/gridSquareSize);
int gridz = (int)Math.floor(terrainZ/gridSquareSize);
if(gridx >= heights.length - 1 || gridz >= heights.length -1 || gridx<0||gridz<0)
{
return 0;
}
float xCoord = (terrainX % gridSquareSize)/gridSquareSize;
float zCoord = (terrainZ % gridSquareSize)/gridSquareSize;
float answer;
if (xCoord <= (1-zCoord))
{
answer = Mathc
.barryCentric(new Vector3f(0, heights[gridx][gridz], 0), new Vector3f(1,
heights[gridx + 1][gridz], 0), new Vector3f(0,
heights[gridx][gridz + 1], 1), new Vector2f(xCoord, zCoord));
} else
{
answer = Mathc
.barryCentric(new Vector3f(1, heights[gridx + 1][gridz], 0), new Vector3f(1,
heights[gridx + 1][gridz + 1], 1), new Vector3f(0,
heights[gridx][gridz + 1], 1), new Vector2f(xCoord, zCoord));
}
return answer;
}
private RawModel generateTerrain(Loader loader,String heightMap)
{
BufferedImage image = null;
try
{
image = ImageIO.read(new File("res/"+heightMap+".png"));
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
int vertex_count = image.getHeight();
heights = new float[vertex_count][vertex_count];
int count = vertex_count * vertex_count;
float[] vertices = new float[count * 3];
float[] normals = new float[count * 3];
float[] textureCoords = new float[count*2];
int[] indices = new int[6*(vertex_count-1)*(vertex_count-1)];
int vertexPointer = 0;
for(int i=0;i<vertex_count;i++){
for(int j=0;j<vertex_count;j++){
vertices[vertexPointer*3] = (float)j/((float)vertex_count - 1) * size;
float height = getheight(j,i,image);
heights[j][i] = height;
vertices[vertexPointer*3+1] = height;
vertices[vertexPointer*3+2] = (float)i/((float)vertex_count - 1) * size;
Vector3f normal = calculateNormal(j,i,image);
normals[vertexPointer*3] = normal.x;
normals[vertexPointer*3+1] = normal.y;
normals[vertexPointer*3+2] = normal.z;
textureCoords[vertexPointer*2] = (float)j/((float)vertex_count - 1);
textureCoords[vertexPointer*2+1] = (float)i/((float)vertex_count - 1);
vertexPointer++;
}
}
int pointer = 0;
for(int gz=0;gz<vertex_count-1;gz++){
for(int gx=0;gx<vertex_count-1;gx++){
int topLeft = (gz*vertex_count)+gx;
int topRight = topLeft + 1;
int bottomLeft = ((gz+1)*vertex_count)+gx;
int bottomRight = bottomLeft + 1;
indices[pointer++] = topLeft;
indices[pointer++] = bottomLeft;
indices[pointer++] = topRight;
indices[pointer++] = topRight;
indices[pointer++] = bottomLeft;
indices[pointer++] = bottomRight;
}
}
return loader.loadToVAO(vertices, textureCoords, normals, indices);
}
private float getheight(int x,int z,BufferedImage image)
{
if(x<=0 || x>= image.getHeight()||z<=0||z>=image.getHeight())
{
return 0;
}
float height = image.getRGB(x,z);
height += maxPixelColor/2f;
height /= maxPixelColor/2f;
height *= maxHeight;
return height;
}
}