Added test case for terrain and fixed player box size
This commit is contained in:
parent
003e885cf7
commit
85ffe136d3
5 changed files with 97 additions and 39 deletions
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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());
|
||||||
// x, y, z
|
setBuffer(VertexBuffer.Type.TexCoord, 2, generateTextureBuffer());
|
||||||
0, 0, 0,
|
setBuffer(VertexBuffer.Type.Normal, 3, generateNormalBuffer());
|
||||||
0, 0, height,
|
|
||||||
width, 0, height,
|
|
||||||
width, 0, 0,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Setup planes
|
|
||||||
setBuffer(VertexBuffer.Type.Index, 3, new short[]{
|
|
||||||
0, 1, 3,
|
|
||||||
3, 1, 2
|
|
||||||
});
|
|
||||||
|
|
||||||
// Setup texture UV coordinates
|
|
||||||
setBuffer(VertexBuffer.Type.TexCoord, 2, new float[]{
|
|
||||||
// u, v
|
|
||||||
0, 0,
|
|
||||||
1, 0,
|
|
||||||
1, 1,
|
|
||||||
0, 1
|
|
||||||
});
|
|
||||||
|
|
||||||
// Setup light normals
|
|
||||||
setBuffer(VertexBuffer.Type.Normal, 3, new float[]{
|
|
||||||
0, 0, 1,
|
|
||||||
0, 0, 1,
|
|
||||||
0, 0, 1,
|
|
||||||
0, 0, 1
|
|
||||||
});
|
|
||||||
|
|
||||||
updateBound();
|
updateBound();
|
||||||
setStatic();
|
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
|
||||||
|
0, 0, 0,
|
||||||
|
0, 0, height,
|
||||||
|
width, 0, height,
|
||||||
|
width, 0, 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a index buffer containing planes for the mesh.
|
||||||
|
*/
|
||||||
|
protected short[] generateIndexBuffer() {
|
||||||
|
return new short[]{
|
||||||
|
0, 1, 3,
|
||||||
|
3, 1, 2
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a texture buffer containing texture UV coordinates.
|
||||||
|
*/
|
||||||
|
protected float[] generateTextureBuffer() {
|
||||||
|
return new float[]{
|
||||||
|
// u, v
|
||||||
|
0, 0,
|
||||||
|
1, 0,
|
||||||
|
1, 1,
|
||||||
|
0, 1
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
36
core/test/se/cookery/gfx/terrain/TerrainMeshTest.java
Normal file
36
core/test/se/cookery/gfx/terrain/TerrainMeshTest.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue