Cannonball shooting

I used the code for shooting cannonballs from the Hello Physics tutorial. But I have a problem.
In some directions the cannonball shoots in the direction of the crosshairs and in other directions it does not shoot in the direction of the crosshairs. Does someone had the same problem and can help me fix it?


we need to see the code. Code is the issue for sure (i cant belive you have 1:1 from hello)

Best way is always to just provide TestCase github repo or something. (if its small enough, here on HUB are also code tags to provide code inside)

edit: and also you got JMETests where are shooting tests ready to run, so you can check them too.

1 Like

If the Crosshairs depend on the screen’s (settings) dimensions, you need to be sure that it was at the right place, and yeah show us the code and the testcase, so we can spot out the problem.

The wiki is out-of-date. Here’s the latest version of “”: jmonkeyengine/ at master · jMonkeyEngine/jmonkeyengine · GitHub

1 Like

This is part of the code like in the tutorial.

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. */

This is another piece of the code where i fire the cannonball.

private final ActionListener actionListener = new ActionListener() {
        public void onAction(String name, boolean keyPressed, float tpf) {
                if (name.equals("Shoot") && !keyPressed) {

This is the initKeys method for shooting the cannonball with the left mouse button.

private void initKeys() {
        inputManager.addMapping("Shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
        inputManager.addListener(actionListener, "Shoot");

In some directions it shoots the cannonballs exactly at the crosshairs. In other directions it does not, it shoots in another direction.
Is this enough code?

I don’t see any obvious bugs in the code you posted. To get good help, you may need to provide more information.

Do you have the code for the crosshairs ?

And btw, this shoots the ball in the direction of your cam, so i guess the crosshair is obviously positioned in a wrong direction…

public void initCrossHairs() {
        guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
        BitmapText ch = new BitmapText(guiFont, false);
        ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);
        ch.setText("+"); // crosshairs
        ch.setLocalTranslation( // center
          settings.getWidth() / 2 - ch.getLineWidth()/2,
          settings.getHeight() / 2 + ch.getLineHeight()/2, 0);

The cannonball sometimes shoots 180 degrees in the other direction in some directions. In some directions the cannonball shoots at the crosshairs and when I turn around about 180 degrees it shoots in another direction. It works in the JmeTest project so I don’t know what’s wrong with my code.

prepare small TestCase and put it into github.

in 99% cases like this, its user code issue(well, we still dont see all code, for example you could set other camera direction each frame for a moment that make issue).

And if its 1%, then we will be able to replicate just by run TestCase.

Just a guess; does your player have their own physics shape. Could the cannon ball be fired in the right direction then bounce off of some invisible physics object

1 Like