Trouble with simplest linear velocity code

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?

Hahahah it took me 2 days to give up and put this online to ask for help, and it took me 2 days and 40 min to find what is the problem.Apparently the code is problem.
This lines of code in shootCannonBall method
ballPhy.setCcdSweptSphereRadius(0.1f);
ballPhy.setCcdMotionThreshold(0.005f);
ballPhy.setLinearVelocity(camDir.mult(50f));
i moved after
ballGeo.addControl(ballPhy);
bulletAppState.getPhysicsSpace().add(ballPhy);

and it works just fine.
I hope this helps someone one day.
P.S. don’t always blindly trust books! :smiley:

1 Like

Hi

It would have been easier for us to help you with the full stack trace. Moreover, “null pointer error” doesn’t exist in Java, you should have written “NullPointerException”. Which book do you use?

I am sorry for what i wrote but all here know what i meant.
For me error is when something doesn’t work and it should work.
I am using “jMonkeyEngine 3.0 Beginner’s Guide” by Ruth Kusterer packt publishing…
and nowhere in the book says this needs to be done before the other thing, or else it won’t work XDDD

Yes, but in the future… an exception without a stack trace is missing 90% of the important information. So you’ll get better help if you always post the stack trace for exceptions… as not doing it is almost the same as not mentioning the exception at all.

Sorry, this is my first post here…ever :slight_smile: thank you for the tips.
I am learning jmonkey for 3weeks now and i am glad there are people like you who are willing to help.