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();
}
}