Simple Collision

Hi, I’m making a simple shoot em up to test, I need to check collision but I not find a simple example to check this, I wrote the following code

[java]

@Override

public void simpleInitApp() {



this.getFlyByCamera().setEnabled(false);

cam.setLocation(new Vector3f(cam.getLocation().x,

cam.getLocation().y,

cam.getLocation().z + 100));



Spatial s = assetManager.loadModel(“Models/Monkey3/Monkey3.j3o”);



Box b = new Box(Vector3f.ZERO, 1, 1, 1);

Geometry geom = new Geometry(“Box”, b);



Material mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

mat.setColor(“Color”, ColorRGBA.Blue);

geom.setMaterial(mat);





rootNode.attachChild(geom);

rootNode.attachChild(s);



DirectionalLight sun = new DirectionalLight();



sun.setDirection(new Vector3f(-1.0f,0,-1.5f).normalizeLocal());

sun.setColor(ColorRGBA.White);

rootNode.addLight(sun);



s.setLocalTranslation(Vector3f.ZERO);

geom.setLocalTranslation(Vector3f.ZERO);

CollisionResults results = new CollisionResults();

geom.collideWith(s, results);

if (results.size() > 0)

System.out.println(“Colision!!!”);







}

[/java]



to display “Colision!!!” when the models collided. I have a geom box and a spatial monkey, both located in 0.0f,0.0f,0.0f, when I check the collision with the collideWith method, I take a exception



“com.jme3.collision.UnsupportedCollisionException

at com.jme3.collision.bih.BIHTree.collideWith(BIHTree.java:461)

at com.jme3.scene.Mesh.collideWith(Mesh.java:856)

at com.jme3.scene.Geometry.collideWith(Geometry.java:448)

at mygame.Main.simpleInitApp(Main.java:64)

at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:228)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:129)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:205)

at java.lang.Thread.run(Thread.java:722)”



Can somebody give a simple example to check collision? It is a simple game in 2D with 3d models ( 2.5D :wink: ) I want to check collision only in x and y without physics.



Thanks.

Bye.

Check “Hello Picking”

Thanks, but i don’t know how apply that example to my issue, why the collideWith method work with Ray class and don’t work with geometry? I thing that maybe i can have and instance off standard Java Rectangle class for all models in scene and check the collisions with constaint method, in this case i have to know how i get the model,s size in x and y to define the rectangles. Maybe it is a complicate solutio, can any body help me with this, i have to manage collision in 2d.



Thanks.

It just doesn’t, you can use some bounding volumes or the bullet sweepTest.

Thanks normen, with the bounding volumes work fine, it have not very presision collision detection but is right for my case, I use the intersects method of bounding volumes



[java]





Box a = new Box(Vector3f.ZERO, 1, 1, 1);

Geometry geomA = new Geometry ("BoxA", a);

Material matA = new Material("Common/MatDefs/Misc/Unshaded.j3md");

matA.setColor("Color", ColorRGBA.Red);

geomA.setMaterial(matA);



Box b = new Box(Vector3f.ZERO, 1, 1, 1);

Geometry geomB = new Geometry ("BoxB", b);

Material matB = new Material("Common/MatDefs/Misc/Unshaded.j3md");

matB.setColor("Color", ColorRGBA.Blue);

geomB.setMaterial(matB);





if (geomA.getWorldBound().intersects(geomB.getWorldBound())

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







[/java]



Thanks you very much.