How to move an Object towards a MousePick on Terain

Hi,

unlike the Topic http://www.jmonkeyengine.com/forum/index.php?topic=12466.0 I want to move the Object to the position like its walking there. So it shall not be set there immediately.

Is there a way to do this like : Objekt.move (location,speed) ?

Or do I need to programm this by myself? If i need to do this by myself, how should I do it?



Thanks for Help.



Edregol

Okay I got something



Vector3f loc = new Vector3f();
      if (MouseInput.get().isButtonDown(0))
      {
         Vector2f screenPos = new Vector2f();
         screenPos.set(am.getHotSpotPosition().x, am.getHotSpotPosition().y);
         Vector3f worldCoords = display.getWorldCoordinates(screenPos, 0);
         Vector3f worldCoords2 = display.getWorldCoordinates(screenPos, 1);
         Ray mouseRay = new Ray(worldCoords, worldCoords2.subtractLocal(worldCoords).normalizeLocal());
         pr.clear();
         rootNode.findPick(mouseRay, pr);
         Vector3f[] vertex = new Vector3f[3];
         boolean foundMeshHit = false;
         if (pr.getNumber() > 0)
         {
            TriMesh mesh = (TriMesh) pr.getPickData(0).getTargetMesh();

            for (int j = 0; j < mesh.getTriangleCount(); j++)
            {
               mesh.getTriangle(j, vertex);
               foundMeshHit = (mouseRay.intersectWhere(vertex[0].addLocal(mesh.getWorldTranslation()), vertex[1]
                     .addLocal(mesh.getWorldTranslation()), vertex[2].addLocal(mesh.getWorldTranslation()), loc));
               if (foundMeshHit)
               {
                  loc.y = terrain.getHeight(player.getLocalTranslation())
                  + ((BoundingBox) player.getWorldBound()).yExtent;
                  player.lookAt(loc, Vector3f.UNIT_Y);
                  
               }
            }
         }
      }
      Vector3f pos = player.getLocalTranslation();
      Vector3f velocity = loc.mult(tpf);
      pos = pos.add(velocity);
      player.setLocalTranslation(pos);


pr is an instance of TrianglePickResults. This code is handled in the update method. Unfortunately the location of my mousepick seem not to be correct. It doesnt matter where I'm clicking, the object will not move to the clicked location. Instead its moving straight away and will not be seen anymore. player is a Node, not the Spatial itself. But I think this is not the problem.

My terain which I click on is an instance of TerrainPage. Could this be a problem?
Any suggestions?

Thanks for the Help.

Edregol

Hi there,

I found a thread which worke fine for me.

http://www.jmonkeyengine.com/forum/index.php?topic=11911.msg88899#msg88899

He got a problem when the move order is issued too often. But, as I sayed, it worked fine for me.



greetings



Edregol

I also wanted to know how to do this for a strategy game. I can’t find the TrianglePickResults, is this in JME3?

See jme3test.collision.TestMousePick

1 Like