I have a canvas with several 3D spheres generated from my own Sphere3d class
which itself is a BranchGroup and has a translation TransformGroup attached
to it and then a Primitive of the Sphere attached to it. I want to select
the individual spheres and tell my program which sphere it is...using the
code below I implemented the MousePickBehavior but found that I have a lot
of inconsistency in picking the objects. Sometimes I click on a sphere and
I pick it, other times I getting nothing at all...if I rotate the scene
around
then sometime I can get the sphere again. It seems like the picking is
going very well. I tried also using PRIMITIVE instead of SHAPE3D for
my 'pickResult.getNode(PickResult.SHAPE3D)' but with similar results.
Additionally I tried using the BRANCH_GROUP but I could not select anything
at all.

My questions are:
   1) Have any of you found inconsistency in your picking?
   2) How can I get the BRANCH_GROUP pickResult to work so that I can
      directly get the node instead of using User defined data attached to
      the Sphere?
   3) Do you think using a VirtualUniverse, instead of SimpleUniverse
affects
      the results? I tested this but could not find an issue.
   4) Does anyone have a better picking system/code or simply can give me
      insight on how to fix the one I have...by the way, the code below is
      entirely from the java3D demos and I only added some print lines.

thanks,

Mario




-------------------------------------------------------------------------


        PickHighlightBehavior pickBeh = new PickHighlightBehavior(canvas3D,
objRoot, bounds);



-------------------------------------------------------------------------



/*
 *      @(#)PickHighlightBehavior.java 1.15 02/04/01 15:03:24
 *
 * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any
 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
 * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that Software is not designed,licensed or intended
 * for use in the design, construction, operation or maintenance of
 * any nuclear facility.
 */

import javax.media.j3d.*;
import com.sun.j3d.utils.picking.PickTool;
import com.sun.j3d.utils.picking.PickResult;
import com.sun.j3d.utils.picking.behaviors.PickMouseBehavior;
import java.util.*;
import java.awt.*;
import java.awt.Event;
import java.awt.AWTEvent;
import java.awt.event.MouseEvent;
import javax.vecmath.*;
import com.sun.j3d.utils.picking.PickIntersection;


public class PickHighlightBehavior extends PickMouseBehavior {
  Appearance savedAppearance = null;
  Shape3D oldShape = null;
  Appearance highlightAppearance;

  public PickHighlightBehavior(Canvas3D canvas, BranchGroup root, Bounds
bounds) {
      super(canvas, root, bounds);
      this.setSchedulingBounds(bounds);
      root.addChild(this);
      Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
      Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
      Color3f highlightColor = new Color3f(0.0f, 1.0f, 0.0f);
      Material highlightMaterial = new Material(highlightColor, black,
                                                highlightColor, white,
                                                80.0f);
      highlightAppearance = new Appearance();
      highlightAppearance.setMaterial(new Material(highlightColor, black,
                                                   highlightColor, white,
                                                   80.0f));

      pickCanvas.setMode(PickTool.BOUNDS);
  }

    public void updateScene(int xpos, int ypos) {
        PickResult pickResult = null;
        Shape3D shape = null;

        pickCanvas.setShapeLocation(xpos, ypos);

        Point3d eyePos = pickCanvas.getStartPosition();
        System.out.println("EYE: "+eyePos);


        pickResult = pickCanvas.pickClosest();


        if (pickResult != null) {
            shape = (Shape3D) pickResult.getNode(PickResult.SHAPE3D);
          System.out.println("     Shape:  "+shape);
        }


        if (oldShape != null){
            oldShape.setAppearance(savedAppearance);
        }
        try {
          if (shape != null) {
            savedAppearance = shape.getAppearance();
            oldShape = shape;

            shape.setAppearance(highlightAppearance);
          }
        } catch ( Exception e ) { System.out.println(" Caught E ");}

    }
}


-------------------------------------------------------------------------











   Mario

Mariusz Zaczek
NASA - Johnson Space Center
Automated Vehicles and Orbit Analysis / DM35
Flight Design and Dynamics Division
Mission Operations Directorate
Bldg: 30A     Room: 3040A

Disclaimer: "The opinions, observations and comments expressed in my email
             are strictly my own and do not necessarily reflect those of
NASA."

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to