Drag and drop example

Hello everybody…



I would like to do a simple example of how to use drag and drop to move a simple sphere from one cube (holding the left button mouse) to another cube (releasing the button). But when releasing the button, I want to that sphere get in the center of the cube…

I dont have the minor idea of how doing this…

could someone help me, and if someone have a code example, could send it to me???



Thanks a lot…  :wink:



Guilherme  :D  :smiley:

When they let go you could simply:


mySphere.setLocalTranslation(myCube.getLocalTranslation());



If I understood the question that should work. ;)

yeah… that I understand…  :smiley:

but how I make my mouse click to drag that sphere while pressing the left mouse button, and how to know that the sphere is around the bounding box of the cube when releasing the button?

I haven't done much with the mouse clicking on 3D elements so someone else will have to help you there, but you can simply handle the mouse-click event and detect what object the click occurred on and then upon release detect what object is closest to it or what is colliding with it to determine how to "lock" it into place.

For the 'near' check you can use collision detection (intersection checks). You can assign larger bounding (and perform bounding check only) if you want to have some more tolerance.



For moving things with the mouse you'd simply change local translation accoring to mouse movement… not sure what's unclear here.

The problem is that dragging is a 2D action, so you need a way to control the 3rd dimension when dragging in a 3d view.  For example, I wrote the Arrow shape class so I could put together a drag tool that let me drag objects along a certain axis or plane.

Hi,



I'm also implementing 3D drag and drop… and have a small problem.

At the moment I can left click and drag my targePoint node object around the screen… but the object seems confined to go in only certain directions.



I basically determine if the mouse position intersects my node object and if so i translate the screen position of the mouse into world coordinates and then update the local translation of my object to the new world cordinates of the mouse.



I think the problem is related to my understanding of the Z value from the screen coordinates vector. (… maybe … ?)

I've attached my code below and would love some feedback which could point me in the right direction.



Thanks in advance!



      if(MouseInput.get().isButtonDown(0)) {

         Vector2f screenPos = new Vector2f();
         screenPos.set(MouseInput.get().getXAbsolute(), MouseInput.get().getYAbsolute());
      
                   // Create a ray starting from the camera, and going in the direction
         // of the mouse's location
         Ray mouseRay = display.getPickRay(screenPos, false, null);
   
         // Does the mouse's ray intersect the targetPoint's world bounds?
         pr.clear();
         rootNode.findPick(mouseRay, pr);

         if (targetPoint.getWorldBound().intersects(mouseRay)) {
            //drag the X target
            log.info("CLICKING on the X Target...");
            Vector3f currentScreenPos = display.getScreenCoordinates(targetPoint.getWorldTranslation());
            Vector3f newWorldPosition = display.getWorldCoordinates(screenPos, currentScreenPos.z );        //// IS THIS CORRECT??
            log.info("Moving targetPoint from " + targetPoint.getLocalTranslation()
                  + " to " + newWorldPosition);
            targetPoint.setLocalTranslation(newWorldPosition);
            targetPoint.updateWorldBound();

         }
            
      }

renanse said:

The problem is that dragging is a 2D action, so you need a way to control the 3rd dimension when dragging in a 3d view.

But how is picking solved then? The well-known way of Java3D is a Pick Beam that points from the center of the viewpane into the direction where the mouse cursor is. The Beam never ends and the system resolves all objects in this Beam to find the clicked item. If moving of a 3D object should be implemented, there should be a similar way.

Maybe a Beam pointing from the center of the viewpane through the mouse cursor until it reaches the terrain (or 'til some outer limit is reached). The moved object could be placed at the location where the Beam hits Terrain and maybe is placed as a ghosted copy of the original.

So far picking is the easy part, thanks to the helper methods already provided by jME.

The above example shows how to create a Ray from the screen coordinates of the mouse position and use this Ray to get a PickResults of all nodes that have BoundingVolumes that intersect that Ray. This is handy if you are going to iterate over a set of nodes.



The example code above doesn't really need a PickResults just yet as I'm really only concerned about one particular object. Obviously the code not really extensible as I'm just testing my algorithm for dragging a specific object.



I believe my problem is understanding how to drag the object across a specific frustrum plane.


Ok, So the above code actually worked fine with a sphere object… Therefore I assume it works for most spatials.

Though at the time I was trying to drag a Text3D object and that definitely didn't work as above.