Time for action – flying cannon balls

Let's keep the default navigational inputs in our PhysicsFallingBricks.java, and simply add a shoot action. Every time the user clicks, you want to shoot a cannon ball from the camera position forward.

  1. Write a shootCannonBall() method that creates a new physical geometry ballGeo from our ballMesh shape and stoneMat material. The setLinearVelocity() line defines the initial speed and direction of the object. In this case, we accelerate the cannon ball forward.
     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(.1f); ...

Get jMonkeyEngine 3.0 Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.