There is one line warning about “local variable hides a field”
Edit: Sorry I copy/pasted it wrong
Edit2: I found the Error! Had to delete BlockTerrainControl on line 56…thank you pspeed, echospot, and destroflyer
Here’s my code:
[java]package mygame;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.jme3.app.SimpleApplication;
import com.jme3.math.Vector3f;
import com.jme3.math.Vector2f;
import com.jme3.system.AppSettings;
import com.cubes.;
import com.cubes.test.blocks.;
import com.cubes.test.*;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.math.Ray;
import com.jme3.scene.Node;
import com.jme3.font.BitmapText;
import com.jme3.scene.shape.Sphere;
import com.jme3.material.Material;
import com.jme3.scene.Geometry;
import com.jme3.math.ColorRGBA;
public class TestTutorial extends SimpleApplication{
public static void main(String[] args){
TestTutorial app = new TestTutorial();
app.start();
}
public TestTutorial(){
settings = new AppSettings(true);
settings.setWidth(1280);
settings.setHeight(720);
settings.setTitle("Cubes Demo - Tutorial");
}
private Node terrainNode;
private BlockTerrainControl blockTerrain;
@Override
public void simpleInitApp(){
initCrossHairs(); // a "+" in the middle of the screen to help aiming
initKeys(); // load custom key mappings
initMark(); // a red sphere to mark the hit
//You have to specify a valid application, the framework will use e.g. its assetManager
CubesTestAssets.registerBlocks();
//Create the block terrain (7x1x7 chunks)
BlockTerrainControl blockTerrain = new BlockTerrainControl(CubesTestAssets.getSettings(this), new Vector3Int(7, 1, 7));
//Specify location, heightmap filepath, maximum height and the block class
//(See the heightmap at the right)
blockTerrain.setBlocksFromHeightmap(new Vector3Int(0, 1, 0), "Textures/GimpCloudHeightmap10.jpg", 10, Block_Grass.class);
blockTerrain.setBlocksFromHeightmap(new Vector3Int(0, 1, 0), "Textures/GimpCloudHeightmap11.jpg", 10, Block_Stone.class);
//Add the block terrain to a node
terrainNode = new Node("terrainNode");
terrainNode.addControl(blockTerrain);
rootNode.attachChild(terrainNode);
/*
//This is your terrain, it contains the whole
//block world and offers methods to modify it
BlockTerrainControl blockTerrain = new BlockTerrainControl(CubesTestAssets.getSettings(this), new Vector3Int(1, 1, 1));
//To set a block, just specify the location and the block object
//(Existing blocks will be replaced)
blockTerrain.setBlock(new Vector3Int(0, 0, 0), Block_Wood.class);
blockTerrain.setBlock(new Vector3Int(0, 0, 1), Block_Wood.class);
blockTerrain.setBlock(new Vector3Int(1, 0, 0), Block_Wood.class);
blockTerrain.setBlock(new Vector3Int(1, 0, 1), Block_Stone.class);
blockTerrain.setBlock(0, 0, 0, Block_Grass.class); //For the lazy users :P
//You can place whole areas of blocks too: setBlockArea(location, size, block)
//(The specified block will be cloned each time)
//The following line will set 3 blocks on top of each other
//({1,1,1}, {1,2,3} and {1,3,1})
blockTerrain.setBlockArea(new Vector3Int(1, 1, 1), new Vector3Int(1, 3, 1), Block_Stone.class);
//Removing a block works in a similar way
blockTerrain.removeBlock(new Vector3Int(1, 2, 1));
blockTerrain.removeBlock(new Vector3Int(1, 3, 1));
//The terrain is a jME-Control, you can add it
//to a node of the scenegraph to display it
Node terrainNode = new Node();
terrainNode.addControl(blockTerrain);
rootNode.attachChild(terrainNode);
*/
cam.setLocation(new Vector3f(-10, 10, 16));
cam.lookAtDirection(new Vector3f(1, -0.56f, -1), Vector3f.UNIT_Y);
flyCam.setMoveSpeed(50);
}
/** Declaring the “Shoot” action and mapping to its triggers. /
private void initKeys() {
inputManager.addMapping(“Shoot”,
new KeyTrigger(KeyInput.KEY_SPACE), // trigger 1: spacebar
new MouseButtonTrigger(MouseInput.BUTTON_LEFT)); // trigger 2: left-button click
inputManager.addListener(actionListener, “Shoot”);
}
/* Defining the “Shoot” action: Determine what was hit and how to respond. */
private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals("Shoot") && !keyPressed) {
/*
// 1. Reset results list.
CollisionResults results = new CollisionResults();
// 2. Aim the ray from cam loc to cam direction.
Ray ray = new Ray(cam.getLocation(), cam.getDirection());
// 3. Collect intersections between Ray and Shootables in results list.
rootNode.collideWith(ray, results);
// 4. Print the results
System.out.println("----- Collisions? " + results.size() + "-----");
for (int i = 0; i < results.size(); i++) {
// For each hit, we know distance, impact point, name of geometry.
float dist = results.getCollision(i).getDistance();
Vector3f pt = results.getCollision(i).getContactPoint();
String hit = results.getCollision(i).getGeometry().getName();
System.out.println("* Collision #" + i);
System.out.println(" You shot " + hit + " at " + pt + ", " + dist + " wu away.");
}
// 5. Use the results (we mark the hit object)
if (results.size() > 0) {
// The closest collision point is what was truly hit:
CollisionResult closest = results.getClosestCollision();
// Let's interact - we mark the hit with a red dot.
mark.setLocalTranslation(closest.getContactPoint());
terrainNode.attachChild(mark); //attach to terrainNode instead of rootNode
} else {
// No hits? Then remove the red mark.
rootNode.detachChild(mark);
}
*/
//Get the free block gap, to which the user is loooking...
Vector3Int blockLocation = getCurrentPointedBlockLocation(true);
//(The block location is null, if the user looks in the sky or out of the map)
if(blockLocation != null){
//... and place a block there :)
blockTerrain.setBlock(blockLocation, Block_Wood.class);
}
}
}
};
/** A centred plus sign to help the player aim. */
protected void initCrossHairs() {
setDisplayStatView(false);
guiFont = assetManager.loadFont(“Interface/Fonts/Default.fnt”);
BitmapText ch = new BitmapText(guiFont, false);
ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);
ch.setText("+"); // crosshairs
ch.setLocalTranslation( // center
settings.getWidth() / 2 - ch.getLineWidth()/2, settings.getHeight() / 2 + ch.getLineHeight()/2, 0);
guiNode.attachChild(ch);
}
private Geometry mark;
/** A red ball that marks the last spot that was “hit” by the “shot”. */
protected void initMark() {
Sphere sphere = new Sphere(30, 30, 0.2f);
mark = new Geometry(“BOOM!”, sphere);
Material mark_mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mark_mat.setColor(“Color”, ColorRGBA.Red);
mark.setMaterial(mark_mat);
}
//“returns the CollisionResults of the mouse cursor and a specified node”
private CollisionResults getRayCastingResults(Node node){
Vector3f origin = cam.getWorldCoordinates(new Vector2f((settings.getWidth() / 2), (settings.getHeight() / 2)), 0.0f);
Vector3f direction = cam.getWorldCoordinates(new Vector2f((settings.getWidth() / 2), (settings.getHeight() / 2)), 0.3f);
direction.subtractLocal(origin).normalizeLocal();
Ray ray = new Ray(origin, direction);
CollisionResults results = new CollisionResults();
node.collideWith(ray, results);
return results;
}
//" use the closest CollisionResult to receive the collisionContactPoint with
//your BlockTerrain and to finally get the “pointed” block location"
private Vector3Int getCurrentPointedBlockLocation(boolean getNeighborLocation){
CollisionResults results = getRayCastingResults(terrainNode);
if(results.size() > 0){
Vector3f collisionContactPoint = results.getClosestCollision().getContactPoint();
return BlockNavigator.getPointedBlockLocation(blockTerrain, collisionContactPoint, getNeighborLocation);
}
return null;
}
}[/java]