The balls don't remove the boxes

Hi, i have problem with collision boxes and balls. Balls don’t remove boxes. Don’t count the points. When i remove balls in code everything works good. Probably, the mistake is in collisions check. I don’t know how it describe more precisly.
Please help.

public class Shoot_main extends SimpleApplication {

private BulletAppState bulletAppState;
private PointLight light = new PointLight();
private ArrayList<BoxGuy> list = new ArrayList<BoxGuy>();
private boolean shooting, spawning;
Material matBullet;
float bulletSize = 0.5f;
private HUD hud;
private Sphere bullet;
private SphereCollisionShape bulletCollisionShape;
private Spatial sceneModel;
private RigidBodyControl landscape;
private CharacterControl player;
private Vector3f walkDirection = new Vector3f();
private Vector3f camDir = new Vector3f();
private Vector3f camLeft = new Vector3f();
private AudioNode audio_gun, audio_nature;
private boolean left, right, up, down = false;

public static void main(String args[]) {
    Shoot_main stage = new Shoot_main();
    stage.start();
    stage.setPauseOnLostFocus(false);
}
public int gameState = 0;
public Nifty nifty;
private FogFilter fog;

public void simpleInitApp() {
  loadStartScreen();
    initCrossHairs();
    initMaterial();
    initKeys();
    initAudio();
    setupLight();
    initFog();

    setDisplayFps(true);
    setDisplayStatView(false);

    bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);
    bullet = new Sphere(32, 32, 1.0f, true, false);
    bullet.setTextureMode(Sphere.TextureMode.Projected);
    bulletCollisionShape = new SphereCollisionShape(1.0f);

    Physics.createPhysicsTestWorld(rootNode, assetManager, bulletAppState.getPhysicsSpace());
    speed = 1.3f;
    flyCam.setMoveSpeed(50);

    viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));
    hud = new HUD(assetManager, guiNode, settings.getWidth(), settings.getHeight());
           hud.reset();

    cam.setLocation(new Vector3f(0.26924422f, 6.646658f, 22.265987f));
    cam.setRotation(new Quaternion(-2.302544E-4f, 0.99302495f, -0.117888905f, -0.0019395084f));

    for (int i = 0; i < 5; i++) {
        list.add(new BoxGuy(this, list.size()));
    }

    assetManager.registerLocator("town.zip", ZipLocator.class);
    sceneModel = assetManager.loadModel("main.scene");
    sceneModel.setLocalScale(2f);

    CollisionShape sceneShape = CollisionShapeFactory.createMeshShape((Node) sceneModel);
    landscape = new RigidBodyControl(sceneShape, 0);
    sceneModel.addControl(landscape);

    CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(2.0f, 6f, 1);
    player = new CharacterControl(capsuleShape, 0.05f);
    player.setJumpSpeed(20);
    player.setFallSpeed(30);
    player.setGravity(30);
    player.setPhysicsLocation(new Vector3f(0, 10, 0));

    rootNode.attachChild(sceneModel);
    bulletAppState.getPhysicsSpace().add(landscape);
    bulletAppState.getPhysicsSpace().add(player);


}

public void hideMouse() {
    mouseInput.setCursorVisible(false);
}

public void initKeys() {
    getInputManager().addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
    getInputManager().addListener(actionListener, "shoot");
    getInputManager().addMapping("spawn", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
    getInputManager().addListener(actionListener, "spawn");
    getInputManager().addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
    getInputManager().addListener(actionListener, "Left");
    getInputManager().addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
    getInputManager().addListener(actionListener, "Right");
    getInputManager().addMapping("Up", new KeyTrigger(KeyInput.KEY_W));
    getInputManager().addListener(actionListener, "Up");
    getInputManager().addMapping("Down", new KeyTrigger(KeyInput.KEY_S));
    getInputManager().addListener(actionListener, "Down");
    getInputManager().addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
    getInputManager().addListener(actionListener, "Jump");
}
private ActionListener actionListener = new ActionListener() {
    public void onAction(String name, boolean keyPressed, float tpf) {
        if (name.equals("shoot")) {
            Geometry bulletg = new Geometry("bullet", bullet);
                bulletg.setMaterial(matBullet);
                Vector3f camLocation = getCamera().getLocation();
                bulletg.setLocalTranslation(getCamera().getDirection().scaleAdd(3, camLocation));
                bulletg.setLocalTranslation(getCamera().getDirection().scaleAdd(3, camLocation));
                bulletg.setLocalScale(bulletSize);
                bulletCollisionShape = new SphereCollisionShape(bulletSize);
                RigidBodyControl bulletNode = new RigidBodyControl(bulletCollisionShape, bulletSize * 10);
                bulletNode.setCcdMotionThreshold(0.001f);
                bulletNode.setLinearVelocity(cam.getDirection().mult(40));
                bulletg.addControl(bulletNode);
                rootNode.attachChild(bulletg);
            getPhysicsSpace().add(bulletNode); 
            hud.updateHUD();
        }
        if (name.equals("spawn") && keyPressed) {
            spawning = true;
        } else if (name.equals("Left")) {
            left = keyPressed;
        } else if (name.equals("Right")) {
            right = keyPressed;
        } else if (name.equals("Up")) {
            up = keyPressed;
        } else if (name.equals("Down")) {
            down = keyPressed;
        } else if (name.equals("Jump")) {
            if (keyPressed) {
                player.jump();
            }
        }
    }
};

@Override
public void simpleUpdate(float tpf) {
    camDir.set(cam.getDirection()).multLocal(0.4f);
    camLeft.set(cam.getLeft()).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());
    }

    player.setWalkDirection(walkDirection);
    cam.setLocation(player.getPhysicsLocation());
    listener.setLocation(cam.getLocation());
    listener.setRotation(cam.getRotation());
    light.setPosition(cam.getLocation());

    for (BoxGuy b : list) {
        b.simpleUpdate(tpf);
    }
    if (shooting) {
        CollisionResults results = new CollisionResults();
        Ray ray = new Ray(getCamera().getLocation(), getCamera().getDirection());
        getRootNode().collideWith(ray, results);
        for (int i = 0; i < results.size(); i++) {
            try {
                list.get(Integer.parseInt(results.getCollision(i).getGeometry().getName())).hurt();
                fpsText.setText("You hit the BOX!");
                hud.score = hud.score + 1;
                //          System.out.println("Your's score: "+hud.score);
            } catch (Exception e) {
            }
        }
    }
    if (spawning) {
        for (int i = 0; i <= 5; i++) {
            list.add(new BoxGuy(this, list.size()));
            spawning = false;
        }
    }

}

public BulletAppState getBulletAppState() {
    return bulletAppState;
}

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

public void initMaterial() {
    Texture west = assetManager.loadTexture("Textures/Sky/west.jpg");
    Texture east = assetManager.loadTexture("Textures/Sky/east.jpg");
    Texture north = assetManager.loadTexture("Textures/Sky/north.jpg");
    Texture south = assetManager.loadTexture("Textures/Sky/south.jpg");
    Texture up_sky = assetManager.loadTexture("Textures/Sky/up.jpg");
    Texture down_sky = assetManager.loadTexture("Textures/Sky/down.jpg");

    matBullet = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key2 = new TextureKey("Textures/rock.png");
    key2.setGenerateMips(true);
    Texture tex2 = assetManager.loadTexture(key2);
    matBullet.setTexture("ColorMap", tex2);

    Spatial sky = SkyFactory.createSky(assetManager, west, east, north, south, up_sky, down_sky);
    rootNode.attachChild(sky);
}

private void setupLight() {
    AmbientLight al = new AmbientLight();
    al.setColor(ColorRGBA.White.mult(0.3f));
    rootNode.addLight(al);

    DirectionalLight dl = new DirectionalLight();
    dl.setColor(ColorRGBA.White);
    dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());
    rootNode.addLight(dl);
}

public void initAudio() {
    audio_gun = new AudioNode(assetManager, "Sounds/Gun.wav", false);
    audio_gun.setPositional(false);
    audio_gun.setLooping(false);
    audio_gun.setVolume(6);
    rootNode.attachChild(audio_gun);

    audio_nature = new AudioNode(assetManager, "Sounds/birdssss.wav", true);
    audio_nature.setLooping(true);
    audio_nature.setPositional(true);
    audio_nature.setVolume(20);
    rootNode.attachChild(audio_nature);
    audio_nature.play();
}

private PhysicsSpace getPhysicsSpace() {
return bulletAppState.getPhysicsSpace();
}
}

https://www.mikeash.com/getting_answers.html

I don’t know how it describe more precisly. When ball collision with box nothing happend. When I remove ball from source code everything works good.

It would be helpful if you could give us more information about what you want your code to do. Going through your code and your original post, it seems as if you want to shoot boxes with balls and have the boxes removed from the scene and points added to a score?

I do, however, have a couple of observations (assuming I’m correct about the shooting bit):

  1. Your ray collision check needs to have the “shooting” boolean set true, but I don’t see anywhere in your code where that variable is set.

  2. Are the bullets just for effects, or do you want the physical bullet impact to determine whether a box is hit or not? If bullets are just for effects, your ray cast is almost ok (see #3). If where the bullets hit is to determine if a hit happened or not, do not use a ray cast. Use physics collision listeners instead.

  3. Your ray cast does not check what got hit. When you cast the ray, the ray could collide with many things, not just boxes. It would be better to explicitly check what you hit than to hope it’s a box and swallow an exception if it’s not.

  4. If you need the ray cast to determine hits, it would be better to put it in the ActionListener than in the update. You probably only want to shoot when the user presses the shoot button, not every frame. If you do this, you also don’t need the “shooting” boolean.

To me, it seems like this stuff is covered in the tutorials which a link was provided in the other thread essentially about the same issue.

You mean this one? :wink: