Combining HelloCollision and HelloPhysics: Cannonball doesn't work

I’ve worked through the basic tutorials. To get more familiar with the SDK, I’m just experimenting in various ways. One thing I tried was to implement the cannon from the HelloPhysics tutorial into the HelloCollision tutorial example. It’s compiling and running, but the behavior is weird. The cannonball only seems to be correctly fired in about a 30 degree arc. I’ve been messing with it for the past 24 hours and can’t understand at all why the cannon doesn’t behave the same when the relevant logic is moved to HelloCollision. Help? My new class with the cannon code added is below.


  • 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
    public class HelloCollision extends SimpleApplication
    implements ActionListener {

private Spatial sceneModel;
private BulletAppState bulletAppState;
private RigidBodyControl landscape;
private CharacterControl player;
private Vector3f walkDirection = new Vector3f();
private boolean left = false, right = false, up = false, down = false;
Material stone_mat;
private RigidBodyControl ball_phy;
private static final Sphere sphere;

static {
/** Initialize the cannon ball geometry */
sphere = new Sphere(32, 32, 0.4f, true, false);

public static void main(String[] args) {
HelloCollision app = new HelloCollision();

public void simpleInitApp() {
/** Set up Physics */
bulletAppState = new BulletAppState();

// We re-use the flyby camera for rotation, while positioning is handled by physics
viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));

// We load the scene from the zip file and adjust its size.
assetManager.registerLocator("", ZipLocator.class);
sceneModel = assetManager.loadModel("main.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);

// 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.
CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
player = new CharacterControl(capsuleShape, 0.05f);
player.setPhysicsLocation(new Vector3f(0, 10, 0));

// We attach the scene and the player to the rootNode and the physics space,
// to make them appear in the game world.


private void setUpLight() {
// We add light so we see the scene
AmbientLight al = new AmbientLight();

DirectionalLight dl = new DirectionalLight();
dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());


/** 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));
    new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
    inputManager.addListener(this, “Left”);
    inputManager.addListener(this, “Right”);
    inputManager.addListener(this, “Up”);
    inputManager.addListener(this, “Down”);
    inputManager.addListener(this, “Jump”);
    inputManager.addListener(this, “shoot”);

/** 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 value, float tpf) {
    if (binding.equals(“Left”)) {
    left = value;
    } else if (binding.equals(“Right”)) {
    right = value;
    } else if (binding.equals(“Up”)) {
    up = value;
    } else if (binding.equals(“Down”)) {
    down = value;
    } else if (binding.equals(“Jump”)) {
    } else if (binding.equals(“shoot”) && !value) {


  • 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.
    public void simpleUpdate(float tpf) {
    Vector3f camDir = cam.getDirection().clone().multLocal(0.6f);
    Vector3f camLeft = cam.getLeft().clone().multLocal(0.4f);
    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()); }
/** This method creates one individual physical cannon ball.
  • By defaul, the ball is accelerated and flies

  • from the camera position in the camera direction.*/
    public void makeCannonBall() {
    /** Create a cannon ball geometry and attach to scene graph. /
    Geometry ball_geo = new Geometry(“cannon ball”, sphere);
    * Position the cannon ball /
    * Make the ball physcial with a mass > 0.0f /
    ball_phy = new RigidBodyControl(1f);
    * Add physical ball to physics space. /
    * Accelerate the physcial ball to shoot it. */

    /** Initialize the materials used in this scene. */
    public void initMaterials() {
    stone_mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
    TextureKey key2 = new TextureKey(“Textures/Terrain/Rock/Rock.PNG”);
    Texture tex2 = assetManager.loadTexture(key2);
    stone_mat.setTexture(“ColorMap”, tex2);

/** A plus sign used as crosshairs to help the player with aiming.*/
protected void initCrossHairs() {
guiFont = assetManager.loadFont(“Interface/Fonts/Default.fnt”);
BitmapText ch = new BitmapText(guiFont, false);
ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);
ch.setText("+"); // fake crosshairs
ch.setLocalTranslation( // center
settings.getWidth() / 2 - guiFont.getCharSet().getRenderedSize() / 3 * 2,
settings.getHeight() / 2 + ch.getLineHeight() / 2, 0);



Hi Polyclef.

I’ve read your code (not so easy without the code tags) and I think it should work. Maybe the problem comes from having the ball set at the same place as your Player character. Then it may imply a collision even before launching the ball.

If you detected collisions for your ball to explode, I think it would explode right at the launch.

Thus, I suggest you create the ball just a little farther from you player, instead of at the camera center.

I’ve not tested it… just an idea.

Sorry about the formatting. I updated it (a little late now I guess).

Anyway, yes, your suggestion seemed to bear out. I set the initial ball location slightly in the direction of the camera and it works fine. Thanks!

I guess the difference between the examples is the presence of the Player? And as you say the location of the Player and the initial location of the ball at at the same point, causing a collision whenever each ball is generated?

Another quick question. Now that it’s working, the linear velocity of the ball is propelling it in the direction of the camera, but it is now also slightly nudging the Player in the opposite direction. My initial instinct to prevent this behavior is to increase the mass of the player, though I don’t see an obvious way to do this. Is this a good approach…if not, what would be?

Most likely you are still colliding slightly which is pushing the player back. Move the canonball further :slight_smile:

Yep, you are correct. increasing the spawn distance from the player solved the pushback issue. Thanks. :slight_smile: