Collision detection-event

Hello i want to do an action like printing in the console, playing a sound when a spatial intersect other spatial. How do i do that?

Here is my try



[java]

import com.jme3.app.SimpleApplication;

import com.jme3.bullet.BulletAppState;

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.CollisionShape;

import com.jme3.bullet.control.GhostControl;

import com.jme3.bullet.control.RigidBodyControl;

import com.jme3.bullet.util.CollisionShapeFactory;

import com.jme3.collision.CollisionResult;

import com.jme3.collision.CollisionResults;

import com.jme3.input.KeyInput;

import com.jme3.input.controls.AnalogListener;

import com.jme3.input.controls.KeyTrigger;

import com.jme3.light.AmbientLight;

import com.jme3.light.DirectionalLight;

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;





public class TestCollision extends SimpleApplication implements AnalogListener,PhysicsCollisionListener

{

private Node cube1, cube2;

static BulletAppState bulletAppState;

private RigidBodyControl controlCube1, controlCube2;

public static void main(String args[])

{

TestCollision testCollision=new TestCollision();

testCollision.start();

}

@Override

public void simpleInitApp()

{

AmbientLight al=new AmbientLight();

al.setColor(ColorRGBA.White);

rootNode.addLight(al);

DirectionalLight dl=new DirectionalLight();

dl.setDirection(new Vector3f(0, -1, -2));

flyCam.setMoveSpeed(10);

Geometry cube1=makeBox(new Vector3f(1, 0, 1));

Geometry cube2=makeBox(new Vector3f(-2, 0, -2));

this.cube1=new Node("cube1");

this.cube2=new Node("cube2");

this.cube1.attachChild(cube1);

this.cube2.attachChild(cube2);

rootNode.attachChild(this.cube1);

rootNode.attachChild(this.cube2);

initKeys();



bulletAppState=new BulletAppState();

stateManager.attach(bulletAppState);

bulletAppState.getPhysicsSpace().addCollisionListener(this);

bulletAppState.getPhysicsSpace().enableDebug(assetManager);

initControl(controlCube1, this.cube1);

initControl(controlCube2, this.cube2);

}

@Override

public void simpleUpdate(float tpf)

{

// CollisionResults collisionResults=new CollisionResults();

// cube1.collideWith(cube2, collisionResults);

// CollisionResult restult=collisionResults.getClosestCollision();

// if(restult!=null)

// System.out.println("1111111111111111111111");

}

private Geometry makeBox(Vector3f loc)

{

Box box=new Box(loc, 0.5f, 0.5f, 0.5f);

Geometry cube=new Geometry("Cube", box);

Material matLightning=new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");

cube.setMaterial(matLightning);

return cube;

}

private void initKeys()

{

inputManager.addMapping("left", new KeyTrigger(KeyInput.KEY_NUMPAD4));

inputManager.addMapping("right", new KeyTrigger(KeyInput.KEY_NUMPAD6));

inputManager.addMapping("forward", new KeyTrigger(KeyInput.KEY_NUMPAD8));

inputManager.addMapping("back", new KeyTrigger(KeyInput.KEY_NUMPAD5));



inputManager.addListener(this, new String[]{"left","right","forward","back"});

}

@Override

public void onAnalog(String name, float value, float tpf)

{

final float SPEED=2;

if(name.equals("left"))

{

cube1.move(-SPEEDtpf, 0, 0);

}

if(name.equals("right"))

{

cube1.move(SPEED
tpf, 0, 0);

}

if(name.equals("forward"))

{

cube1.move(0, 0, -SPEEDtpf);

}

if(name.equals("back"))

{

cube1.move(0, 0, SPEED
tpf);

}

}

@Override

public void collision(PhysicsCollisionEvent e)

{

System.out.println("cube 1 intersect cube 2");

}

private void initControl(RigidBodyControl control, Node cube)

{

CollisionShape collisionShape=CollisionShapeFactory.createDynamicMeshShape(cube);

control=new MyPhysicsControl(collisionShape, 0);

cube.addControl(control);

bulletAppState.getPhysicsSpace().add(control);

}

}

[/java]



Btw Ive read all tutorials<br /> <br /> https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_picking<br /> <br /> https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_collision<br /> <br /> https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_physics<br /> <br /> https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:collision_and_intersection<br /> <br /> https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:mouse_picking<br /> <br /> https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:physics<br /> <br /> but i still cant figure it out :frowning: pls help me.Can you send me the code pls

anybody…pls…