Collision with mesh not geom

Hi i’ve made a map and imported it from blender.Now i need to give my units a heigh (an Y) to set them to right place,when i was testing stuff before making map i was using raycasting and a simple box geom,but now when i’ve imported pretty complex map i cant get ray to collide with map mesh,it collides to its box but not to mesh it self,what can i do?

   private AnalogListener analogListener = new AnalogListener() {
            if(playerChanel.getAnimationName().equalsIgnoreCase("Walk1HSw&Shield")){//Walk
                player.move(cam.getDirection().getX()*tpf, 0,cam.getDirection().getZ()*tpf);
    }
}

  public void simpleUpdate(float tpf) {
  origin= newVector3f(player.getWorldTranslation().getX(),player.getWorldTranslation().getY()+5,player.getWorldTranslation().getZ());
      dir=new Vector3f (player.getWorldTranslation().getX(), player.getWorldTranslation().getY()-10, 
  player.getWorldTranslation().getZ()) ;
      ray = new Ray(origin, dir);
      //floor.collideWith(ray, results);
      terrain.collideWith(ray, results);
      
      if (results.getClosestCollision()!=null){

           player.getLocalTranslation().setY(results.getClosestCollision().
                   
      
   getContactPoint().getY()+player.getChild(0).getLocalScale().getY()/2+player.getChild(0).getLocalScale().getY()/4);
           System.out.println("ColPoint-"+results.getClosestCollision().getContactPoint()+
                   "  whatColl"+results.getClosestCollision().getGeometry()+"  
      playersLocation"+player.getLocalTranslation());
           
      }
 }
  • Node terrain = (Node)assetManager.loadModel(“Models/MapFirstTry/MapFirstTry.j3o”);

main problem is player crashes through textures of terrain from time to time in some places ,but some other places dont

here is the file

Use raycast with spatial.

// load your map
Spatial model = assetManager.loadModel("path/to/your/asset.j3o");

// raycast
CollisionResults results = new CollisionResults();
Ray ray = ....;// set the origin and direction

// collision with mesh
model.collisideWith(ray, result);
if (results.size() > 0) {
    // The closest collision point is what was truly hit:
    CollisionResult closest = results.getClosestCollision();
    Vector3f point = closest.getContactPoint();
}

It hits air for me :frowning:

In the “tests” project of jME there are some examples that show you how to do raycasting against a terrain - a red sphere is placed where the ray hits the terrain. Maybe you look there. Do you use the SDK? Then it’s “File > New Project > jME Tests (or similar)”, then you just run one of the many .java Files that have a main() in them or the TestChoser.java which lets you select a test. :slight_smile:

What does your collision code look like? If you follow the way @yan structured his collision code example, then it should work to collide with the geometry titled “model,” and in the case that your “model” is a node, then it would collide with all the children attached to the node.

model.collisideWith(ray, result);

If your collision is happening in mid air over a terrain, then I think you might be calling the ".getWorldBound()’ method on your collision object (I had this problem at one point, I had wrongly thought the bounding volume could take on the shape of a mesh, but a spatial’s bounding volume will always be either a box or a sphere)

Also… jME 2 had “triangle accurate collision” which might also be what you are looking for.
Maybe jME 3.2 has the same functionality either directly or as a plugin.

And … you might also use physics mesh and physics ray-collision from bullet.
I guess you’d need a separate PhysicsSpace to also have the usual physics in your game.

Your code looks fine from here.

AKA: we can’t debug what we can’t see. Did you put the ray in the right place? Did you point it in the right direction? Who knows?

The magic is the ray’s origin and direction.

Since you said:

I assume you already have a coordination of (x, 0, z), and you want y.

It means the ray direction should be either UP(0, 1, 0) or DOWN(0, -1, 0).

Then you need to set ray origin point to a right place.

If (the `direction` is `DOWN(0, -1, 0)`) {
     the `origin point` should be located `high above` your map
}

or

If (the `direction` is `UP(0, 1, 0)`) {
     the `origin point` should be located `down below` your map
}

To confirm that, you need the AABB (Axis Aligned Bounding Box) of your map in world space. With the center position and extentY of AABB, you can calculate the max and min height of your map. You can also get max and min from AABB directly.

In jME3, we have BoundingBox for AABB and BoundingSphere for Sphere, they are both BoundingVolume. Every Spatial has its BoundingVolume, BoundingBox is the default bounding type.

// The ray origin point 
float x = ...;// your x
float y = 0; // to be calculated
float z = ...;// your z
Vector3f origin = new Vector3f(x, y, z);

// The ray direction
// In this case, I use DOWN direction. which means origin.y should be high above your map.
Vector3f direction = new Vector3f(0, -1, 0);

// load your map
Spatial model = assetManager.loadModel("path/to/your/asset.j3o");

// get AABB form the model
BoundingBox aabb = (BoundingBox) model.getWorldBound();
// make sure y is above AABB.
Vector3f max = aabb.getMax( new Vector3f() );
origin.y = max.y + 1f;

// raycast
CollisionResults results = new CollisionResults();
Ray ray = new Ray(origin, direction);// set the origin and direction

// TODO collision with mesh

Thnx for your help,it seems the problem was that there was node inside node inside node before mesh,i’ve removed extra nodes and it has started to work properly

Damn no ,its not working i just keep getting collision on some thing else but not what i see on mesh,its like it ignores hills and mountains

Em could blender mesh be corrupted? its like it has some places where my character falls and some where he follows terrain perfectly

You could create a test case and post the code of your TestCase.java here.
Also upload the problematic Blender file (or a small sub-section of your mesh).
Then someone with a little bit of free time might help you. :slight_smile:

(This is the standard advice that every experienced user would give you)

Yep, and I also asked for it… so now it’s x2.

Most of the experienced users are probably ignoring this thread now until code/test case is provided.