So I’v been working on a FPS in JMonkey for a few weeks now, and although I know Java fairly well, and have read most of the tutorials and docs, I’m rather new to Jmonkey. It was working great, up until last night when I ran my project, and got a NullPointerException in BinaryImporter.load. it looked something like this:
java.lang.NullPointerException
at com.jme3.export.binary.BinaryImporter.readObject(BinaryImporter.java:316)
at com.jme3.export.binary.BinaryImporter.load(BinaryImporter.java:242)
at com.jme3.export.binary.BinaryImporter.load(BinaryImporter.java:125)
at com.jme3.export.binary.BinaryImporter.load(BinaryImporter.java:109)
at com.jme3.asset.DesktopAssetManager.loadAsset(DesktopAssetManager.java:288)
at com.jme3.asset.DesktopAssetManager.loadModel(DesktopAssetManager.java:374)
at com.jme3.asset.DesktopAssetManager.loadModel(DesktopAssetManager.java:378)
at mygame.Main.simpleInitApp(Main.java:200)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:226)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:130)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:207)
at java.lang.Thread.run(Thread.java:744)
I checked and double checked all of my code, and found no errors!
When I looked through the error to find the code positioning, the first reference to code that was my own (not built in JMONKEY) was at mygame.Main.simpleInitApp(Main.java:200), and that line of code looks like this:
Node rifle = (Node) this.assetManager.loadModel("Models/Pistol_1911/held_1911.j3o");'
(I know that the node is called rifle, and references a pistol model. I was playing around with versatility of my rifle controll at the time of the error)
Files of my src path:
mygame:
Main.java(extends SimpleApplication)
Settings.java(a JFrame set up by Netbeans gui builder for modifying settings (I didnt want to get into NIFTY yet))
Start.java(contains main(String[] args) void, simply starts the app);
mygame.customcontrollers:
Rifle.java(Extends AbstractControl for controlling held weapon)
Main.java contents:
package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.export.binary.BinaryExporter;
import com.jme3.font.BitmapText;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.debug.Arrow;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Cylinder;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import mygame.customcontrollers.Rifle;
public class Main extends SimpleApplication
implements ActionListener {
private Spatial sceneModel;
public BulletAppState bulletAppState;
private RigidBodyControl landscape;
private CharacterControl player;
private Vector3f walkDirection = new Vector3f();
private boolean left = false, right = false, up = false, down = false;
//Temporary vectors used on each frame.
private Vector3f camDir = new Vector3f();
private Vector3f camLeft = new Vector3f();
public static void main(String[] args) {
Main app = new Main();
app.start();
}
boolean playerAttached = false;
boolean refreshSettings = true;
public void setPlayerAttached(boolean b) {
try {
if (b) {
bulletAppState.getPhysicsSpace().add(player);
} else {
bulletAppState.getPhysicsSpace().remove(player);
}
} catch (Exception e) {
}
}
public Node player_node;
public void initPlayer() {
Node n;
n = (Node)assetManager.loadModel("Models/player_marker.j3o");
n.setName("player_marker");
player_node = n;
CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(3f, 12f, 1);
player = new CharacterControl(capsuleShape, 0.05f);
player.setJumpSpeed(20);
player.setFallSpeed(30);
player.setGravity(30);
player.setPhysicsLocation(startLocation);
rootNode.attachChild(player_node);
cords = new BitmapText(guiFont, false);
cords.setSize(guiFont.getCharSet().getRenderedSize() * 2);
cords.setColor(ColorRGBA.White);
cords.setLocalTranslation(0f, settings.getHeight(), 0);
guiNode.attachChild(cords);
}
Vector3f startLocation = new Vector3f(0, 10, 0);
public void loadMap(String path) {
try {
rootNode.detachChildNamed("scene");
} catch (Exception e) {
}
try {
bulletAppState.getPhysicsSpace().remove(landscape);
} catch (Exception e) {
}
// We load the scene from the zip file and adjust its size.
sceneModel = assetManager.loadModel(path);
sceneModel.setLocalScale(2f);
sceneModel.setName("scene");
CollisionShape sceneShape =
CollisionShapeFactory.createMeshShape((Node) sceneModel);
landscape = new RigidBodyControl(sceneShape, 0);
sceneModel.addControl(landscape);
rootNode.attachChild(sceneModel);
bulletAppState.getPhysicsSpace().add(landscape);
try{
Node marker = ((Node)((Node)sceneModel).getChild("player_marker"));
startLocation = marker.getLocalTranslation();
((Node)sceneModel).detachChild(marker);
resetPlayer();
}catch(NullPointerException n){}
}
public void loadMap(Node model) {
try {
rootNode.detachChildNamed("scene");
} catch (Exception e) {
}
try {
bulletAppState.getPhysicsSpace().remove(landscape);
} catch (Exception e) {
}
sceneModel = model;
sceneModel.setLocalScale(2f);
sceneModel.setName("scene");
CollisionShape sceneShape =
CollisionShapeFactory.createMeshShape((Node) sceneModel);
landscape = new RigidBodyControl(sceneShape, 0);
sceneModel.addControl(landscape);
rootNode.attachChild(sceneModel);
bulletAppState.getPhysicsSpace().add(landscape);
try{
Node marker = ((Node)((Node)sceneModel).getChild("player_marker"));
startLocation = marker.getLocalTranslation();
((Node)sceneModel).detachChild(marker);
resetPlayer();
}catch(NullPointerException n){}
}
public BitmapText cross;
public void initCrossHairs() {
setDisplayStatView(false);
guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
cross = new BitmapText(guiFont, false);
cross.setSize(guiFont.getCharSet().getRenderedSize() * 2);
cross.setText("."); // crosshairs
cross.setLocalTranslation( // center
settings.getWidth() / 2 - cross.getLineWidth() / 2, settings.getHeight() / 2 + cross.getLineHeight() / 2, 0);
guiNode.attachChild(cross);
}
public void changeCrossHairs(String s) {
cross.setText(s); // crosshairs
cross.setLocalTranslation( // center
settings.getWidth() / 2 - cross.getLineWidth() / 2, settings.getHeight() / 2 + cross.getLineHeight() / 2, 0);
}
public void simpleInitApp() {
initCrossHairs();
/**
* Set up Physics
*/
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
//bulletAppState.getPhysicsSpace().enableDebug(assetManager);
// We re-use the flyby camera for rotation, while positioning is handled by physics
viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));
flyCam.setMoveSpeed(100);
setUpKeys();
/**
* A white, directional light source
*/
DirectionalLight sun = new DirectionalLight();
sun.setDirection((new Vector3f(-0.5f, -0.5f, -0.5f)).normalizeLocal());
sun.setColor(ColorRGBA.White);
rootNode.addLight(sun);
this.loadMap("Scenes/Test Scene03.j3o");
this.initPlayer();
this.setPlayerAttached(true);
Node rifle = (Node) this.assetManager.loadModel("Models/Pistol_1911/held_1911.j3o");
r = new Rifle(this);
//rifle.setMaterial(new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md"));
System.out.println("Created Controller " + r.toString());
rifle.setName("rifle_node");
rifle.getChild(0).addControl(r);
rootNode.attachChild(rifle);
// We set up collision detection for the player by creating
// a capsule collision shape and a CharacterControl.
// The CharacterControl offers extra settings for
// size, stepheight, jumping, falling, and gravity.
// We also put the player in its starting position.
// We attach the scene and the player to the rootnode and the physics space,
// to make them appear in the game world.
}
Rifle r;
/**
* We over-write some navigational key mappings here, so we can add
* physics-controlled walking and jumping:
*/
private void setUpKeys() {
inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping("Settings", new KeyTrigger(KeyInput.KEY_E));
inputManager.addListener(this, "Left");
inputManager.addListener(this, "Right");
inputManager.addListener(this, "Up");
inputManager.addListener(this, "Down");
inputManager.addListener(this, "Jump");
inputManager.addListener(this, "Settings");
}
public void createBoxTarget(Vector3f pos) {
Geometry g = makeCube("cube", 6, 6, 6);
rootNode.attachChild(g);
System.out.println("Attached " + g.getName() + " at " + pos.toString());
g.setLocalTranslation(pos);
}
Geometry makeCube(String name, float x, float y, float z) {
Box box = new Box(1, 1, 1);
Geometry cube = new Geometry(name, box);
cube.setLocalTranslation(x, y, z);
Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat1.setColor("Color", ColorRGBA.randomColor());
cube.setMaterial(mat1);
return cube;
}
public Geometry createCylinder(float width, float height, ColorRGBA c) {
Cylinder cap = new Cylinder(30, 30, width, height, true);
Geometry g = new Geometry("cylinder", cap);
Quaternion roll90 = new Quaternion();
roll90.fromAngleAxis(FastMath.PI / 2, new Vector3f(1, 0, 0));
/* The rotation is applied: The object rolls by 180 degrees. */
g.setLocalRotation(roll90);
Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat1.setColor("Color", c);
g.setMaterial(mat1);
return g;
}
public Material makeMaterial(String name, ColorRGBA color) {
Material mat = new Material(assetManager, name);
mat.setColor("Color", color);
return mat;
}
public Geometry showVector3fArrow(Vector3f v, ColorRGBA color, String name) {
Arrow a = new Arrow(v);
Material mat = makeMaterial("Common/MatDefs/Misc/Unshaded.j3md", color);
Geometry geom = new Geometry(name, a);
geom.setMaterial(mat);
return geom;
}
BitmapText cords;
public void updateCords() {
cords.setText("X:" + player.getPhysicsLocation().getX() + " Y:" + player.getPhysicsLocation().getY() + " Z:" + player.getPhysicsLocation().getZ()); // crosshairs
}
@Override
public void stop() {
guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
BitmapText saving = new BitmapText(guiFont, false);
saving.setSize(guiFont.getCharSet().getRenderedSize() * 6);
saving.setText("Prepairing world for binary save"); // crosshairs
saving.setColor(ColorRGBA.Green);
saving.setLocalTranslation( // center
settings.getWidth() / 2 - saving.getLineWidth() / 2, settings.getHeight() / 2 + cross.getLineHeight() / 2, 0);
guiNode.attachChild(saving);
((Node) rootNode.getChild("rifle_node")).getChild(0).removeControl(r);
Node n;
n = (Node)assetManager.loadModel("Models/player_marker.j3o");
n.setName("player_marker");
Node c = new Node("Camera_Direction");
Vector3f dir = new Vector3f(cam.getDirection().getX() * 10, cam.getDirection().getY() * 10, cam.getDirection().getZ() * 10);
c.attachChild(showVector3fArrow(dir, ColorRGBA.Blue, "cam_direction"));
rootNode.attachChild(c);
rootNode.attachChild(n);
c.setLocalTranslation(cam.getLocation());
n.setLocalTranslation(new Vector3f(player.getPhysicsLocation().getX(), player.getPhysicsLocation().getY() - 2.9927845f, player.getPhysicsLocation().getZ()));
/**
* Save a Node to a .j3o file.
*/
BinaryExporter exporter = BinaryExporter.getInstance();
File file = new File("C:\\Users\\alans_000\\Documents\\NetBeansProjects\\Trench Warfair\\assets\\Scenes\\last_run.j3o");
try {
System.out.println("Saving run to file");
exporter.save(rootNode, file);
System.out.println("Run Saved");
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Failed to save node!", ex);
}
super.stop();
System.exit(0);
}
/**
* These are our custom actions triggered by key presses. We do not walk
* yet, we just keep track of the direction the user pressed.
*/
public void onAction(String binding, boolean isPressed, float tpf) {
if (binding.equals("Left")) {
left = isPressed;
} else if (binding.equals("Right")) {
right = isPressed;
} else if (binding.equals("Up")) {
up = isPressed;
} else if (binding.equalsIgnoreCase("Settings") && isPressed) {
Settings s = new Settings(this);
s.setLocationRelativeTo(null);
s.setVisible(true);
} else if (binding.equals("Down")) {
down = isPressed;
} else if (binding.equals("Jump")) {
if (isPressed) {
player.jump();
}
}
}
/**
* This is the main event loop--walking happens here. We check in which
* direction the player is walking by interpreting the camera direction
* forward (camDir) and to the side (camLeft). The setWalkDirection()
* command is what lets a physics-controlled player walk. We also make sure
* here that the camera moves with player.
*/
@Override
public void simpleUpdate(float tpf) {
camDir.set(cam.getDirection()).multLocal(0.6f);
camLeft.set(cam.getLeft()).multLocal(0.4f);
walkDirection.set(0, 0, 0);
if (left) {
walkDirection.addLocal(camLeft);
}
if (right) {
walkDirection.addLocal(camLeft.negate());
}
if (up) {
walkDirection.addLocal(new Vector3f(camDir.getX(), 0f, camDir.getZ()));
}
if (down) {
walkDirection.addLocal(new Vector3f(camDir.negate().getX(), 0f, camDir.negate().getZ()));
}
player.setWalkDirection(walkDirection);
cam.setLocation(player.getPhysicsLocation());
listener.setLocation(cam.getLocation());
listener.setRotation(cam.getRotation());
updateCords();
player_node.setLocalTranslation(new Vector3f(player.getPhysicsLocation().getX(), player.getPhysicsLocation().getY() - 2.9927845f, player.getPhysicsLocation().getZ()));
//System.out.println("Player:" + cam.getLocation());
if (refreshSettings) {
System.out.println("Refreshing settings");
try {
rootNode.detachChild(player_node);
guiNode.detachChild(cords);
if (this.playerAttached) {
rootNode.attachChild(player_node);
} else {
}
if (cordsAttached){
guiNode.attachChild(cords);
}
} catch (NullPointerException n) {
}
refreshSettings = false;
}
}
public boolean cordsAttached = true;
private void resetPlayer() {
player.setPhysicsLocation(this.startLocation);
}
}
Settings.java contents:
package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.export.binary.BinaryExporter;
import com.jme3.font.BitmapText;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.debug.Arrow;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Cylinder;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import mygame.customcontrollers.Rifle;
/**
* Example 9 - How to make walls and floors solid. This collision code uses
* Physics and a custom Action Listener.
*
* @author normen, with edits by Zathras
*/
//NOTE---------------WHEN ITITALIZING A HELD OBJECT FROM DWIF EDITOR, USE (Node) s.getChild(0)
public class Main extends SimpleApplication
implements ActionListener {
private Spatial sceneModel;
public BulletAppState bulletAppState;
private RigidBodyControl landscape;
private CharacterControl player;
private Vector3f walkDirection = new Vector3f();
private boolean left = false, right = false, up = false, down = false;
//Temporary vectors used on each frame.
//They here to avoid instanciating new vectors on each frame
private Vector3f camDir = new Vector3f();
private Vector3f camLeft = new Vector3f();
public static void main(String[] args) {
Main app = new Main();
app.start();
}
boolean playerAttached = false;
boolean refreshSettings = true;
public void setPlayerAttached(boolean b) {
try {
if (b) {
bulletAppState.getPhysicsSpace().add(player);
} else {
bulletAppState.getPhysicsSpace().remove(player);
}
} catch (Exception e) {
}
}
public Node player_node;
public void initPlayer() {
Node n;
n = (Node)assetManager.loadModel("Models/player_marker.j3o");
n.setName("player_marker");
player_node = n;
CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(3f, 12f, 1);
player = new CharacterControl(capsuleShape, 0.05f);
player.setJumpSpeed(20);
player.setFallSpeed(30);
player.setGravity(30);
player.setPhysicsLocation(startLocation);
rootNode.attachChild(player_node);
cords = new BitmapText(guiFont, false);
cords.setSize(guiFont.getCharSet().getRenderedSize() * 2);
cords.setColor(ColorRGBA.White);
cords.setLocalTranslation(0f, settings.getHeight(), 0);
guiNode.attachChild(cords);
}
Vector3f startLocation = new Vector3f(0, 10, 0);
public void loadMap(String path) {
try {
rootNode.detachChildNamed("scene");
} catch (Exception e) {
}
try {
bulletAppState.getPhysicsSpace().remove(landscape);
} catch (Exception e) {
}
// We load the scene from the zip file and adjust its size.
sceneModel = assetManager.loadModel(path);
sceneModel.setLocalScale(2f);
sceneModel.setName("scene");
// We set up collision detection for the scene by creating a
// compound collision shape and a static RigidBodyControl with mass zero.
CollisionShape sceneShape =
CollisionShapeFactory.createMeshShape((Node) sceneModel);
landscape = new RigidBodyControl(sceneShape, 0);
sceneModel.addControl(landscape);
rootNode.attachChild(sceneModel);
bulletAppState.getPhysicsSpace().add(landscape);
try{
Node marker = ((Node)((Node)sceneModel).getChild("player_marker"));
startLocation = marker.getLocalTranslation();
((Node)sceneModel).detachChild(marker);
resetPlayer();
}catch(NullPointerException n){}
}
public void loadMap(Node model) {
try {
rootNode.detachChildNamed("scene");
} catch (Exception e) {
}
try {
bulletAppState.getPhysicsSpace().remove(landscape);
} catch (Exception e) {
}
// We load the scene from the zip file and adjust its size.
sceneModel = model;
sceneModel.setLocalScale(2f);
sceneModel.setName("scene");
// We set up collision detection for the scene by creating a
// compound collision shape and a static RigidBodyControl with mass zero.
CollisionShape sceneShape =
CollisionShapeFactory.createMeshShape((Node) sceneModel);
landscape = new RigidBodyControl(sceneShape, 0);
sceneModel.addControl(landscape);
rootNode.attachChild(sceneModel);
bulletAppState.getPhysicsSpace().add(landscape);
try{
Node marker = ((Node)((Node)sceneModel).getChild("player_marker"));
startLocation = marker.getLocalTranslation();
((Node)sceneModel).detachChild(marker);
resetPlayer();
}catch(NullPointerException n){}
}
public BitmapText cross;
public void initCrossHairs() {
setDisplayStatView(false);
guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
cross = new BitmapText(guiFont, false);
cross.setSize(guiFont.getCharSet().getRenderedSize() * 2);
cross.setText("."); // crosshairs
cross.setLocalTranslation( // center
settings.getWidth() / 2 - cross.getLineWidth() / 2, settings.getHeight() / 2 + cross.getLineHeight() / 2, 0);
guiNode.attachChild(cross);
}
public void changeCrossHairs(String s) {
cross.setText(s); // crosshairs
cross.setLocalTranslation( // center
settings.getWidth() / 2 - cross.getLineWidth() / 2, settings.getHeight() / 2 + cross.getLineHeight() / 2, 0);
}
public void simpleInitApp() {
initCrossHairs();
/**
* Set up Physics
*/
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
//bulletAppState.getPhysicsSpace().enableDebug(assetManager);
// We re-use the flyby camera for rotation, while positioning is handled by physics
viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));
flyCam.setMoveSpeed(100);
setUpKeys();
/**
* A white, directional light source
*/
DirectionalLight sun = new DirectionalLight();
sun.setDirection((new Vector3f(-0.5f, -0.5f, -0.5f)).normalizeLocal());
sun.setColor(ColorRGBA.White);
rootNode.addLight(sun);
this.loadMap("Scenes/Test Scene03.j3o");
this.initPlayer();
this.setPlayerAttached(true);
Node rifle = (Node) this.assetManager.loadModel("Models/Pistol_1911/held_1911.j3o");
r = new Rifle(this);
//rifle.setMaterial(new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md"));
System.out.println("Created Controller " + r.toString());
rifle.setName("rifle_node");
rifle.getChild(0).addControl(r);
rootNode.attachChild(rifle);
// We set up collision detection for the player by creating
// a capsule collision shape and a CharacterControl.
// The CharacterControl offers extra settings for
// size, stepheight, jumping, falling, and gravity.
// We also put the player in its starting position.
// We attach the scene and the player to the rootnode and the physics space,
// to make them appear in the game world.
}
Rifle r;
/**
* We over-write some navigational key mappings here, so we can add
* physics-controlled walking and jumping:
*/
private void setUpKeys() {
inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping("Settings", new KeyTrigger(KeyInput.KEY_E));
inputManager.addListener(this, "Left");
inputManager.addListener(this, "Right");
inputManager.addListener(this, "Up");
inputManager.addListener(this, "Down");
inputManager.addListener(this, "Jump");
inputManager.addListener(this, "Settings");
}
public void createBoxTarget(Vector3f pos) {
Geometry g = makeCube("cube", 6, 6, 6);
rootNode.attachChild(g);
System.out.println("Attached " + g.getName() + " at " + pos.toString());
g.setLocalTranslation(pos);
}
Geometry makeCube(String name, float x, float y, float z) {
Box box = new Box(1, 1, 1);
Geometry cube = new Geometry(name, box);
cube.setLocalTranslation(x, y, z);
Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat1.setColor("Color", ColorRGBA.randomColor());
cube.setMaterial(mat1);
return cube;
}
public Geometry createCylinder(float width, float height, ColorRGBA c) {
Cylinder cap = new Cylinder(30, 30, width, height, true);
Geometry g = new Geometry("cylinder", cap);
Quaternion roll90 = new Quaternion();
roll90.fromAngleAxis(FastMath.PI / 2, new Vector3f(1, 0, 0));
/* The rotation is applied: The object rolls by 180 degrees. */
g.setLocalRotation(roll90);
Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat1.setColor("Color", c);
g.setMaterial(mat1);
return g;
}
public Material makeMaterial(String name, ColorRGBA color) {
Material mat = new Material(assetManager, name);
mat.setColor("Color", color);
return mat;
}
public Geometry showVector3fArrow(Vector3f v, ColorRGBA color, String name) {
Arrow a = new Arrow(v);
Material mat = makeMaterial("Common/MatDefs/Misc/Unshaded.j3md", color);
Geometry geom = new Geometry(name, a);
geom.setMaterial(mat);
return geom;
}
BitmapText cords;
public void updateCords() {
cords.setText("X:" + player.getPhysicsLocation().getX() + " Y:" + player.getPhysicsLocation().getY() + " Z:" + player.getPhysicsLocation().getZ()); // crosshairs
}
@Override
public void stop() {
guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
BitmapText saving = new BitmapText(guiFont, false);
saving.setSize(guiFont.getCharSet().getRenderedSize() * 6);
saving.setText("Prepairing world for binary save"); // crosshairs
saving.setColor(ColorRGBA.Green);
saving.setLocalTranslation( // center
settings.getWidth() / 2 - saving.getLineWidth() / 2, settings.getHeight() / 2 + cross.getLineHeight() / 2, 0);
guiNode.attachChild(saving);
((Node) rootNode.getChild("rifle_node")).getChild(0).removeControl(r);
Node n;
n = (Node)assetManager.loadModel("Models/player_marker.j3o");
n.setName("player_marker");
Node c = new Node("Camera_Direction");
Vector3f dir = new Vector3f(cam.getDirection().getX() * 10, cam.getDirection().getY() * 10, cam.getDirection().getZ() * 10);
c.attachChild(showVector3fArrow(dir, ColorRGBA.Blue, "cam_direction"));
rootNode.attachChild(c);
rootNode.attachChild(n);
c.setLocalTranslation(cam.getLocation());
n.setLocalTranslation(new Vector3f(player.getPhysicsLocation().getX(), player.getPhysicsLocation().getY() - 2.9927845f, player.getPhysicsLocation().getZ()));
/**
* Save a Node to a .j3o file.
*/
BinaryExporter exporter = BinaryExporter.getInstance();
File file = new File("C:\\Users\\alans_000\\Documents\\NetBeansProjects\\Trench Warfair\\assets\\Scenes\\last_run.j3o");
try {
System.out.println("Saving run to file");
exporter.save(rootNode, file);
System.out.println("Run Saved");
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Failed to save node!", ex);
}
super.stop();
System.exit(0);
}
/**
* These are our custom actions triggered by key presses. We do not walk
* yet, we just keep track of the direction the user pressed.
*/
public void onAction(String binding, boolean isPressed, float tpf) {
if (binding.equals("Left")) {
left = isPressed;
} else if (binding.equals("Right")) {
right = isPressed;
} else if (binding.equals("Up")) {
up = isPressed;
} else if (binding.equalsIgnoreCase("Settings") && isPressed) {
Settings s = new Settings(this);
s.setLocationRelativeTo(null);
s.setVisible(true);
} else if (binding.equals("Down")) {
down = isPressed;
} else if (binding.equals("Jump")) {
if (isPressed) {
player.jump();
}
}
}
/**
* This is the main event loop--walking happens here. We check in which
* direction the player is walking by interpreting the camera direction
* forward (camDir) and to the side (camLeft). The setWalkDirection()
* command is what lets a physics-controlled player walk. We also make sure
* here that the camera moves with player.
*/
@Override
public void simpleUpdate(float tpf) {
camDir.set(cam.getDirection()).multLocal(0.6f);
camLeft.set(cam.getLeft()).multLocal(0.4f);
walkDirection.set(0, 0, 0);
if (left) {
walkDirection.addLocal(camLeft);
}
if (right) {
walkDirection.addLocal(camLeft.negate());
}
if (up) {
walkDirection.addLocal(new Vector3f(camDir.getX(), 0f, camDir.getZ()));
}
if (down) {
walkDirection.addLocal(new Vector3f(camDir.negate().getX(), 0f, camDir.negate().getZ()));
}
player.setWalkDirection(walkDirection);
cam.setLocation(player.getPhysicsLocation());
listener.setLocation(cam.getLocation());
listener.setRotation(cam.getRotation());
updateCords();
player_node.setLocalTranslation(new Vector3f(player.getPhysicsLocation().getX(), player.getPhysicsLocation().getY() - 2.9927845f, player.getPhysicsLocation().getZ()));
//System.out.println("Player:" + cam.getLocation());
if (refreshSettings) {
System.out.println("Refreshing settings");
try {
rootNode.detachChild(player_node);
guiNode.detachChild(cords);
if (this.playerAttached) {
rootNode.attachChild(player_node);
} else {
}
if (cordsAttached){
guiNode.attachChild(cords);
}
} catch (NullPointerException n) {
}
refreshSettings = false;
}
}
public boolean cordsAttached = true;
private void resetPlayer() {
player.setPhysicsLocation(this.startLocation);
}
}