Help with Code, Please

I have been working on a heightmap. I can’t get the code to work so that the cube stays on the terrain. When the height is set to a constant number t he cube seems to stay on the terrain. I don’t know who to ask, I’m pulling my hair out!

Here is the code:

////////////////////Move in Z direction 
    	//height map is 80 * 80 vertices, 180 size
    	//move in negative z direction
        if (window.isKeyPressed(GLFW_KEY_W)) {
        	
        	 //change in z
        	 gZ += .05f;
        	
        	 // size of height map * scale of display is size of terrain to draw :  180 * .04  = 7.2
           	 // terrain size is 7.2 so 3.6 is in center and height map terrain is 25 times bigger 
        	 // because scale of display is .04
        	 float zTerrain = (3.6f + gZ) * 25f;
        	 
        	 //height map cell is size of height map divided by (vectors - 1)   :   2.278
        	 //first parameter is x, centered so, half of 79 cells times 2.278
        	 float answer = terrain.getTheHeight(79f * .5f * 2.278f, zTerrain 	,0f,0f,theHeight);
          	 gameItems[0].setScale(.125f);
        	 //scale of displayed terrain Y is .04 times height map's Y and image is centered on 
          	 //displayed terrain
        	 gameItems[0].setPosition(0.f,  .04f * answer  + 0.0625f,  -3.6f-gZ );
             camera.setPosition(0, 1,-gZ);
        	
        ////////////////////

//////////Make terrain
        VERTEX_COUNT = 80;
        SIZE = 180;
        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[] indices2 = new int[6*(VERTEX_COUNT-1)*(VERTEX_COUNT-1)];
		int vertexPointer = 0;
		
		
		for(int i=-40;i<(VERTEX_COUNT/2);i++){
			for(int j=-40;j<(VERTEX_COUNT/2);j++){
				vertices[vertexPointer*3] = (float)j/((float)VERTEX_COUNT - 1) * SIZE;
				
				
				tempHeight = terrain.getHeight(j+40, i+40, image);
				vertices[vertexPointer*3+1] =  tempHeight;
				theHeight[j + 40][i + 40] = tempHeight;
				vertices[vertexPointer*3+2] = (float)i/((float)VERTEX_COUNT - 1) * SIZE;
				//normals[vertexPointer*3] = 0;
				//normals[vertexPointer*3+1] = 1;
				//normals[vertexPointer*3+2] = 0;
				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;
				indices2[pointer++] = topLeft;
				indices2[pointer++] = bottomLeft;
				indices2[pointer++] = topRight;
				indices2[pointer++] = topRight;
				indices2[pointer++] = bottomLeft;
				indices2[pointer++] = bottomRight;
				
			}
		}
    
		//////////

/////////////Part of terrain class
	public float getHeight(int x, int z, BufferedImage image)
	{
		//image is 256 x 256, 32 bit png
		float height = image.getRGB(x, z);
		
		//MAX_PIXEL_COLOUR = 256 * 256 * 256
		//MAX_HEIGHT = 40
		height  += MAX_PIXEL_COLOUR/2f;
		height /= MAX_PIXEL_COLOUR/2f;
		height *= MAX_HEIGHT;
		
		return height;
		}
	
	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;
	}


	public float getTheHeight(float worldX, float worldZ, float xOfGrid, float zOfGrid , float [][]theHeights)
	{
		float answer = 0;
		//world coordinates not used yet
		float terrainX = worldX - xOfGrid;
		float terrainZ = worldZ - zOfGrid;
		//gridsquaresize = grid size / vertex - 1
		//180 / 79 = 2.278f
		float gridSquareSize = 2.278f;  
		int gridX = (int) Math.floor(terrainX/gridSquareSize);
		int gridZ = (int) Math.floor(terrainZ/gridSquareSize);
		
		float xCoord = (terrainX % gridSquareSize)/gridSquareSize;
		float zCoord = (terrainZ % gridSquareSize)/gridSquareSize;
		
		if (xCoord <= (1-zCoord))
		{
			answer = barryCentric(new Vector3f(0, theHeights[gridX][gridZ], 0), new Vector3f(1,
					theHeights[gridX + 1][gridZ], 0), new Vector3f(0,
					theHeights[gridX][gridZ + 1], 1), new Vector2f(xCoord, zCoord));
		}else
		{
			answer = barryCentric(new Vector3f(1, theHeights[gridX + 1][gridZ], 0), new Vector3f(1,
					theHeights[gridX + 1][gridZ + 1], 1), new Vector3f(0,
					theHeights[gridX][gridZ + 1], 1), new Vector2f(xCoord, zCoord));
		}
		
    	
		return(answer);
	}
	/////////////

Thanks!

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.