# 2d array to calculate height for player in heightmap?

I'm trying to work out how to do so my player position takes into the account of the floor so if I generate a floor that is hilly when I walk I won't walk through the heel. The problem is that I'm not sure how to do it.

I can do basic collision detection for walls. However, this isn't collision detection. I need the add the height of the terrain to the player.

So as you see the terrain is really a grid with 4 points making a face. So I can get the 4 points and work out. I can probably work this out on myself, but I was wondering if I should store say the heigh at (x,z) in a 2d array or do I just work out the height again everytime I move?

It just I use simplexnoise and I worry that this will be expensive to do every turn.

 import java.util.ArrayList;

public class VoxelLevel
{
Mesh mesh;
Material material;
Transform transform;
Shader shader;

VoxelLevel(String textureName)
{
material = new Material(new Texture(textureName));
transform = new Transform();

shader = BasicShader.getInstance();

generateLevel();

}

private void generateLevel()
{
ArrayList<Vertex> vertices = new ArrayList<Vertex>();
ArrayList<Integer> indices = new ArrayList<Integer>();

int sizeX = 10;
int sizeZ = 10;
int freq = 25;

for(int x=-300; x<300; x+= sizeX)
{
for(int z=-300; z<300; z+=sizeZ)
{
indices.add(vertices.size()+0);
indices.add(vertices.size()+3);
indices.add(vertices.size()+2);
indices.add(vertices.size()+0);
indices.add(vertices.size()+1);
indices.add(vertices.size()+3);

vertices.add(new Vertex(new Vector3f(x,50+25*(float)(SimplexNoise.noise(x/freq, z/freq) + 0.25 * SimplexNoise.noise(2*x/freq, 2*z/freq)),z), new Vector2f(0.75f,0.75f)));
vertices.add(new Vertex(new Vector3f(x+sizeX,50+25*(float)(SimplexNoise.noise((x+sizeX)/freq, z/freq) + 0.25 * SimplexNoise.noise(2*(x+sizeX)/freq, 2*z/freq)),z), new Vector2f(0.75f,1.0f)));
vertices.add(new Vertex(new Vector3f(x,50+25*(float)(SimplexNoise.noise(x/freq, (z+sizeZ)/freq)+ 0.25 * SimplexNoise.noise(2*x/freq, 2*(z+sizeZ)/freq)), z+sizeZ), new Vector2f(1.0f,0.75f)));
vertices.add(new Vertex(new Vector3f(x+sizeX,50+25*(float)(SimplexNoise.noise((x+sizeX)/freq, (z+sizeZ)/freq)+ 0.25 * SimplexNoise.noise(2*(x+sizeX)/freq, 2*(z+sizeZ)/freq)),z+sizeZ), new Vector2f(1.0f,1.0f)));
}
}

Vertex[] vertArray = new Vertex[vertices.size()];
Integer[] intArray = new Integer[indices.size()];

vertices.toArray(vertArray);
indices.toArray(intArray);

mesh = new Mesh(vertArray, Util.toIntArray(intArray));
}

public void input()
{

}

public void update()
{
//SimplexNoise.noise(0.25, 0.25);
//SimplexNoise.noise(0.35, 0.65);
//SimplexNoise.noise(0.254, 0.225);
//SimplexNoise.noise(0.254, 0.215);
}

public void render()
{
shader.bind();
shader.updateUniforms(transform.getTransformation(), transform.getProjectedTransformation(), material);
mesh.draw();
}

}

• Sounds like you should try both ways and see what works for you. The only problem I see you describing here is a design indecision problem. And that's a problem you need to solve on your own. – MichaelHouse Oct 8 '13 at 0:21
• Byte56 I was more worried about computational costs. Like I have to call PerlinNoise 4 times every frame versus say storing a 100x100 array or even 4 * 100x100 array. I was under the impression that PerlinNoise was expensive calculation. But, it seems to have no effect on my fps. But, then I was told that fps is misleading. – pangaea Oct 8 '13 at 0:40
• Profiling your code is a good way to find out if one way is faster than another. It's more accurate than an answer you'll get here since it's actual instead of theoretical. Netbeans has a nice profiler. – MichaelHouse Oct 8 '13 at 1:39
• I don't think you need to profile to know that a array look up is going to be faster then calculating the perlin noise at a given point. Store the vertArray and get the points from that. Something like i0 = (x * 600 + z) * 4 – nykwil Oct 9 '13 at 20:45

## 1 Answer

Getting the height from your terrain at any x/z point is super fast so there's no reason to store and manage a cache, it just adds complexity.