Bellow you will find the code:
I want to point out, that I followed the beginner tutorial and I still get a nullpointerexception using the bulletAppState object created from the BulletAppState class.
What I want to do is make the chess blocks solid (or the floor) so that the chess pieces can fall onto them when released.
package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Sphere;
import com.jme3.util.SkyFactory;
import mygame.CameraMode.RtsCam;
/**
* test
* @author normenhansen
*/
public class Main extends SimpleApplication {
public static void main(String[] args){
Main app = new Main();
app.start();
}
private RtsCam myCam;
private Node targetPool = new Node("Targetable");
private Node currentTable = new Node("CurrentTable");
//Node chessTable = new Node("ChessTable");
private Vector3f blockSize = new Vector3f(10f, 1f, 10f);
private BulletAppState bulletAppState;
@Override
public void simpleInitApp() {
bulletAppState = new BulletAppState();
flyCam.setEnabled(false);
setDisplayFps(false);
setDisplayStatView(false);
Spatial mySky = SkyFactory.createSky(assetManager, "Textures/Sky/Bright/BrightSky.dds", false);
mySky.setName("Sky");
mySky.setQueueBucket(RenderQueue.Bucket.Sky);
mySky.setCullHint(Spatial.CullHint.Never);
Spatial myTerrain = assetManager.loadModel("Scenes/myTerrainScene.j3o");
Material myTerrainMats = new Material(assetManager, "Common/MatDefs/Misc/ColoredTextured.j3md");
myTerrainMats.setTexture("ColorMap", assetManager.loadTexture("Textures/dirt.jpg"));
myTerrain.setMaterial(myTerrainMats);
myTerrain.setName("Ground");
//RigidBodyControl myTerrain_phy = new RigidBodyControl(0.0f);
//myTerrain.addControl(myTerrain_phy);
//bulletAppState.getPhysicsSpace().add(myTerrain_phy);
myCam = new RtsCam(RtsCam.UpVector.Y_UP);
myCam.setDistance(265);
stateManager.attach(myCam);
currentTable.attachChild(makeTable("Table0", 70, 70, 2, 2, true, bulletAppState));
targetPool.attachChild(currentTable);
rootNode.attachChild(mySky);
rootNode.attachChild(myTerrain);
rootNode.attachChild(targetPool);
initMark();
inputManager.addMapping("LeftMouseButton", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addMapping("RightMouseButton", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
inputManager.addListener(mouseSelectionEvent, "LeftMouseButton", "RightMouseButton");
}
private Node makeTable(String nodeName, float xOffset, float yOffset, float xSeparation, float ySeparation, boolean withBorder, BulletAppState bAppState){
Node table = new Node(nodeName);
if(withBorder){
for(int z = 1; z <= 9; z++){
for(int x = 1; x <= 9; x++){
Geometry block = null;
if(x==1&&z==1){
block = new Geometry("Bz"+(x)+""+(z), new Box(blockSize.x, blockSize.y, blockSize.z));
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
block.setLocalTranslation(xOffset-blockSize.x*(x-1)*xSeparation, 1, yOffset-blockSize.z*(z-1)*ySeparation);
mat.setColor("Color", ColorRGBA.Yellow);
block.setMaterial(mat);
} else if(x>=2&&x<=9&&z==1){
block = new Geometry("Bz"+(x)+""+(z), new Box(blockSize.x, blockSize.y, blockSize.z));
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
block.setLocalTranslation(xOffset-blockSize.x*(x-1)*xSeparation, 1, yOffset-blockSize.z*(z-1)*ySeparation);
mat.setColor("Color", ColorRGBA.Yellow);
block.setMaterial(mat);
} else if(x==1&&z>=2&&z<=9){
block = new Geometry("Bz"+(x)+""+(z), new Box(blockSize.x, blockSize.y, blockSize.z));
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
block.setLocalTranslation(xOffset-blockSize.x*(x-1)*xSeparation, 1, yOffset-blockSize.z*(z-1)*ySeparation);
mat.setColor("Color", ColorRGBA.Yellow);
block.setMaterial(mat);
} else if(x>=2&&z>=2&&x<=9&&z<=9){
block = new Geometry("Bx"+(x-1)+""+(z-1), new Box(blockSize.x, blockSize.y, blockSize.z));
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
block.setLocalTranslation(xOffset-blockSize.x*(x-1)*xSeparation, 1, yOffset-blockSize.z*(z-1)*ySeparation);
if(x%2==1&&z%2==1){
mat.setColor("Color", ColorRGBA.DarkGray);
block.setMaterial(mat);
} else if(x%2==0&&z%2==1){
mat.setColor("Color", ColorRGBA.White);
block.setMaterial(mat);
} else if(x%2==1&&z%2==0){
mat.setColor("Color", ColorRGBA.White);
block.setMaterial(mat);
} else if(x%2==0&&z%2==0){
mat.setColor("Color", ColorRGBA.DarkGray);
block.setMaterial(mat);
}
}
if(bAppState!=null&&block!=null){
RigidBodyControl rbody = new RigidBodyControl(0.0f);
block.addControl(rbody);
bAppState.getPhysicsSpace().add(rbody);
}
table.attachChild(block);
}
}
} else {
for(int z = 1; z <= 8; z++){
for(int x = 1; x <= 8; x++){
Geometry block = new Geometry("Bx"+x+""+z, new Box(blockSize.x, blockSize.y, blockSize.z));
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
block.setLocalTranslation(xOffset-blockSize.x*(x-1)*xSeparation, 1, yOffset-blockSize.z*(z-1)*ySeparation);
if(x%2==1&&z%2==1){
mat.setColor("Color", ColorRGBA.DarkGray);
block.setMaterial(mat);
} else if(x%2==0&&z%2==1){
mat.setColor("Color", ColorRGBA.White);
block.setMaterial(mat);
} else if(x%2==1&&z%2==0){
mat.setColor("Color", ColorRGBA.White);
block.setMaterial(mat);
} else if(x%2==0&&z%2==0){
mat.setColor("Color", ColorRGBA.DarkGray);
block.setMaterial(mat);
}
if(bAppState!=null&&block!=null){
RigidBodyControl rbody = new RigidBodyControl(0.0f);
block.addControl(rbody);
bAppState.getPhysicsSpace().add(rbody);
}
table.attachChild(block);
}
}
}
return table;
}
CollisionResults collisionState;
private boolean leftMouseButtonDraggable = true;
private boolean rightMouseButtonDraggable = true;
@Override
public void simpleUpdate(float tpf){
rootNode.updateGeometricState();
myCam.update(tpf);
Vector3f origin = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f);
Vector3f direction = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f);
direction.subtractLocal(origin).normalizeLocal();
Ray ray = new Ray(origin, direction);
collisionState = new CollisionResults();
targetPool.collideWith(ray, collisionState);
if (collisionState.size() > 0) {
CollisionResult closest = collisionState.getClosestCollision();
mark.setLocalTranslation(closest.getContactPoint());
mark.setMaterial(closest.getGeometry().getMaterial());
rootNode.attachChild(mark);
if(leftMouseButtonPressed){
if(leftMouseButtonDraggable){
leftMouseButtonDraggable = false;
} else {
System.out.println("Left Mouse Button Pressed Over: " + closest.getGeometry().getName());
}
} else {
System.out.println("Left Mouse Button Released Over: " + closest.getGeometry().getName());
leftMouseButtonDraggable = true;
}
if(rightMouseButtonPressed){
if(rightMouseButtonDraggable){
if(!leftMouseButtonDraggable){
leftMouseButtonDraggable = true;
}
rightMouseButtonDraggable = false;
} else {
//System.out.println("Left Mouse Button Pressed Over: " + closest.getGeometry().getName());
}
} else {
//System.out.println("Left Mouse Button Released Over: " + closest.getGeometry().getName());
rightMouseButtonDraggable = true;
}
} else {
rootNode.detachChild(mark);
}
}
private Geometry mark;
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);
}
public void chessTableScale(float scale){
blockSize = blockSize.scaleAdd(scale, blockSize);
}
private boolean leftMouseButtonPressed = false;
private boolean rightMouseButtonPressed = false;
private ActionListener mouseSelectionEvent = new ActionListener(){
public void onAction(String e, boolean isPressed, float tpf) {
if(e.equals("LeftMouseButton")){
leftMouseButtonPressed = isPressed;
} else if(e.equals("RightMouseButton")){
rightMouseButtonPressed = isPressed;
}
}
};
}