Problem with PhysicsCollisionListiner

Ok looks like with big help from Normen I have solved all problems. I’m leaving here code if someone will have same issue.

[java]
package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.renderer.RenderManager;

/**

  • test

  • @author normenhansen
    */
    public class Main extends SimpleApplication {

    Room room1;
    Room room2;
    Bullet lissn = new Bullet(this);

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

    @Override
    public void simpleInitApp() {
    inputManager.addMapping(“shoot in room 1”, new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
    inputManager.addMapping(“shoot in room 2”, new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
    inputManager.addListener(actionListener, “shoot in room 1”, “shoot in room 2”);
    initRooms();
    }
    private ActionListener actionListener = new ActionListener() {
    public void onAction(String name, boolean keyPressed, float tpf) {
    if (name.equals(“shoot in room 1”) && !keyPressed) {
    room1.createBullet(“first bullet in room 1”);
    }

         if (name.equals("shoot in room 2") && !keyPressed) {
             room2.createBullet("second bullet in room 2");
         }
     }
    

    };

    @Override
    public void simpleUpdate(float tpf) {
    }

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

    private void initRooms() {
    room1 = new Room(“first”, lissn);
    room2 = new Room(“second”, lissn);
    getStateManager().attach(room1);
    getStateManager().attach(room2);
    }

    public Room getRoomByName(String name) {
    if (name.equals(“first”)) {
    return room1;
    } else if (name.equals(“second”)) {
    return room2;
    } else {
    return null;
    }
    }
    }
    [/java] [java]
    /*

  • To change this template, choose Tools | Templates

  • and open the template in the editor.
    */
    package mygame;

import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.TextureKey;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.material.Material;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Sphere;
import com.jme3.texture.Texture;

/**
*

  • @author Setas
    */
    public class Room extends AbstractAppState {

    private BulletAppState bulletAppState;
    private String name;
    private SimpleApplication app;
    private Node rootNode;
    Material mat;
    Material mat3;
    Bullet lisn;

    Room(String name, Bullet lisn) {
    this.name = name;
    bulletAppState = new BulletAppState();
    this.lisn = lisn;

    }

    Room(String name) {
    this.name = name;
    bulletAppState = new BulletAppState();
    }

    @Override
    public void initialize(AppStateManager stateManager, Application app) {
    super.initialize(stateManager, app);
    this.app = (SimpleApplication) app;
    rootNode = new Node();
    app.getStateManager().attach(bulletAppState);
    this.app.getRootNode().attachChild(rootNode);
    mat = new Material(app.getAssetManager(), “Common/MatDefs/Misc/Unshaded.j3md”);
    bulletAppState.getPhysicsSpace().addCollisionListener(lisn);

     initFloor();
    

    }

    public void createBullet(String name) {
    System.out.println(“create”);
    Bullet lisn = new Bullet();
    Sphere ballMesh = new Sphere(32, 32, 0.25f, true, false);
    Geometry ballGeo = new Geometry(name + “[created]”, ballMesh);
    ballGeo.setUserData(“room”, getName());
    ballGeo.setMaterial(mat);
    ballGeo.setLocalTranslation(new Vector3f(0, 5, 0));
    ballGeo.addControl(lisn);
    rootNode.attachChild(ballGeo);
    bulletAppState.getPhysicsSpace().add(lisn);
    }

    @Override
    public String toString() {
    return “[ room name is - " + name + " ]”;
    }

    public void initFloor() {
    mat3 = new Material(app.getAssetManager(), “Common/MatDefs/Misc/Unshaded.j3md”);
    TextureKey key3 = new TextureKey(“Textures/Terrain/Pond/Pond.jpg”);
    key3.setGenerateMips(true);
    Texture tex3 = app.getAssetManager().loadTexture(key3);
    tex3.setWrap(Texture.WrapMode.Repeat);
    mat3.setTexture(“ColorMap”, tex3);
    Box floorBox = new Box(Vector3f.ZERO, 10f, 0.1f, 5f);
    floorBox.scaleTextureCoordinates(new Vector2f(3, 6));
    Geometry floor = new Geometry(“floor”, floorBox);
    floor.setMaterial(mat3);
    floor.setShadowMode(RenderQueue.ShadowMode.Receive);
    floor.setLocalTranslation(0, -0.1f, 0);
    floor.addControl(new RigidBodyControl(new BoxCollisionShape(new Vector3f(10f, 0.1f, 5f)), 0));
    rootNode.attachChild(floor);
    bulletAppState.getPhysicsSpace().add(floor);
    }

    public BulletAppState getBulletAppState() {
    return bulletAppState;
    }

    public PhysicsSpace getPhysicSpace() {
    return bulletAppState.getPhysicsSpace();
    }

    public String getName() {
    return name;
    }

    public void removeBullet(String name){
    bulletAppState.getPhysicsSpace().removeAll(rootNode.getChild(name));
    rootNode.getChild(name).removeFromParent();
    }
    }
    [/java] [java]
    /*

  • To change this template, choose Tools | Templates

  • and open the template in the editor.
    */
    package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.PhysicsTickListener;
import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.bullet.collision.PhysicsCollisionListener;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.RigidBodyControl;

/**
*

  • @author Setas
    */
    public class Bullet extends RigidBodyControl implements PhysicsCollisionListener {

    private Main app;

    public Bullet(CollisionShape shape, float mass) {
    super(shape, mass);
    }

    Bullet(SimpleApplication app) {
    this.app = (Main) app;
    }

    Bullet() {
    }

    public void collision(PhysicsCollisionEvent event) {
    System.out.println(event.getNodeA().getUserData(“room”) + " - bullet colliding - " + space + " " + spatial);
    System.out.println(event.getNodeB().getUserData(“room”) + " - bullet colliding - " + space + " " + spatial);

     String nodeARoom = event.getNodeA().getUserData("room");
     String nodeBRoom = event.getNodeB().getUserData("room");
     String nodeAName = event.getNodeA().getName();
     String nodeBName = event.getNodeB().getName();
    
     if (nodeARoom != null) {
         app.getRoomByName(nodeARoom).removeBullet(nodeAName);
     } else if (nodeBRoom != null) {
         app.getRoomByName(nodeBRoom).removeBullet(nodeBName);
     }
    

    }

}
[/java]

1 Like