#java #lwjgl
#java #lwjgl
Вопрос:
Я пытаюсь создать простую систему столкновения с рельефом для своей игры, но она просто возвращает небольшие значения в диапазоне от 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;
}
}
Комментарии:
1. ericlippert.com/2014/03/05/how-to-debug-small-programs
2. Страшный вомбат я просто вставил небольшую часть своего кода, так что это не поможет