EDIT: I just realized this shouldn’t have been posted in this section of the forum, sorry about that.
I’m using code from one of the tutorials on collisions and physics. What I do is I have a method to initialize the scene and one for updating it in my main class. I call these methods from a control class for nifty. The initialize part works just fine, however the update part returns a null pointer. Here is my code.
GuiGameBase.java
[java]
public class GuiGameBase extends SimpleApplication implements ActionListener,ScreenController{
…//unrelated code
public void gameStartSettings(){
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
// 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();
setUpLight();
// Load scene from zip file.
assetManager.registerLocator(“town.zip”, ZipLocator.class.getName());
sceneModel = assetManager.loadModel(“main.scene”);
sceneModel.setLocalScale(2f);
// Create compound collision shape for scene.
CollisionShape sceneShape =
CollisionShapeFactory.createMeshShape((Node) sceneModel);
landscape = new RigidBodyControl(sceneShape, 0);
sceneModel.addControl(landscape);
//Set up crosshairs
initCrossHairs();
// Set character collision and physics.
CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
player = new CharacterControl(capsuleShape, 0.05f);
player.setJumpSpeed(charjump);
player.setFallSpeed(100);
player.setGravity(80);
player.setPhysicsLocation(new Vector3f(0, 10, 0));
// Attach scene and player to root node and physics space.
rootNode.attachChild(sceneModel);
bulletAppState.getPhysicsSpace().add(landscape);
bulletAppState.getPhysicsSpace().add(player);
}
public void onGameUpdate(){
Vector3f camDir = cam.getDirection().clone().multLocal(charspeed); // <
Null Pointer Exception
Vector3f camLeft = cam.getLeft().clone().multLocal(charspeed);
walkDirection.set(0, 0, 0);
if (left) { walkDirection.addLocal(camLeft); }
if (right) { walkDirection.addLocal(camLeft.negate()); }
if (up) { walkDirection.addLocal(camDir); }
if (down) { walkDirection.addLocal(camDir.negate()); }
player.setWalkDirection(walkDirection);
cam.setLocation(player.getPhysicsLocation());
}
......//More unrelated code.
[/java]
Now, I call these in the update of my other class. HudScreen.java
[java]
package gamerScreen;
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.input.FlyByCamera;
import de.lessvoid.nifty.Nifty;
import com.jme3.renderer.Camera;
import com.jme3.font.BitmapFont;
import com.jme3.input.InputManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.system.AppSettings;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
public class HudScreen extends AbstractAppState implements ScreenController {
private Nifty nifty;
private GuiGameBase base = new GuiGameBase().getInstance();
private Screen screen;
private AssetManager assetManager;
private FlyByCamera flyCam;
private Node rootNode;
private Node guiNode;
private ViewPort guiViewPort;
private ViewPort viewPort;
private AppSettings settings;
private AppStateManager stateManager;
private BitmapFont guiFont;
private InputManager inputManager;
private boolean gameStart = false;
private boolean gameUpdate = false;
private SimpleApplication app;
/** Nifty GUI ScreenControl methods */
public void bind(Nifty nifty, Screen screen) {
this.nifty = nifty;
this.screen = screen;
}
public void onStartScreen() {
checkScreen();
}
public void onEndScreen() { }
public void startGame(){
}
public void changeScreen(String nextScreen){
nifty.gotoScreen(nextScreen);
}
public void gameExit(){
nifty.exit();
System.exit(0);
}
/** jME3 AppState methods */
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
this.app=(SimpleApplication)app;
assetManager = app.getAssetManager();
guiViewPort = app.getGuiViewPort();
}
public void checkScreen(){
if(nifty.getCurrentScreen() == screen){
gameStart = true;
}
}
@Override
public void update(float tpf) {
/** jME update loop! */
if(gameStart){
guiNode = app.getGuiNode();
rootNode = app.getRootNode();
flyCam = app.getFlyByCamera();
flyCam.setEnabled(true);
flyCam.setDragToRotate(false);
base.gameStartSettings();
gameStart = false;
gameUpdate = true;
}
if(gameUpdate){
base.onGameUpdate();;
}
}
}[/java]
The problem is probably something simple, but after two hours of searching around I figured I'd finally come to you guys. If I call just base.gameStartSettings() the scene starts fine, but I have no weight/gravity or collision box. So then I have to call base.onGameUpdate() and I get a nullpointer exception. (and charspeed is set);