Added test case for terrain and fixed player box size

This commit is contained in:
Ziver Koc 2020-04-21 00:35:11 +02:00
parent 003e885cf7
commit 85ffe136d3
5 changed files with 97 additions and 39 deletions

View file

@ -14,7 +14,7 @@ public class Player {
public Player(AssetManager assetManager) { public Player(AssetManager assetManager) {
Geometry box = GeometryUtil.createBox(assetManager, 0.5f,0.7f,0.5f, ColorRGBA.Red); Geometry box = GeometryUtil.createBox(assetManager, 0.5f,0.7f,0.5f, ColorRGBA.Red);
box.setLocalTranslation(0.25f, 0.7f, 0.25f); box.setLocalTranslation(0.5f, 0.7f, 0.5f);
playerNode.attachChild(box); playerNode.attachChild(box);
} }

View file

@ -32,47 +32,69 @@ public class TerrainMesh extends Mesh {
* @param heightCount the number of squares that make up the whole terrain * @param heightCount the number of squares that make up the whole terrain
*/ */
public TerrainMesh(float width, float height, int widthCount, int heightCount){ public TerrainMesh(float width, float height, int widthCount, int heightCount){
this.width = 1; this.width = width;
this.height = 1; this.height = height;
updateGeometry(); updateGeometry();
} }
protected void updateGeometry() { protected void updateGeometry() {
// Setup vertexes setBuffer(VertexBuffer.Type.Position, 3, generateVertexBuffer());
setBuffer(VertexBuffer.Type.Position, 3, new float[]{ setBuffer(VertexBuffer.Type.Index, 3, generateIndexBuffer());
setBuffer(VertexBuffer.Type.TexCoord, 2, generateTextureBuffer());
setBuffer(VertexBuffer.Type.Normal, 3, generateNormalBuffer());
updateBound();
setStatic();
}
/**
* Generates a vertex buffer containing vertex xyz coordinates.
*/
protected float[] generateVertexBuffer() {
float[] vertexBuf = new float[3 * widthCount * heightCount];
return new float[]{
// x, y, z // x, y, z
0, 0, 0, 0, 0, 0,
0, 0, height, 0, 0, height,
width, 0, height, width, 0, height,
width, 0, 0, width, 0, 0,
}); };
}
// Setup planes /**
setBuffer(VertexBuffer.Type.Index, 3, new short[]{ * Generates a index buffer containing planes for the mesh.
*/
protected short[] generateIndexBuffer() {
return new short[]{
0, 1, 3, 0, 1, 3,
3, 1, 2 3, 1, 2
}); };
}
// Setup texture UV coordinates /**
setBuffer(VertexBuffer.Type.TexCoord, 2, new float[]{ * Generates a texture buffer containing texture UV coordinates.
*/
protected float[] generateTextureBuffer() {
return new float[]{
// u, v // u, v
0, 0, 0, 0,
1, 0, 1, 0,
1, 1, 1, 1,
0, 1 0, 1
}); };
}
// Setup light normals /**
setBuffer(VertexBuffer.Type.Normal, 3, new float[]{ * Generates a normal buffer containing light normals.
*/
protected float[] generateNormalBuffer() {
return new float[]{
0, 0, 1, 0, 0, 1,
0, 0, 1, 0, 0, 1,
0, 0, 1, 0, 0, 1,
0, 0, 1 0, 0, 1
}); };
updateBound();
setStatic();
} }
} }

View file

@ -7,8 +7,8 @@ import com.jme3.scene.shape.Box;
public class GeometryUtil { public class GeometryUtil {
public static Geometry createBox(AssetManager assetManager, float x, float y, float z, ColorRGBA color) { public static Geometry createBox(AssetManager assetManager, float width, float height, float depth, ColorRGBA color) {
Geometry box = new Geometry("Box", new Box(x,y,z)); Geometry box = new Geometry("Box", new Box(width/2, height/2, depth/2));
box.setMaterial(MaterialUtil.createMaterial(assetManager, color)); box.setMaterial(MaterialUtil.createMaterial(assetManager, color));
return box; return box;

View file

@ -12,8 +12,8 @@ class BlockTest {
Block block = new Block(0, 0); Block block = new Block(0, 0);
assertEquals(0, block.getBlockStartX()); assertEquals(0, block.getBlockStartX());
assertEquals(0, block.getBlockStartY()); assertEquals(0, block.getBlockStartY());
assertEquals(19, block.getBlockEndX()); assertEquals(Block.BLOCK_SIZE-1, block.getBlockEndX());
assertEquals(19, block.getBlockEndY()); assertEquals(Block.BLOCK_SIZE-1, block.getBlockEndY());
} }
@Test @Test

View file

@ -0,0 +1,36 @@
package se.cookery.gfx.terrain;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class TerrainMeshTest {
@Test
public void singleSquareTerrain(){
TerrainMesh terrain = new TerrainMesh(1, 1, 1 ,1);
float[] index = terrain.generateVertexBuffer();
assertEquals(3*4, index.length);
assertArrayEquals(new float[]{
0, 0, 0,
0, 0, 1,
1, 0, 1,
1, 0, 0,
}, index);
}
@Test
public void fourSquareTerrain(){
TerrainMesh terrain = new TerrainMesh(1, 1, 2 ,2);
float[] index = terrain.generateVertexBuffer();
assertEquals(3*9, index.length);
assertArrayEquals(new float[]{
0, 0, 0,
0, 0, 1,
1, 0, 1,
1, 0, 0,
}, index);
}
}