Renamed some functions and variables to make more sense
This commit is contained in:
parent
682c06a5f6
commit
70534bcf2c
5 changed files with 62 additions and 69 deletions
|
|
@ -111,6 +111,8 @@ public class CookeryClient extends SimpleApplication {
|
|||
CollisionResults results = new CollisionResults();
|
||||
rootNode.collideWith(ray, results);
|
||||
|
||||
System.out.println("Click registered:");
|
||||
|
||||
// (Print the results so we see what is going on:)
|
||||
for (int i = 0; i < results.size(); i++) {
|
||||
System.out.println("Selection #" + i + ": " +
|
||||
|
|
@ -118,7 +120,6 @@ public class CookeryClient extends SimpleApplication {
|
|||
"Contact: " + results.getCollision(i).getContactPoint() + ", " +
|
||||
"Distance: " + results.getCollision(i).getDistance() + " WU");
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
// Use the results -- we rotate the selected geometry.
|
||||
if (results.size() > 0) {
|
||||
|
|
|
|||
|
|
@ -13,12 +13,7 @@ public class BlockHeightMap implements TerrainHeightMap {
|
|||
|
||||
|
||||
@Override
|
||||
public int getSquareCountWidth() {
|
||||
return Block.BLOCK_SIZE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSquareCountHeight() {
|
||||
return Block.BLOCK_SIZE;
|
||||
public float getHeight(float x, float z) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,14 +2,5 @@ package se.cookery.gfx.terrain;
|
|||
|
||||
public interface TerrainHeightMap {
|
||||
|
||||
|
||||
/**
|
||||
* @return the number of squares on the width that make a single TerrainMesh instance.
|
||||
*/
|
||||
int getSquareCountWidth();
|
||||
|
||||
/**
|
||||
* @return the number of squares on the height that make a single TerrainMesh instance.
|
||||
*/
|
||||
int getSquareCountHeight();
|
||||
float getHeight(float x, float z);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
package se.cookery.gfx.terrain;
|
||||
|
||||
import com.jme3.material.Material;
|
||||
import com.jme3.math.ColorRGBA;
|
||||
import com.jme3.math.Vector3f;
|
||||
import com.jme3.scene.Geometry;
|
||||
import com.jme3.scene.Node;
|
||||
import se.cookery.gfx.util.MaterialUtil;
|
||||
|
||||
/**
|
||||
* This class represents a infinite terrain object (using multiple buffered TerrainMesh objects)
|
||||
|
|
@ -13,8 +11,8 @@ import se.cookery.gfx.util.MaterialUtil;
|
|||
public class TerrainManager {
|
||||
protected static final int BUFFER_SIZE = 3;
|
||||
|
||||
private float squareWidth;
|
||||
private float squareHeight;
|
||||
private float squareLength;
|
||||
private int squareCount;
|
||||
private TerrainHeightMap heightMap;
|
||||
|
||||
private Geometry[][] terrainBuffer;
|
||||
|
|
@ -24,26 +22,27 @@ public class TerrainManager {
|
|||
/**
|
||||
* Create a new instance of TerrainManager
|
||||
*
|
||||
* @param squareWidth is the width of a single square
|
||||
* @param squareHeight is the height of a single square
|
||||
* @param heightMap is the height map for the terrain
|
||||
* @param squareLength is the width of a single square
|
||||
* @param squareCount is the height of a single square
|
||||
* @param heightMap is the height map for the terrain
|
||||
*/
|
||||
public TerrainManager(float squareWidth, float squareHeight, TerrainHeightMap heightMap, Material material) {
|
||||
this.squareWidth = squareWidth;
|
||||
this.squareHeight = squareHeight;
|
||||
public TerrainManager(float squareLength, int squareCount, TerrainHeightMap heightMap, Material material) {
|
||||
this.squareLength = squareLength;
|
||||
this.squareCount = squareCount;
|
||||
this.heightMap = heightMap;
|
||||
this.terrainBuffer = new Geometry[BUFFER_SIZE][BUFFER_SIZE];
|
||||
|
||||
for (int x=0; x<BUFFER_SIZE; x++) {
|
||||
for (int z=0; z<BUFFER_SIZE; z++) {
|
||||
TerrainMesh terrainMesh = new TerrainMesh(squareWidth, squareHeight, heightMap.getSquareCountWidth(), heightMap.getSquareCountHeight());
|
||||
TerrainMesh terrainMesh = new TerrainMesh(squareLength, squareLength, squareCount, squareCount);
|
||||
terrainBuffer[x][z] = new Geometry("TerrainMesh " + x + "x" + z, terrainMesh);
|
||||
terrainBuffer[x][z].setMaterial(material);
|
||||
terrainBuffer[x][z].setLocalTranslation(terrainMesh.getMeshLengthX() * x, 0, terrainMesh.getMeshLengthZ() * z);
|
||||
node.attachChild(terrainBuffer[x][z]);
|
||||
}
|
||||
}
|
||||
|
||||
setTerrainCenter(Vector3f.ZERO);
|
||||
setPlayerPosition(Vector3f.ZERO);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -51,11 +50,8 @@ public class TerrainManager {
|
|||
* Method will move the terrain center by reorganizing the buffer
|
||||
* by deallocating out of scope terrain and allocating new terrain that is in scope.
|
||||
*/
|
||||
protected void setTerrainCenter(Vector3f newCenter) {
|
||||
Vector3f newTerrainZero = new Vector3f(
|
||||
(squareWidth * heightMap.getSquareCountWidth()) * (int)(newCenter.getX()/(squareWidth * heightMap.getSquareCountWidth())),
|
||||
0,
|
||||
(squareHeight * heightMap.getSquareCountHeight()) * (int)(newCenter.getZ()/(squareHeight * heightMap.getSquareCountHeight())));
|
||||
protected void setPlayerPosition(Vector3f newCenter) {
|
||||
Vector3f newTerrainZero = getTerrainVector(newCenter);
|
||||
|
||||
if (newTerrainZero.equals(terrainZero))
|
||||
return;
|
||||
|
|
@ -67,13 +63,23 @@ public class TerrainManager {
|
|||
for (int z=0; z<BUFFER_SIZE; z++) {
|
||||
TerrainMesh terrainMesh = (TerrainMesh) terrainBuffer[x][z].getMesh();
|
||||
terrainBuffer[x][z].setLocalTranslation(
|
||||
terrainZero.getX() + (x - centerIndex) * terrainMesh.getWidth(),
|
||||
terrainZero.getX() + (x - centerIndex) * terrainMesh.getMeshLengthX(),
|
||||
0 ,
|
||||
terrainZero.getZ() + (z - centerIndex) * terrainMesh.getHeight());
|
||||
terrainZero.getZ() + (z - centerIndex) * terrainMesh.getMeshLengthZ());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a vector to the closest terrain mesh coordinate.
|
||||
*/
|
||||
private Vector3f getTerrainVector(Vector3f position) {
|
||||
return new Vector3f(
|
||||
(squareLength * squareCount) * (int)(position.getX()/(squareLength * squareCount)),
|
||||
0,
|
||||
(squareLength * squareCount) * (int)(position.getZ()/(squareLength * squareCount)));
|
||||
}
|
||||
|
||||
|
||||
public Node getGfxNode() {
|
||||
return node;
|
||||
|
|
|
|||
|
|
@ -9,25 +9,25 @@ import com.jme3.scene.VertexBuffer;
|
|||
*/
|
||||
public class TerrainMesh extends Mesh {
|
||||
|
||||
private float squareWidth;
|
||||
private float squareHeight;
|
||||
private int squareCountWidth;
|
||||
private int squareCountHeight;
|
||||
private float squareLengthX;
|
||||
private float squareLengthZ;
|
||||
private int squareCountX;
|
||||
private int squareCountZ;
|
||||
|
||||
|
||||
/**
|
||||
* Created a terrain Mesh with the given sizes.
|
||||
*
|
||||
* @param squareWidth is the width of a single square
|
||||
* @param squareHeight is the height of a single square
|
||||
* @param squareCountWidth the number of squares that make up the whole terrain
|
||||
* @param squareCountHeight the number of squares that make up the whole terrain
|
||||
* @param squareLengthX is the width of a single square
|
||||
* @param squareLengthZ is the depth of a single square
|
||||
* @param squareCountX the number of squares that make up the whole terrain on the X axis
|
||||
* @param squareCountZ the number of squares that make up the whole terrain on the Z axis
|
||||
*/
|
||||
public TerrainMesh(float squareWidth, float squareHeight, int squareCountWidth, int squareCountHeight){
|
||||
this.squareWidth = squareWidth;
|
||||
this.squareHeight = squareHeight;
|
||||
this.squareCountWidth = squareCountWidth;
|
||||
this.squareCountHeight = squareCountHeight;
|
||||
public TerrainMesh(float squareLengthX, float squareLengthZ, int squareCountX, int squareCountZ){
|
||||
this.squareLengthX = squareLengthX;
|
||||
this.squareLengthZ = squareLengthZ;
|
||||
this.squareCountX = squareCountX;
|
||||
this.squareCountZ = squareCountZ;
|
||||
|
||||
updateGeometry();
|
||||
}
|
||||
|
|
@ -46,16 +46,16 @@ public class TerrainMesh extends Mesh {
|
|||
* Generates a vertex buffer containing vertex xyz coordinates.
|
||||
*/
|
||||
protected float[] generateVertexBuffer() {
|
||||
int vertexCountWidth = squareCountWidth + 1;
|
||||
int vertexCountHeight = squareCountHeight + 1;
|
||||
float[] vertexBuf = new float[3 * vertexCountWidth * vertexCountHeight];
|
||||
int vertexCountWidth = squareCountX + 1;
|
||||
int vertexCountDepth = squareCountZ + 1;
|
||||
float[] vertexBuf = new float[3 * vertexCountWidth * vertexCountDepth];
|
||||
|
||||
for (int row=0; row<vertexCountHeight; row++) {
|
||||
for (int row=0; row<vertexCountDepth; row++) {
|
||||
for (int col=0; col<vertexCountWidth; col++) {
|
||||
int vertexIndex = (3 * vertexCountWidth * row) + (3 * col);
|
||||
vertexBuf[vertexIndex + 0] = squareWidth * col; // X
|
||||
vertexBuf[vertexIndex + 0] = squareLengthX * col; // X
|
||||
vertexBuf[vertexIndex + 1] = 0; // Y
|
||||
vertexBuf[vertexIndex + 2] = squareHeight * row; // Z
|
||||
vertexBuf[vertexIndex + 2] = squareLengthZ * row; // Z
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -66,17 +66,17 @@ public class TerrainMesh extends Mesh {
|
|||
* Generates a index buffer containing planes for the mesh.
|
||||
*/
|
||||
protected short[] generateIndexBuffer() {
|
||||
int vertexCountWidth = squareCountWidth + 1;
|
||||
int triangleCount = squareCountWidth * squareCountHeight * 2;
|
||||
int vertexCountWidth = squareCountX + 1;
|
||||
int triangleCount = squareCountX * squareCountZ * 2;
|
||||
short[] indexBuf = new short[3 * triangleCount];
|
||||
|
||||
for (int row = 0; row< squareCountHeight; row++) {
|
||||
for (int col = 0; col< squareCountWidth; col++) {
|
||||
for (int row = 0; row< squareCountZ; row++) {
|
||||
for (int col = 0; col< squareCountX; col++) {
|
||||
int vertexTopLeft = (row * vertexCountWidth) + col;
|
||||
int vertexTopRight = vertexTopLeft + 1;
|
||||
int vertexBottomLeft = ((row + 1) * vertexCountWidth) + col;
|
||||
int vertexBottomRight = vertexBottomLeft + 1;
|
||||
int indexOffset = 3 * 2 * ((row * squareCountWidth) + col);
|
||||
int indexOffset = 3 * 2 * ((row * squareCountX) + col);
|
||||
|
||||
indexBuf[indexOffset + 0] = (short) vertexTopLeft;
|
||||
indexBuf[indexOffset + 1] = (short) vertexBottomLeft;
|
||||
|
|
@ -117,19 +117,19 @@ public class TerrainMesh extends Mesh {
|
|||
}
|
||||
|
||||
|
||||
public float getWidth() {
|
||||
return squareWidth * squareCountWidth;
|
||||
public float getMeshLengthX() {
|
||||
return squareLengthX * squareCountX;
|
||||
}
|
||||
|
||||
public float getHeight() {
|
||||
return squareHeight * squareCountHeight;
|
||||
public float getMeshLengthZ() {
|
||||
return squareLengthZ * squareCountZ;
|
||||
}
|
||||
|
||||
public float getSquareWidth() {
|
||||
return squareWidth;
|
||||
public float getSquareLengthX() {
|
||||
return squareLengthX;
|
||||
}
|
||||
|
||||
public float getSquareHeight() {
|
||||
return squareHeight;
|
||||
public float getSquareLengthZ() {
|
||||
return squareLengthZ;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue