getClosestCollision problem

Hi. I found problem "CollisionResults.getClosestCollision.getGeometry" returns null.

It is happen a geometry overtake another geometry.



import com.jme3.app.SimpleApplication;
import com.jme3.bounding.BoundingBox;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;

public class TestClosetCollision extends SimpleApplication {

   Geometry geom1, geom2;
   Node node;
   Ray ray;
   CollisionResults results;
   
   @Override
   public void simpleInitApp() {
      Box box = new Box(Vector3f.ZERO, Vector3f.UNIT_XYZ);
      node = new Node();
      geom1 = this.getGeometory(box, "box1", new Vector3f(5, 0, 0));
      geom2 = this.getGeometory(box, "box2", new Vector3f(10, 0, 0));
      
      node.attachChild(geom1);
      node.attachChild(geom2);
      
      ray = new Ray(Vector3f.ZERO, Vector3f.UNIT_X);
      results = new CollisionResults();
      
      this.rootNode.attachChild(node);
   }
   
   public Geometry getGeometory(Mesh mesh, String name, Vector3f v) {
      Geometry geom = new Geometry(name, mesh);
      Material mat = new Material(this.getAssetManager(), "Common/MatDefs/Misc/SolidColor.j3md");
      mat.setColor("m_Color", ColorRGBA.randomColor());
      geom.setMaterial(mat);
      geom.setLocalTranslation(v);
      geom.setModelBound(new BoundingBox());
      geom.updateModelBound();
      geom.updateGeometricState();
      return geom;
   }
   
   Vector3f v  = new Vector3f(.001f, 0, 0);
   
   @Override
   public void simpleUpdate(float tpf) {
      
      Vector3f local = geom2.getLocalTranslation();
      if (local.x < 0 || local.x > 10) {
         v.x *= -1;
      }
      local.addLocal(v);
      geom2.setLocalTranslation(local);
      node.updateGeometricState();
      
      results.clear();
      node.collideWith(ray, results);
      System.out.println("----collision


");
      CollisionResult result = results.getClosestCollision();
      System.out.println("geom:"+result.getGeometry());
      System.out.println("dist:"+result.getDistance());
   }

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



Thank you.

I think I might be experiencing something similar as I've been frustrated with a piece of code I've been working on for a while that I couldn't figure out for the life of me why it was returning null for the closest geometry even though there were several it should be piercing with the ray collision. Guess this might have something to do with it.

I think this might be related to this bug:

http://code.google.com/p/jmonkeyengine/issues/detail?id=176

Its related to how the CollisionResults is automatically sorted on getCollision() call, so the proper geometry can't be set.

Latest svn repository code works fine ! Thank you.  :slight_smile: