Intersection problem

Hi there,



have a trouble with MousePicking. Have to say, first, that I’ve looked around, especially in the forum, to understand how to proceed but… I still have problems. So, look this image first:









and the code:


   
    void setCamera() {
       cam.setLocation(new Vector3f(-6f, 3.4f, 3f));
       cam.lookAt(new Vector3f(0f, 1f, 0f), new Vector3f(0f, 1f, 0f));       
       cam.setUp(new Vector3f(0f, 1f, 0f));
       cam.setLeft(new Vector3f(-1f, 0f, 0f));
    }
    
    protected void simpleUpdate() {
       
      //My pov needs to be always the same...
      setCamera();

      MouseInput thisMouse = MouseInput.get();
      results.clear();
      if (thisMouse.isButtonDown(0)) {
      
         Vector2f screenPos = new Vector2f();
          screenPos.set(am.getHotSpotPosition().x, am.getHotSpotPosition().y);
          
          System.out.println("Screen pos: " + screenPos.x + " " + screenPos.y);
          
          Vector3f worldCoords = display.getWorldCoordinates(screenPos, 0);
          System.out.println("World pos: " + worldCoords.x + " " + worldCoords.y + " " + worldCoords.z);
          
          Ray mouseRay = new Ray(cam.getLocation(), worldCoords.subtractLocal(cam.getLocation()));
          pr.clear();
          rootNode.findPick(mouseRay, results);
          Vector3f[] vertex = new Vector3f[3];
          Vector3f loc = loc = new Vector3f();
          
          //this is taken from http://www.jmonkeyengine.com/jmeforum/index.php?topic=2410.msg18258#msg18258
          if (results.getNumber() > 0) {
            TriMesh mesh = (TriMesh) results.getPickData(0).getTargetMesh();
            
            for (int j = 0; j < mesh.getTriangleQuantity(); j++) {
               mesh.getTriangle(j, vertex);
               if (hit = mouseRay.intersectWhere(vertex[0].addLocal(mesh.getWorldTranslation()),
                                     vertex[1].addLocal(mesh.getWorldTranslation()),
                                     vertex[2].addLocal(mesh.getWorldTranslation()), loc))
                              break;
                      }
         }
        }
    }



and the output:


Screen pos: 271.0 165.0
World pos: -5.2505794 2.9160218 2.5111568



but the mouse pointer is on the ground, that is z=0!, so, why I get 2.9160218 ? have put that box just for a proof, infact the code of the box is:


      b = new Box("Box", new Vector3f(0f, 0f, 0f), new Vector3f(1f, 1f, 1f));      
      scene.attachChild(b);




this is the XML of the room:



<?xml version="1.0" encoding="UTF-8" ?>
<scene>
        <node name="Plane" translation="0 0 0" rotation="-0.7071068 0 0 0.7071068" scale="3 2 1">
            <mesh name="Plane.nocull">
                <vertex data="1 1 0  -1 1 0  -1 -1 0  1 -1 0  1 1 0  1 -1 0  1 -1 3  1 1 3  1 1 0  1 1 0  1 1 3  -1 1 3  -1 1 0  1 1 0  "/>
                <normal data="-0.5773492 -0.5773492 0.5773492  0 -0.7070833 0.7070833  0 0 1  -0.7070833 0 0.7070833  -0.5773492 -0.5773492 0.5773492  -0.7070833 0 0.7070833  -1 0 0  -0.7070833 -0.7070833 0  -0.5773492 -0.5773492 0.5773492  -0.5773492 -0.5773492 0.5773492  -0.7070833 -0.7070833 0  0 -1 0  0 -0.7070833 0.7070833  -0.5773492 -0.5773492 0.5773492  "/>
                <texturecoords data="0.5981508 0.3965699  0.0478456 0.39657  0.0478456 0.0296998  0.5981508 0.0296997  0.6654687 0.3373605  0.6661217 0.7285731  0.9595312 0.7280834  0.9588782 0.3368707  0.6654687 0.3373605  0.5624372 0.7280658  0.5624372 0.9873777  0.0438131 0.9873777  0.0438131 0.7280658  0.5624372 0.7280658  "/>
                <index data="0 1 2 2 3 0 4 5 6 6 7 8 9 10 11 11 12 13"/>
                <materialstate diffuse="0.8 0.8 0.8 1" ambient="0 0 0 1" emissive="0 0 0 1" specular="0.68616 0.68616 0.68616 1" shiny="12.5244618" alpha="1"/>
                <texturestate  >
                    <texture file="C:Documents and SettingsAdministratorDocumentiModelliProjectuv_01.jpg" texnum="0" wrap="4"/>
                </texturestate>
            </mesh>
        </node>
</scene>



I wrote a lot of stuff, I hope you're not scared :)

thanks in advance

Nice screenshot!  :-o



I particularly like the floor texture and door…it all makes that box look particularly sad without texture though. :o



darkfrog

thanks. the box has put there just to be sure that the floor is in z=0. no need a texture for this  XD

oh my… what a stupid!



my code now is:



if (results.getNumber() > 0) {
        for(int i=0; i<results.getNumber(); i++) {
           TriMesh mesh = (TriMesh) results.getPickData(i).getTargetMesh();            
      for (int j = 0; j < mesh.getTriangleQuantity(); j++) {
                   mesh.getTriangle(j, vertex);
                    if (hit = mouseRay.intersectWhere(vertex[0].addLocal(mesh.getWorldTranslation()),
                               vertex[1].addLocal(mesh.getWorldTranslation()),
                               vertex[2].addLocal(mesh.getWorldTranslation()), loc)) {                           
                          System.out.println("hit pos: " + loc.x + " " + loc.y + " " + loc.z);
                           break;
                       }
                }
            }
}



and it works pefectly if I click the box, but when I click the room I get no "hit pos" message, there is no intersection then... but why??

hmm, thinking out loud here, but is the winding proper on your floor?  As a quick test, try throwing in a cullstate on your rootnode that culls front faces.  It should make the floor invisible and allow you to see through to the boxes inner faces.

renanse said:

hmm, thinking out loud here,


am sorry for this, but it's really all the day I'm trying and retrying... it's something more than thinking... sorry.

renanse said:

As a quick test, try throwing in a cullstate on your rootnode that culls front faces.


err... excuse me?  :// what do you practically mean with "throw in a cullstate" ?


edit: btw, I only need to know the x,z coordinates of the floor in the point where I click. maybe Intersection is not useful to?

You misunderstand me.  I said "thinking out loud here" in regards to myself…  I merely meant that what I was about to say was just a quick thought and not something I had spent time thinking through.



By "throw in a cullstate" I mean to create a cullstate and set it on your rootnode to test if your triangles are wound in the proper direction on the plane you created.  They should be wound (in other words, the order of the indices specifying a triangle) in counter clockwise order if you were facing the triangles front.  If they are not, the cullstate will illustrate this by culling the opposite faces than those you expect.  Likewise, if they are not in correct order, sometimes various math operations involving triangles can give improper results.



As I said above, I may be following a completely incorrect train of thought though.