Problem with collision


hi there, first post here,
i have a problem with collisions.. i'm beginner with jme and not so skilled with java just yet..

i loaded a maze object (wavefront file.obj with file.mtl and textures)
it displays fine, and jme looks very interesting..
here's the inside of the object:


now about my collision problem,
as shown on below picture (when i move away from the object)

i can see a single giant bounding box surrounding the whole maze object.
i tryed collision with it and it worked, but,

what i'm trying to do is to have collision test with walls and floor, ceilling..
why when i'm inside the maze and i don't touch walls or floor or ceilling the collision still happen?
only when i get out of the maze and its giant bounding box the collision stops..

what do i am missing ?

thanks for your attention


and here's the code:


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.bounding.CollisionTree;
import com.jme.bounding.CollisionTreeManager;
import com.jme.intersection.CollisionData;
import com.jme.intersection.CollisionResults;
import com.jme.intersection.TriangleCollisionResults;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Geometry;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Box;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.ObjToJme;

public class HelloModelLoading extends SimpleGame {
   
        Spatial b, m;
   CollisionResults results;
   CollisionData oldData;
   
    public static void main(String[] args) {
        HelloModelLoading app = new HelloModelLoading();
        app.setConfigShowMode(ConfigShowMode.AlwaysShow);
        Logger.getLogger("").setLevel(Level.SEVERE);
        //MouseInput.get().setCursorVisible(true);
        app.start();
    }

    protected void simpleInitGame() {       
       
      CollisionTreeManager.getInstance().setTreeType(CollisionTree.Type.AABB);      
      results = new TriangleCollisionResults();
      b = new Box("box", new Vector3f(0,0,0), 1, 1 ,1);
      b.setLocalScale(.1f);
      ((Geometry) b).setSolidColor(ColorRGBA.white.clone());
      b.setModelBound(new BoundingBox());
      b.updateModelBound();

        URL model=HelloModelLoading.class.getClassLoader().getResource("data/test_01_02.obj");
        FormatConverter converter=new ObjToJme();
        converter.setProperty("mtllib",model);

        ByteArrayOutputStream BO=new ByteArrayOutputStream();
        try {
            converter.convert(model.openStream(), BO);
            m=(Spatial) BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
            m.setLocalScale(.1f);
            m.setModelBound(new BoundingBox());
            m.updateModelBound();

            rootNode.attachChild(b);
            rootNode.attachChild(m);

        } catch (Exception e) {   // Just in case anything happens
            System.out.println("Damn exceptions! O_o n" + e);
            e.printStackTrace();
            System.exit(0);
        }
    }
   
    protected void simpleUpdate() {   

       results.clear();
       b.setLocalTranslation(new Vector3f(cam.getLocation()));
       b.findCollisions(m, results);
       if (results.getNumber() > 0) {
          System.out.println("somehow collision happened n");
       }
    }  
}


if the while maze is one single Geometry, you have a problem. Normally the single walls / floors should be their own Geometry.



You can try going through the maze recursively and adding BoundingVolumes to each part of the maze. if there are no parts of the maze, you have to model the maze differently. e.g. model one wall as a Geometry and group them in your modelling tool (will be a Node later in jME) and so on. Then the recursive adding of BoundingVolumes should work.



The collision always happens because you check for a collision with the maze which has one BoundingVolume around it and if you are inside it -> you have a collision.

Depending on the modeling package you are using it should be possible to divide your model into parts more suitable for bounding collision checking before exporting to OBJ.

Or see http://code.google.com/p/jmonkeyengine/source/browse/trunk/src/jmetest/intersection/TestCollisionTree.java

for an example of how to use triangle accurate collisions.

hevee said:

Depending on the modeling package you are using it should be possible to divide your model into parts more suitable for bounding collision checking before exporting to OBJ.
Or see http://code.google.com/p/jmonkeyengine/source/browse/trunk/src/jmetest/intersection/TestCollisionTree.java
for an example of how to use triangle accurate collisions.


thanks for replies

triangle accurate collisions? so this is the name of the thing i need ?
i mean how do ppl usualy do check if they are colliding or are just in an empty space?
if i place myself in some empty space i have in my 3d model, it will alway return collision true ?
how do jme make bounding boxes accurate to the model geometry ? i can't add boxes and place them manually each time i have a wall in my model..

so my bounding box approach isn't good? i need another thing to test for collision ? triangle accurate collisions will be it ?

i was already looking at TestCollisionTree.java, i copied few lines from that file into my project,
i was trying to understand it, and even tried thoses lines:

CollisionTreeManager.getInstance().setTreeType(CollisionTree.Type.AABB);
results = new TriangleCollisionResults();

but i couldn't make it work like the CollisionTree demo .. all i get is that giant bounding box ;)
just can't see what i miss yet ..

in my opinion, triangle collisions are overkill for that maze. if you subdivide your maze youll be good to go with boundingvolumes. e.g. if you add a BoundingBox to a simple wall in your maze it will fit the dimensions of the wall exactly. if you place the player inside the maze and check for collisions with the walls you will not get collisions if you have no collisions. you just have to check for collisions with the walls and not with the maze itself. just know that triangle collisions are much more performance killing than collisions with bounding volumes.



just subdivide your maze, as i said before

dhdd said:


just subdivide your maze, as i said before


err like how ?
also this maze is just a test, i plan to have bigger ones.. basicaly the same just bigger..
you're suggesting i add manualy boxes and place them manually setting their loacation vector? no it can be ?
there must be some kind of automated stuff in jme no ? i just need empty space inside my model to not report collision true..

i have no idea about subdivide yet.. i'm still trying to figure how to adapt collisionTree example so i don't get collision when being in and empty space inside my model..

sorry for my bad english :/

in your modelling tool, you will most propably see one object, and thats the complete maze. What you want is a lot of objects. each of them being a single wall, a cornerstone, the floor, etc. Then you might want to group all of them (not join, not merge or stuff like that) just group.



When you import that model into jME you will have one Node and the children of that node are the floors, walls, and so on.



Then you can check for collision with the floor, or a particular wall, or both, and so forth.



hope its clearer now