Hi Bruno,
 
I've been working on a geometry editing program for a while now, and part of it includes simple point picking.  You have to do a bit of work to set it up.  You have to set the picking capabilities of the geometries you want to be able to pick.  Also, you have to enable pick reporting for any nodes you want to be able to recover from a pick operation.  What happens is that the mouse event is used to generate a pick shape - usually a ray that goes "into" the canvas.  All shapes that intersect this ray are returned in the pickResult.  This pickResult is then used to get a pickIntersection, which gives the coordinates and other information.  The coordinates returned are not screen coordinates as returned by the mouseEvent, but instead virtual world coordinates from within your Canvas3D.
 
Here is a code segment from my program.  My program also recovers the transform3D and shape for the point, but I have removed that code for simplicity. This method is called from processStimulus, and the class extends Behavior.  If you are using an indexed geometry, you first have to get the pickIntersection (pi) object, then you can use either pi.getClosestVertexCoordinates() to get the (x,y,z) coordinates directly, or you can use pi.getClosestVertexIndex().  Note that getClosestVertexIndex() returns the index of that vertex in the selected primitive - in an IndexedQuadArray it returns a number from 0 to 3.  To get the real index in your geometry, see the final line of this sample.
 
Good luck.  Picking can be annoying, but when it works, it's great!
 
Chris
Oceanic Consulting Corporation
 
private void processPickPoint (AWTEvent event) {
        // set the pick location to the location of the click
        if (event instanceof MouseEvent)
            pickCanvas.setShapeLocation((MouseEvent)event);
        else 
            return;
        Point3d eyePos = pickCanvas.getStartPosition ();
        pickResult = pickCanvas.pickAllSorted();
 
        if (pickResult != null) {
            // Get closest intersection results
            PickIntersection pi = pickResult[0].getClosestIntersection(eyePos);
            
            // Position selected point at closest vertex
            Point3d clickPt = pi.getClosestVertexCoordinates();
            pointArray.setCoordinate(0,clickPt);
            pointArray.setValidVertexCount(1);
 
            // set index of selection in this indexed quad array           
            pointIndex = pi.getPrimitiveCoordinateIndices()[pi.getClosestVertexIndex()];
        }
 }
 
 
-----Original Message-----
From: Bruno Sousa Caiado [mailto:[EMAIL PROTECTED]]
Sent: Thursday, July 20, 2000 11:38 AM
To: [EMAIL PROTECTED]
Subject: [JAVA3D] Picking - all help is appreciated

Hello.
I�m sorry for the "desperate" subject but i�m loosing my mind with this problem...
 
What i�m trying to do is to find out the (x,y,z) of a point when i click a mouse button
 
Finding out x and y is easy with the MouseEvent class.My problem is knowing the z value!
If the mouse is clicked on top of an object that�s has z=10 in that point how can i find out this value?
 
All help is appreciated....
 
Thank you and keep up the good work
 
Bruno Caiado

Reply via email to