TestTrianglePicking only works sometime for issueing movement orders

Hello,



i'm new to JME and atthe moment familiarizing with it.

I read some tutorials and amreading this forum for a bit of time.



I started out with the TestTrianglePick to issue Movement orders to spheres on a Quad.

(left cklick select, right click move to picked location)



What happens is, that sometimes it the sphere tells me that location is reached,while the sphere is actually not there at all.



Basicly the code is identical to TestTrianglePick and i added the movement code for Terrainplaces i found around here:


 protected void simpleUpdate() {
 // Get the mouse input device from the jME mouse
        // Is button 0 down? Button 0 is left click
        if (MouseInput.get().isButtonDown(0)) {
            Vector2f screenPos = new Vector2f();
            // Get the position that the mouse is pointing to
            screenPos.set(am.getHotSpotPosition().x, am.getHotSpotPosition().y);
            // Get the world location of that X,Y value
            Vector3f worldCoords = display.getWorldCoordinates(screenPos, 0);
            Vector3f worldCoords2 = display.getWorldCoordinates(screenPos, 1);
            System.out.println( worldCoords.toString() );
            // Create a ray starting from the camera, and going in the direction
            // of the mouse's location
            Ray mouseRay = new Ray(worldCoords, worldCoords2.subtractLocal(worldCoords).normalizeLocal());
            // Does the mouse's ray intersect the box's world bounds?
            pr.clear();
            rootNode.findPick(mouseRay, pr);
                       
            for (int i = 0; i < pr.getNumber(); i++) {
               PickData pd =pr.getPickData(i);
               if(pd.getTargetMesh().getName().startsWith("ARO")){
                  //we chose the sphere and set it the active tomove entity
                  pd.getTargetMesh().setRandomColors();
                  activeToMove=pd.getTargetMesh();                  
               }               
            }
        }
        if (MouseInput.get().isButtonDown(1)){ //right mouse button
           //issue the move order          
           float newX=(am.getHotSpotPosition().x);
           float newY=(am.getHotSpotPosition().y);
           issueMovementOrder(activeToMove, newX, newY);
        }
       
        commitMove();
    }

 /**
    * method to actually commit all movements of all entitys
    */
   private void commitMove() {
      List<SpatialMove> movementList=new ArrayList<SpatialMove>();
      movementList.addAll(movementOrders.values());      
      for(int i=0;i<movementList.size();i++){
         SpatialMove order =movementList.get(i);
         if(order.move()){
            movementOrders.remove(order.getSpatial().getName());
         }
      }         
   }

public void issueMovementOrder(Spatial spatial,float newX, float newY) {
      System.out.println("Issueing movementOrder for: "+spatial+" from: "+spatial.getWorldTranslation()+" to x:"+newX+" y:"+newY);
       Vector2f screenPos = new Vector2f(newX, newY);
       Vector3f startPoint = DisplaySystem.getDisplaySystem().getWorldCoordinates(screenPos, 0);
       Vector3f endPoint = DisplaySystem.getDisplaySystem().getWorldCoordinates(screenPos, 1);
       Ray ray = new Ray(startPoint, endPoint.subtract(startPoint));
      
       gotoPick = new TrianglePickResults();
       gotoPick.setCheckDistance(true);
       b.findPick(ray, gotoPick);
      
       Vector3f loc = new Vector3f();
       Vector3f[] vertex = new Vector3f[3];
       boolean foundMeshHit = false;
       if(gotoPick.getNumber() > 0) {
         TriMesh mesh = (TriMesh) gotoPick.getPickData(0).getTargetMesh();
         
         for(int j=0; j<mesh.getTriangleCount(); j++) {
             mesh.getTriangle(j, vertex);
             foundMeshHit = (ray.intersectWhere(
                vertex[0].addLocal(mesh.getWorldTranslation()),
                vertex[1].addLocal(mesh.getWorldTranslation()),
                vertex[2].addLocal(mesh.getWorldTranslation()), loc));            
             if(foundMeshHit) {
                //translate your object to loc
                SpatialMove order = new SpatialMove(spatial,loc);
                System.out.println("Issueing movementOrder for: "+spatial+" from: "+spatial.getLocalTranslation()+" to :"+loc);
                movementOrders.put(spatial.getName(),order);
             }
         }
       }   
    }

/** class to store the spatial and its move target as well as commit the moving
**/
private class SpatialMove{
       Spatial spatial;       
      Vector3f destination;       
       
      /**
       * constructor for SpatialMove
       * @param destination
       * @param spatial
       */
      public SpatialMove( Spatial spatial,Vector3f destination) {
         super();
         this.destination = destination;
         this.spatial = spatial;
      }      
       /**
        * method to move the designated spatial to the destination
        * @param destination
        */
       public boolean move(){          
          boolean ready=false;
          if(!destinationReached(spatial.getLocalTranslation(),destination)){
             //spatial not yet there -> move it
                Vector3f unit2Destination = new Vector3f();
             unit2Destination=destination.subtract(spatial.getLocalTranslation());         
             System.out.println("Moving "+spatial+"to "+destination+". now: "+spatial.getLocalTranslation());
             spatial.setLocalTranslation(spatial.getLocalTranslation().add(unit2Destination.mult(tpf)));//
          }else{
             //destination reached
             System.out.println("Destination: "+destination+" reached" );
             ready=true;             
          }
          return ready;          
      }
      /**
       * method to calc if destination is to be considered reached because movement isn't that precise
       * @param is
       * @param target
       * @return
       */
      private boolean destinationReached(Vector3f is,Vector3f target){
         /**
          * when coordinates difference is smaller than eps-> coordinate considered reached
          */
         float eps =0.2f;
         
         float x =is.x-target.x;
         float y =is.y-target.y;
         float z =is.z-target.z;
         if(x<eps&&y<eps&&z<eps){
            return true;
         }
         return false;
      }
....getters and setters for destination and Spatial



As you can see at selection a sphere is set as active and on right click a move order is added.
Every update all movements in the order HashMap are processed
SpatialMove therby combines a spatialand its destination for movement

Most of the timemovement works,but some destinations dont.

Thanks in advance,still trying to learn the concepts

Hi, i got a problem.

If I use an instance of Terainpage for my terain. The object which needs to be moved moves close to the clicked location but is floating above the terain location which i clicked. If I use an instance of Terainblock the object will not move a bit. Any idea how to solve that?



Thanks for help



Edregol