Ray collides with terrian


I has a problem with Ray and Terrain. I following a video online (- YouTube) to create a terrain for my game. I need the collision to move my character to. But it turns out that, the ray (I created the ray same as the book’s example) does not collide with the terrain. As I understand, my terrain does has any geometries so there is nothing for the ray to collides with. When I loadModel() the terrain, It should be the a Spatial or a Node (with no Geom child). Thus, I am figuring out how to detect the collision. Is the ray only collide with geometry??
I hope there is a solution for this, I found some same issues but I cannot applied them on my case because I create a terrain from Jmonkey editor but not writing code.

P/S: I just think about a solution that I put my terrain on a big invisible box and detect the collision with the box instead of the terrain. but not sure that the invisible box is invisible with the ray. I will try it now but I hope there is a official solution for this case.

I’d say the terrain is a geometry but could you please show us some code for how the Ray is setup and the node you try to collide with?

Forgot to mention that http://wiki.jmonkeyengine.org/doku.php/jme3:beginner:hello_picking is a great resource on the subject as well.

First of all, I load the terrain
private Spatial mapModel;

mapModel = assetManager.loadModel(“Scenes/Island.j3o”); // the Island.j3o is the terrain I created without geometries

Here is the code for the ray:

     if (name.equals(MAPPING_CLICK)&& !isPressed){ //isPressed to test if button is only pressed
            CollisionResults results = new CollisionResults(); // new collision
            // convert 2D vector from clicking to 3D 
            Vector2f click2d = inputManager.getCursorPosition();
            Vector3f click3d = cam.getWorldCoordinates(new Vector2f(click2d.getX(), click2d.getY()), 0f);

            Vector3f dir = cam.getWorldCoordinates(
                        new Vector2f(click2d.getX(), click2d.getY()), 1f).subtractLocal(click3d);        
            Ray ray = new Ray(click3d, dir);
            rootNode.collideWith(ray, results);
            if (results.size() > 0) {// results size is the number of the faces collided with
                target = results.getClosestCollision().getGeometry();

After running the code, results.size() is zero when clicking on map.
Thanks for your help.

Aslo, I have tested with Sinbad model which containing 7 parts of geometry. It has the collisions with those geometries but when I click on the map, no collision.

Here is the game view

Not sure it’s the issue… but I think your Ray is invalid because the direction is not normalized.


Your vector from the click is invalid because its at 0, the answer to the “question” you pose by saying “cam.getWorldCoordinates(new Vector2f(click2d.getX(), click2d.getY()), 0f);” is “what point in the world goes through the camera position and the camera position (0)” where the answer is “all of them”.

I think it’s the other way around that’s problematic.

Passing 0 to to go from 2D to 3D should give you a point just in front of the camera’s actual location.

But Ray does not normalize the direction that was passed as it expects the caller to half-know what they are up to. And while some of the mesh collision routines might be ok with that, it’s possible that the terrain mesh is not.

Right, that was position on screen.

I am so happy today. Yeah, problem is about the direction is not normalized. I think I will investigate more in this “normalized” direction to avoid the problems later. Your guys are awesome. Thank you so much, really appreciate.