Make an object disappear on collision

How would I make an object disappear on collision, like in the hello physics tutorial, after the cannonball hits a wall or the ground make it disappear.

Just detach the spatial and remove the physics control from the physics space.

I’m sorry Normen, I’m not understanding that. Just started with jMonkey this week and only kinda grasp the tutorials.

if u just started this week, keep reading them :slight_smile:

essentially its:
[java]spatial.removeFromParent()
physicsSpace.remove (spatial)[/java]

Still not able to grasp.
Where would this be implemented?
Here is my code:

import com.jme3.app.SimpleApplication;
import com.jme3.asset.TextureKey;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.font.BitmapText;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Dome;
import com.jme3.scene.shape.Sphere;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture.WrapMode;

public class Main extends SimpleApplication {

public static void main(String[] args) {
    Main app = new Main();
    app.start();
}
private BulletAppState bulletAppState;

Material floor_mat;
Material ceiling_mat;
Material left_mat;
Material right_mat;
Material back_mat;
Material front_mat;
Material floor_mat2;
Material ceiling_mat2;
Material left_mat2;
Material right_mat2;
Material floor_mat3;
Material ceiling_mat3;
Material left_mat3;
Material right_mat3;
Material floor_mat4;
Material ceiling_mat4;
Material left_mat4;
Material right_mat4;
    
private RigidBodyControl    floor_phy;
private static final Box    floor;
private RigidBodyControl    ceiling_phy;
private static final Box    ceiling;
private RigidBodyControl    left_phy;
private static final Box    left;
private RigidBodyControl    right_phy;
private static final Box    right;
private RigidBodyControl    back_phy;
private static final Box    back;
private static final Box    front;
private RigidBodyControl    floor_phy2;
private static final Box    floor2;
private RigidBodyControl    ceiling_phy2;
private static final Box    ceiling2;
private RigidBodyControl    left_phy2;
private static final Box    left2;
private RigidBodyControl    right_phy2;
private static final Box    right2;
private RigidBodyControl    floor_phy3;
private static final Box    floor3;
private RigidBodyControl    ceiling_phy3;
private static final Box    ceiling3;
private RigidBodyControl    left_phy3;
private static final Box    left3;
private RigidBodyControl    right_phy3;
private static final Box    right3;
private RigidBodyControl    floor_phy4;
private static final Box    floor4;
private RigidBodyControl    ceiling_phy4;
private static final Box    ceiling4;
private RigidBodyControl    left_phy4;
private static final Box    left4;
private RigidBodyControl    right_phy4;
private static final Box    right4;

static {
/** Initialize the floor geometry */
floor = new Box(Vector3f.ZERO, 6f, 0.1f, 6f);
floor.scaleTextureCoordinates(new Vector2f(3, 6));
/** Initialize the ceiling geometry */
ceiling = new Box(Vector3f.ZERO, 6f, 0.1f, 6f);
ceiling.scaleTextureCoordinates(new Vector2f(3, 6));
/** Initialize the left geometry */
left = new Box(Vector3f.ZERO, 0.1f, 6f, 6f);
left.scaleTextureCoordinates(new Vector2f(3, 6));
/** Initialize the right geometry */
right = new Box(Vector3f.ZERO, 0.1f, 6f, 6f);
right.scaleTextureCoordinates(new Vector2f(3, 6));
/** Initialize the back geometry */
back = new Box(Vector3f.ZERO, 6f, 6f, 0.1f);
back.scaleTextureCoordinates(new Vector2f(3, 6));
/** Initialize the front geometry */
front = new Box(Vector3f.ZERO, 6f, 6f, 0.1f);
front.scaleTextureCoordinates(new Vector2f(3, 6));
/** Initialize the floor2 geometry */
floor2 = new Box(Vector3f.ZERO, 6f, 0.1f, 6f);
floor2.scaleTextureCoordinates(new Vector2f(3, 6));
/** Initialize the ceiling2 geometry */
ceiling2 = new Box(Vector3f.ZERO, 6f, 0.1f, 6f);
ceiling2.scaleTextureCoordinates(new Vector2f(3, 6));
/** Initialize the left2 geometry */
left2 = new Box(Vector3f.ZERO, 0.1f, 6f, 6f);
left2.scaleTextureCoordinates(new Vector2f(3, 6));
/** Initialize the right2 geometry */
right2 = new Box(Vector3f.ZERO, 0.1f, 6f, 6f);
right2.scaleTextureCoordinates(new Vector2f(3, 6));
/** Initialize the floor3 geometry */
floor3 = new Box(Vector3f.ZERO, 6f, 0.1f, 6f);
floor3.scaleTextureCoordinates(new Vector2f(3, 6));
/** Initialize the ceiling3 geometry */
ceiling3 = new Box(Vector3f.ZERO, 6f, 0.1f, 6f);
ceiling3.scaleTextureCoordinates(new Vector2f(3, 6));
/** Initialize the left3 geometry */
left3 = new Box(Vector3f.ZERO, 0.1f, 6f, 6f);
left3.scaleTextureCoordinates(new Vector2f(3, 6));
/** Initialize the right3 geometry */
right3 = new Box(Vector3f.ZERO, 0.1f, 6f, 6f);
right3.scaleTextureCoordinates(new Vector2f(3, 6));
/** Initialize the floor4 geometry */
floor4 = new Box(Vector3f.ZERO, 6f, 0.1f, 6f);
floor4.scaleTextureCoordinates(new Vector2f(3, 6));
/** Initialize the ceiling4 geometry */
ceiling4 = new Box(Vector3f.ZERO, 6f, 0.1f, 6f);
ceiling4.scaleTextureCoordinates(new Vector2f(3, 6));
/** Initialize the left4 geometry */
left4 = new Box(Vector3f.ZERO, 0.1f, 6f, 6f);
left4.scaleTextureCoordinates(new Vector2f(3, 6));
/** Initialize the right4 geometry */
right4 = new Box(Vector3f.ZERO, 0.1f, 6f, 6f);
right4.scaleTextureCoordinates(new Vector2f(3, 6));
   
}

@Override
public void simpleInitApp() {
    
    /** Set up Physics */
    bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);
    bulletAppState.getPhysicsSpace().enableDebug(assetManager);
    bulletAppState.getPhysicsSpace().setGravity(Vector3f.ZERO);
    
    viewPort.setBackgroundColor(ColorRGBA.Black); // set up background color

    /** Add InputManager action: Left click triggers shooting. */
    inputManager.addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
    inputManager.addListener(actionListener, "shoot");
    

    /** Initialize the scene, materials, and physics space */
    initMaterials();
    initFloor();
    initCeiling();
    initLeft();
    initRight();
    initBack();
    initFloor2();
    initCeiling2();
    initLeft2();
    initRight2();
    initFloor3();
    initCeiling3();
    initLeft3();
    initRight3();
    initFloor4();
    initCeiling4();
    initLeft4();
    initRight4();
    
    initCrossHairs();
    }

    /**
    * Every time the shoot action is triggered, a new ball is produced.
    * The ball is set up to fly from the camera position in the camera direction.
    */
    private ActionListener actionListener = new ActionListener() {
        @Override
        public void onAction(String name, boolean keyPressed, float tpf) {
        if (name.equals("shoot") && !keyPressed) {
        makeBall();
  }
}

};

/** Initialize the materials used in this scene. */
public void initMaterials() {
    
        Sphere sphereTarget = new Sphere(64, 64, 1f);
        Geometry Target1 = new Geometry("SphereTarget", sphereTarget);
        Material sphereTarget1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");  // create a simple material
        sphereTarget1.setColor("Color", ColorRGBA.Pink);   // set color of material to yellow
        Target1.setMaterial(sphereTarget1);                   // set the ball's material
        Target1.setLocalTranslation(new Vector3f(0f, 4f, -36f)); 
        Target1.rotate(2f, 3f, 2f);  // Rotate it a bit
        RigidBodyControl Target1_phy1 = new RigidBodyControl(1f); // Make the ball physcial with a mass > 0.0f 
        Target1.addControl(Target1_phy1); // Add physical ball to physics space
        bulletAppState.getPhysicsSpace().add(Target1_phy1); 
        Target1_phy1.applyCentralForce(new Vector3f(4f, 3f, 8f)); 
        Target1_phy1.setMass(1f); 
        Target1_phy1.setKinematic(false);
        rootNode.attachChild(Target1);

        Sphere sphereTargetBig = new Sphere(64, 64, 1.5f);
        Geometry Target2 = new Geometry("SphereTargetBig", sphereTargetBig);
        Material sphereTarget2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");  // create a simple material
        sphereTarget2.setColor("Color", ColorRGBA.Green);   // set color of material to green
        Target2.setMaterial(sphereTarget2);                   // set the ball's material
        Target2.setLocalTranslation(new Vector3f(4f, 4f, -16f)); 
        Target2.rotate(2f, 3f, 2f);  // Rotate it a bit
        RigidBodyControl Target2_phy1 = new RigidBodyControl(1f); // Make the ball physcial with a mass > 0.0f 
        Target2.addControl(Target2_phy1); // Add physical ball to physics space
        bulletAppState.getPhysicsSpace().add(Target2_phy1); 
        Target2_phy1.applyCentralForce(new Vector3f(4f, 3f, 8f)); 
        Target2_phy1.setMass(1f); 
        Target2_phy1.setKinematic(false);
        rootNode.attachChild(Target2);
        
        Box cubeTarget = new Box(Vector3f.ZERO, 1f, 1f, 1f);
        Geometry Target3 = new Geometry("CubeTarget", cubeTarget);
        Material cubeTarget1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");  // create a simple material
        cubeTarget1.setColor("Color", ColorRGBA.Blue);   // set color of material to blue
        Target3.setMaterial(cubeTarget1);                   // set the cube's material
        Target3.setLocalTranslation(new Vector3f(2f, 2f, -26f)); 
        Target3.rotate(2f, 3f, 2f);  // Rotate it a bit
        RigidBodyControl Target3_phy1 = new RigidBodyControl(1f); // Make the cube physcial with a mass > 0.0f 
        Target3.addControl(Target3_phy1); // Add physical cube to physics space
        bulletAppState.getPhysicsSpace().add(Target3_phy1); 
        Target3_phy1.applyCentralForce(new Vector3f(4f, 3f, 8f)); 
        Target3_phy1.setMass(1f); 
        Target3_phy1.setKinematic(false);
        rootNode.attachChild(Target3);
        
        Box cubeTargetBig = new Box(Vector3f.ZERO, 1f, 1f, 1f);
        Geometry Target4 = new Geometry("CubeTargetBig", cubeTargetBig);
        Material cubeTarget2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");  // create a simple material
        cubeTarget2.setColor("Color", ColorRGBA.Orange);   // set color of material to orange
        Target4.setMaterial(cubeTarget2);                   // set the cube's material
        Target4.setLocalTranslation(new Vector3f(2f, 2f, -26f)); 
        Target4.rotate(2f, 3f, 2f);  // Rotate it a bit
        RigidBodyControl Target4_phy1 = new RigidBodyControl(1f); // Make the cube physcial with a mass > 0.0f 
        Target4.addControl(Target4_phy1); // Add physical cube to physics space
        bulletAppState.getPhysicsSpace().add(Target4_phy1); 
        Target4_phy1.applyCentralForce(new Vector3f(4f, 3f, 8f)); 
        Target4_phy1.setMass(1f); 
        Target4_phy1.setKinematic(false);
        rootNode.attachChild(Target4);
        
    
    floor_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key1 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
    key1.setGenerateMips(true);
    Texture tex1 = assetManager.loadTexture(key1);
    tex1.setWrap(WrapMode.Repeat);
    floor_mat.setTexture("ColorMap", tex1);

    ceiling_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
    key2.setGenerateMips(true);
    Texture tex2 = assetManager.loadTexture(key2);
    tex2.setWrap(WrapMode.Repeat);
    ceiling_mat.setTexture("ColorMap", tex2);
    
    left_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key3 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
    key3.setGenerateMips(true);
    Texture tex3 = assetManager.loadTexture(key3);
    tex3.setWrap(WrapMode.Repeat);
    left_mat.setTexture("ColorMap", tex3);
    
    right_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key4 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
    key4.setGenerateMips(true);
    Texture tex4 = assetManager.loadTexture(key4);
    tex4.setWrap(WrapMode.Repeat);
    right_mat.setTexture("ColorMap", tex4);
    
    back_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key5 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
    key5.setGenerateMips(true);
    Texture tex5 = assetManager.loadTexture(key5);
    tex5.setWrap(WrapMode.Repeat);
    back_mat.setTexture("ColorMap", tex5);
    
    
    //cube 2
    
    floor_mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key12 = new TextureKey("Textures/Terrain/BrickWall/BrickWall.jpg");
    key12.setGenerateMips(true);
    Texture tex12 = assetManager.loadTexture(key12);
    tex12.setWrap(WrapMode.Repeat);
    floor_mat2.setTexture("ColorMap", tex12);

    ceiling_mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key22 = new TextureKey("Textures/Terrain/BrickWall/BrickWall.jpg");
    key22.setGenerateMips(true);
    Texture tex22 = assetManager.loadTexture(key22);
    tex22.setWrap(WrapMode.Repeat);
    ceiling_mat2.setTexture("ColorMap", tex22);
    
    left_mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key32 = new TextureKey("Textures/Terrain/BrickWall/BrickWall.jpg");
    key32.setGenerateMips(true);
    Texture tex32 = assetManager.loadTexture(key32);
    tex32.setWrap(WrapMode.Repeat);
    left_mat2.setTexture("ColorMap", tex32);
    
    right_mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key42 = new TextureKey("Textures/Terrain/BrickWall/BrickWall.jpg");
    key42.setGenerateMips(true);
    Texture tex42 = assetManager.loadTexture(key42);
    tex42.setWrap(WrapMode.Repeat);
    right_mat2.setTexture("ColorMap", tex42);
    
    //cube 3
    
    floor_mat3 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key13 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
    key13.setGenerateMips(true);
    Texture tex13= assetManager.loadTexture(key13);
    tex13.setWrap(WrapMode.Repeat);
    floor_mat3.setTexture("ColorMap", tex13);

    ceiling_mat3 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key23 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
    key23.setGenerateMips(true);
    Texture tex23 = assetManager.loadTexture(key23);
    tex23.setWrap(WrapMode.Repeat);
    ceiling_mat3.setTexture("ColorMap", tex23);
            
    left_mat3 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key33 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
    key33.setGenerateMips(true);
    Texture tex33 = assetManager.loadTexture(key33);
    tex33.setWrap(WrapMode.Repeat);
    left_mat3.setTexture("ColorMap", tex33);
    
    right_mat3 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key43 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
    key43.setGenerateMips(true);
    Texture tex43 = assetManager.loadTexture(key43);
    tex43.setWrap(WrapMode.Repeat);
    right_mat3.setTexture("ColorMap", tex43);
    
    //cube4
    
    floor_mat4 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key14 = new TextureKey("Textures/Terrain/BrickWall/BrickWall.jpg");
    key14.setGenerateMips(true);
    Texture tex14 = assetManager.loadTexture(key14);
    tex14.setWrap(WrapMode.Repeat);
    floor_mat4.setTexture("ColorMap", tex14);

    ceiling_mat4 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key24 = new TextureKey("Textures/Terrain/BrickWall/BrickWall.jpg");
    key24.setGenerateMips(true);
    Texture tex24 = assetManager.loadTexture(key24);
    tex24.setWrap(WrapMode.Repeat);
    ceiling_mat4.setTexture("ColorMap", tex24);
    
    left_mat4 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key34 = new TextureKey("Textures/Terrain/BrickWall/BrickWall.jpg");
    key34.setGenerateMips(true);
    Texture tex34 = assetManager.loadTexture(key34);
    tex34.setWrap(WrapMode.Repeat);
    left_mat4.setTexture("ColorMap", tex34);
    
    right_mat4 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key44 = new TextureKey("Textures/Terrain/BrickWall/BrickWall.jpg");
    key44.setGenerateMips(true);
    Texture tex44 = assetManager.loadTexture(key44);
    tex44.setWrap(WrapMode.Repeat);
    right_mat4.setTexture("ColorMap", tex44);
    
}
    
    public void initFloor() {
        Geometry floor_geo = new Geometry("Floor", floor);
        floor_geo.setMaterial(floor_mat);
        floor_geo.setLocalTranslation(0, -6f, 0f);
        this.rootNode.attachChild(floor_geo);
        /* Make the floor physical with mass 0.0f! */
        floor_phy = new RigidBodyControl(0.0f);
        floor_geo.addControl(floor_phy);
        bulletAppState.getPhysicsSpace().add(floor_phy);
}
    public void initCeiling() {
        Geometry ceiling_geo = new Geometry("Ceiling", ceiling);
        ceiling_geo.setMaterial(ceiling_mat);
        ceiling_geo.setLocalTranslation(0, 6f, 0);
        this.rootNode.attachChild(ceiling_geo);
        /* Make the ceiling physical with mass 0.0f! */
        ceiling_phy = new RigidBodyControl(0.0f);
        ceiling_geo.addControl(ceiling_phy);
        bulletAppState.getPhysicsSpace().add(ceiling_phy);
}  
    
    public void initLeft() {
        Geometry left_geo = new Geometry("Left", left);
        left_geo.setMaterial(left_mat);
        left_geo.setLocalTranslation(-6f, 0f, 0f);
        this.rootNode.attachChild(left_geo);
        /* Make the left wall physical with mass 0.0f! */
        left_phy = new RigidBodyControl(0.0f);
        left_geo.addControl(left_phy);
        bulletAppState.getPhysicsSpace().add(left_phy);
}  
    
    public void initRight() {
        Geometry right_geo = new Geometry("Right", right);
        right_geo.setMaterial(right_mat);
        right_geo.setLocalTranslation(6f, 0f, 0f);
        this.rootNode.attachChild(right_geo);
        /* Make the right wall physical with mass 0.0f! */
        right_phy = new RigidBodyControl(0.0f);
        right_geo.addControl(right_phy);
        bulletAppState.getPhysicsSpace().add(right_phy);
}  
    
    public void initBack() {
        Geometry back_geo = new Geometry("Back", back);
        back_geo.setMaterial(back_mat);
        back_geo.setLocalTranslation(0f, 0f, -42f);
        this.rootNode.attachChild(back_geo);
        /* Make the back wall physical with mass 0.0f! */
        back_phy = new RigidBodyControl(0.0f);
        back_geo.addControl(back_phy);
        bulletAppState.getPhysicsSpace().add(back_phy);
}  
    
    
     public void initFloor2() {
        Geometry floor_geo2 = new Geometry("Floor2", floor2);
        floor_geo2.setMaterial(floor_mat2);
        floor_geo2.setLocalTranslation(0, -6f, -12f);
        this.rootNode.attachChild(floor_geo2);
        /* Make the floor2 physical with mass 0.0f! */
        floor_phy2 = new RigidBodyControl(0.0f);
        floor_geo2.addControl(floor_phy2);
        bulletAppState.getPhysicsSpace().add(floor_phy2);
}
    public void initCeiling2() {
        Geometry ceiling_geo2 = new Geometry("Ceiling2", ceiling2);
        ceiling_geo2.setMaterial(ceiling_mat2);
        ceiling_geo2.setLocalTranslation(0, 6f, -12f);
        this.rootNode.attachChild(ceiling_geo2);
        /* Make the ceiling2 physical with mass 0.0f! */
        ceiling_phy2 = new RigidBodyControl(0.0f);
        ceiling_geo2.addControl(ceiling_phy2);
        bulletAppState.getPhysicsSpace().add(ceiling_phy2);
}  
    
    public void initLeft2() {
        Geometry left_geo2 = new Geometry("Left2", left2);
        left_geo2.setMaterial(left_mat2);
        left_geo2.setLocalTranslation(-6f, 0f, -12f);
        this.rootNode.attachChild(left_geo2);
        /* Make the left2 wall physical with mass 0.0f! */
        left_phy2= new RigidBodyControl(0.0f);
        left_geo2.addControl(left_phy2);
        bulletAppState.getPhysicsSpace().add(left_phy2);
}  
    
    public void initRight2() {
        Geometry right_geo2 = new Geometry("Right2", right2);
        right_geo2.setMaterial(right_mat2);
        right_geo2.setLocalTranslation(6f, 0f, -12f);
        this.rootNode.attachChild(right_geo2);
        /* Make the right2 wall physical with mass 0.0f! */
        right_phy2 = new RigidBodyControl(0.0f);
        right_geo2.addControl(right_phy2);
        bulletAppState.getPhysicsSpace().add(right_phy2);
}        
       
    public void initFloor3() {
        Geometry floor_geo3 = new Geometry("Floor3", floor3);
        floor_geo3.setMaterial(floor_mat3);
        floor_geo3.setLocalTranslation(0, -6f, -24f);
        this.rootNode.attachChild(floor_geo3);
        /* Make the floor3 physical with mass 0.0f! */
        floor_phy3 = new RigidBodyControl(0.0f);
        floor_geo3.addControl(floor_phy3);
        bulletAppState.getPhysicsSpace().add(floor_phy3);
}
    public void initCeiling3() {
        Geometry ceiling_geo3 = new Geometry("Ceiling3", ceiling3);
        ceiling_geo3.setMaterial(ceiling_mat3);
        ceiling_geo3.setLocalTranslation(0, 6f, -24f);
        this.rootNode.attachChild(ceiling_geo3);
        /* Make the ceiling3 physical with mass 0.0f! */
        ceiling_phy3 = new RigidBodyControl(0.0f);
        ceiling_geo3.addControl(ceiling_phy3);
        bulletAppState.getPhysicsSpace().add(ceiling_phy3);
}  
    
    public void initLeft3() {
        Geometry left_geo3 = new Geometry("Left3", left3);
        left_geo3.setMaterial(left_mat3);
        left_geo3.setLocalTranslation(-6f, 0f, -24f);
        this.rootNode.attachChild(left_geo3);
        /* Make the left3 wall physical with mass 0.0f! */
        left_phy3 = new RigidBodyControl(0.0f);
        left_geo3.addControl(left_phy3);
        bulletAppState.getPhysicsSpace().add(left_phy3);
}  
    
    public void initRight3() {
        Geometry right_geo3 = new Geometry("Right3", right3);
        right_geo3.setMaterial(right_mat3);
        right_geo3.setLocalTranslation(6f, 0f, -24f);
        this.rootNode.attachChild(right_geo3);
        /* Make the right3 wall physical with mass 0.0f! */
        right_phy3 = new RigidBodyControl(0.0f);
        right_geo3.addControl(right_phy3);
        bulletAppState.getPhysicsSpace().add(right_phy3);
}  
    
     public void initFloor4() {
        Geometry floor_geo4 = new Geometry("Floor4", floor4);
        floor_geo4.setMaterial(floor_mat4);
        floor_geo4.setLocalTranslation(0, -6f, -36f);
        this.rootNode.attachChild(floor_geo4);
        /* Make the floor4 physical with mass 0.0f! */
        floor_phy4 = new RigidBodyControl(0.0f);
        floor_geo4.addControl(floor_phy4);
        bulletAppState.getPhysicsSpace().add(floor_phy4);
}
    public void initCeiling4() {
        Geometry ceiling_geo4 = new Geometry("Ceiling4", ceiling4);
        ceiling_geo4.setMaterial(ceiling_mat4);
        ceiling_geo4.setLocalTranslation(0, 6f, -36f);
        this.rootNode.attachChild(ceiling_geo4);
        /* Make the ceiling4 physical with mass 0.0f! */
        ceiling_phy4 = new RigidBodyControl(0.0f);
        ceiling_geo4.addControl(ceiling_phy4);
        bulletAppState.getPhysicsSpace().add(ceiling_phy4);
}  
    
    public void initLeft4() {
        Geometry left_geo4 = new Geometry("Left4", left4);
        left_geo4.setMaterial(left_mat4);
        left_geo4.setLocalTranslation(-6f, 0f, -36f);
        this.rootNode.attachChild(left_geo4);
        /* Make the left4 wall physical with mass 0.0f! */
        left_phy4 = new RigidBodyControl(0.0f);
        left_geo4.addControl(left_phy4);
        bulletAppState.getPhysicsSpace().add(left_phy4);
}  
    
    public void initRight4() {
        Geometry right_geo4 = new Geometry("Right4", right4);
        right_geo4.setMaterial(right_mat4);
        right_geo4.setLocalTranslation(6f, 0f, -36f);
        this.rootNode.attachChild(right_geo4);
        /* Make the right4 wall physical with mass 0.0f! */
        right_phy4 = new RigidBodyControl(0.0f);
        right_geo4.addControl(right_phy4);
        bulletAppState.getPhysicsSpace().add(right_phy4);
}  
    
    /** A plus sign used as crosshairs to help the player with aiming.*/
    protected void initCrossHairs() {
        guiNode.detachAllChildren();
        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);
        guiNode.attachChild(ch);
}

    public void makeBall() {    
        Sphere sphere1 = new Sphere(32, 32, 0.25f);
        Geometry ball1 = new Geometry("Ball1", sphere1);
        Material sph1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");  // create a simple material
        sph1.setColor("Color", ColorRGBA.Yellow);   // set color of material to yellow
        ball1.setMaterial(sph1);                   // set the ball's material
        ball1.setLocalTranslation(cam.getLocation()); 
        ball1.rotate(2f, 3f, 2f);  // Rotate it a bit
        RigidBodyControl ball_phy1 = new RigidBodyControl(1f); // Make the ball physcial with a mass > 0.0f 
        ball1.addControl(ball_phy1); // Add physical ball to physics space
        bulletAppState.getPhysicsSpace().add(ball_phy1); 
        ball_phy1.setLinearVelocity(cam.getDirection().mult(25)); // Accelerate the physcial ball to shoot it
        rootNode.attachChild(ball1);
}   
    
    
@Override
public void simpleUpdate(float tpf) {
    //TODO: add update code
}

@Override
public void simpleRender(RenderManager rm) {
    //TODO: add render code
}

}

use a PhysicsCollisionListener
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:physics_listeners