Thank you for the criticism. Here is the code. It is explained below, sorry about that.
public float getHeightOfTerrain(float worldX, float worldZ) {
float terrainX = worldX - this.x;
float terrainZ = worldZ - this.z;
float gridSquareSize = SIZE / ((float) heights.length - 1);
int gridX = (int) Math.floor(terrainX / gridSquareSize);
int gridZ = (int) Math.floor(terrainZ/ gridSquareSize;)
if(gridX >= heights.length - 1 || gridZ >= heights.length - 1 || gridZ < 0 || gridZ < 0) {
return 0;
}
float xCoord = (terrainX % gridSquareSize) / gridSquareSize;
float zCoord = (terrainZ % gridSquareSize) / gridSquareSize;
float answer;
if (xCoord <= (1-zCoord)) {
answer = Maths.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 = Maths.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));
}
public static float barryCentric(Vector3f p1, Vector3f p2, Vector3f p3, Vector2f pos) {
float det = (p2.z - p3.z) * (p1.x - p3.x) + (p3.x - p2.x) * (p1.z - p3.z);
float l1 = ((p2.z - p3.z) * (pos.x - p3.x) + (p3.x - p2.x) * (pos.y - p3.z)) / det;
float l2 = ((p3.z - p1.z) * (pos.x - p3.x) + (p1.x - p3.x) * (pos.y - p3.z)) / det;
float l3 = 1.0f - l1 - l2;
return l1 * p1.y + l2 * p2.y + l3 * p3.y;
}
The first two lines of the first code function are explained at the following link (It's not needed in this example and is for multiple terrains; terrainx and terrainy will have the value of zero). https://www.gamedev.net/forums/topic/701121-very-easy-beginners-question-opengl/
I am using the top function to get any height by passing in world coordinates. The height array holds the heights by there x and z indexes when the terrain was made.
This is what the top function doing
The actual cell is determined by gridx and gridz.
The actual coordinates are than found.
Than the included cellexplanation.gif diagrams formula Is used to determine which triangle cell in the regular square cell.
The height is than determined using the Barry Centric formula.
The uploaded image : gridcomplete.gif illustrates my problem. If there is an incline or decline than there are changing height values when moving from cell A to cell B. If I am in cell A and I do not pass the unknown smallest amount of x and z and I move into cell B than I have missed some changes of height that I would like to use to change the velocity of the moving object.
I am looking to determine the minimum value so that I can keep using the entirety of the heights to determine the object's speed, if this is possible. I don't know the minimal unit of measuring, or how it would be determined. I read everything I could find about world coordinates and pixels.
Or, maybe interpolation is needed and if so I would appreciate some super pseudocode and/or help. The cell structure is below in : cellexplanation.gif;
Otherwise, I'd like to here about any other methods that are not to difficult for a new Graphics Programmer.
Hopefully this is more understandable,
Josheir
Cite: tutorial 22 :