Hello everyone.
I started learning jme a while ago and got to the physics chapter in my book.I did everything as the book said and my code doesn’t work.Here is the code and when i run it i get null pointer error.
public class PhysicsFallingBricks extends SimpleApplication {
public static void main(String[] args) {
PhysicsFallingBricks app = new PhysicsFallingBricks();
app.start();
}
private BulletAppState bulletAppState;
Material brickMat,stoneMat,woodMat;
private static Sphere ballMesh;
private static Box brickMesh;
private static Box floorMesh;
private static Node wallNode;
private RigidBodyControl brickPhy;
private RigidBodyControl ballPhy;
private RigidBodyControl floorPhy;
private static final float BRICK_LENGTH = 0.4f;
private static final float BRICK_WIDTH = 0.3f;
private static final float BRICK_HEIGHT = 0.25f;
private static final float WALL_WIDTH = 12;
private static final float WALL_HEIGHT = 6;
private static final String SHOOT = "shoot";
static{
floorMesh = new Box(Vector3f.ZERO, 10f, 0.5f, 5f);
brickMesh = new Box(Vector3f.ZERO, BRICK_LENGTH, BRICK_HEIGHT, BRICK_WIDTH);
ballMesh = new Sphere(32, 32, 0.25f, true, false);
ballMesh.setTextureMode(TextureMode.Projected);
floorMesh.scaleTextureCoordinates(new Vector2f(4f, 4f));
}
@Override
public void simpleInitApp() {
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
brickMat = assetManager.loadMaterial("Materials/brick.j3m");
stoneMat = assetManager.loadMaterial("Materials/pebbles.j3m");
woodMat = assetManager.loadMaterial("Materials/bark.j3m");
Geometry floorGeo = new Geometry("Floor", floorMesh);
floorGeo.setMaterial(woodMat);
floorGeo.move(0f, - BRICK_HEIGHT * 2f, 0f);
rootNode.attachChild(floorGeo);
floorPhy = new RigidBodyControl(0.0f);
floorGeo.addControl(floorPhy);
bulletAppState.getPhysicsSpace().add(floorPhy);
wallNode = new Node("wall");
float offsetH = BRICK_LENGTH / 3;
float offsetV = 0;
for(int j=0;j<WALL_HEIGHT;j++){
for(int i=0;i<WALL_WIDTH;i++){
Vector3f brickPos = new Vector3f(offsetH + BRICK_LENGTH * 2.1f * i -
(BRICK_LENGTH * WALL_WIDTH),
offsetV + BRICK_HEIGHT, 0f);
wallNode.attachChild(makeBrick(brickPos));
}
offsetH = -offsetH;
offsetV += 2 * BRICK_HEIGHT;
}
rootNode.attachChild(wallNode);
AmbientLight ambient = new AmbientLight();
rootNode.addLight(ambient);
DirectionalLight sun = new DirectionalLight();
sun.setDirection(new Vector3f(1.4f, -1.4f, -1.4f));
rootNode.addLight(sun);
inputManager.addMapping(SHOOT,new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(actionListener, SHOOT);
}
@Override
public void simpleUpdate(float tpf) {
//TODO: add update code
}
@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
public Geometry makeBrick(Vector3f loc) {
Geometry brickGeo = new Geometry("Brick", brickMesh);
brickGeo.setMaterial(brickMat);
wallNode.attachChild(brickGeo);
brickGeo.move(loc);
brickPhy = new RigidBodyControl(5f);
brickGeo.addControl(brickPhy);
bulletAppState.getPhysicsSpace().add(brickPhy);
return brickGeo;
}
public void shootCannonBall() {
Geometry ballGeo = new Geometry("cannon ball", ballMesh);
ballGeo.setMaterial(stoneMat);
ballGeo.setLocalTranslation(cam.getLocation());
rootNode.attachChild(ballGeo);
ballPhy = new RigidBodyControl(5f);
ballPhy.setCcdSweptSphereRadius(0.1f);
ballPhy.setCcdMotionThreshold(0.005f);
ballPhy.setLinearVelocity(cam.getDirection().mult(50f));
ballGeo.addControl(ballPhy);
bulletAppState.getPhysicsSpace().add(ballPhy);
}
private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean isPressed, float tpf) {
if (name.equals(SHOOT) && !isPressed) {
shootCannonBall();
}
}
};
}
I understand this code and i can’t figure out why its not working.
The error is in line 121
ballPhy.setLinearVelocity(cam.getDirection().mult(50f));
when i run it without it it creates cannonBalls() but they just lay there.
I don’t think the code is problem could it be something else?