Collision Listeners

Hey Guys,

I am making a Portal Like Object, but for some reason I can’t detect Collisions with it. I have no Idea what I did wrong. Any help please?

Code Below



Portal.Java

[java]



import actor.Controls.PortalControl;

import com.jme3.bullet.collision.PhysicsCollisionEvent;

import com.jme3.bullet.collision.PhysicsCollisionListener;

import com.jme3.bullet.collision.shapes.BoxCollisionShape;

import com.jme3.bullet.collision.shapes.HullCollisionShape;

import com.jme3.bullet.control.GhostControl;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.Node;

import com.jme3.scene.shape.Box;



/**

*

  • @author Gogol

    */

    public class Portal

    {

    byte x, y, z;



    public GravityWell Parent;

    String IDENT;



    public Node portal;



    public Portal(byte x, byte y, byte z, GravityWell Parent)

    {

    this.x = x;

    this.y = y;

    this.z = z;

    this.Parent = Parent;

    IDENT = "" + Parent.LocalSeed + "-[" + x + "," + y + "," + z + "]";

    portal = new Node(IDENT);



    Box b = new Box( 1, 1, 1); // create cube shape at the origin

    Geometry geom = new Geometry("Box", b); // create cube geometry from the shape

    Material mat = new Material(Parent.App.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md"); // create a simple material

    mat.setColor("Color", ColorRGBA.Green); // set color of material to blue

    geom.setMaterial(mat);



    portal.attachChild(geom);



    GhostControl gControl = new GhostControl(new HullCollisionShape(b));

    portal.addControl(gControl);

    PortalControl pControl = new PortalControl(this);

    portal.addControl(pControl);

    }





    }

    [/java]



    PortalControl

    [java]



    import com.jme3.bullet.BulletAppState;

    import com.jme3.bullet.collision.PhysicsCollisionEvent;

    import com.jme3.bullet.collision.PhysicsCollisionListener;

    import com.jme3.bullet.control.GhostControl;

    import com.jme3.bullet.control.RigidBodyControl;

    import space.Portal;



    /**

    *
  • @author Gogol

    */

    public class PortalControl extends GhostControl implements PhysicsCollisionListener

    {

    BulletAppState bullet;



    Portal Parent;

    public PortalControl(Portal portal)

    {

    Parent = portal;

    this.bullet = Parent.Parent.Parent.Parent.Parent.bullet;

    bullet.getPhysicsSpace().addCollisionListener(this);

    }



    public void collision(PhysicsCollisionEvent event)

    {

    System.out.println("COLLISION!");

    if(event.getNodeA().getName().equals(Parent.portal.getName()))

    {

    System.out.println(event.getNodeB().getName());

    }

    else if(event.getNodeB().getName().equals(Parent.portal.getName()))

    {

    System.out.println(event.getNodeA().getName());

    }

    }



    }

    [/java]

Do you add the controls to the physics space?

I don’t think 2 ghost objects can collide. Use control.getOverlappingObjects() in simpleUpdate(float tpf). Failing that try it in a physicsTick listener.