5 Replies Latest reply: Sep 6, 2011 7:55 AM by rukbat RSS

    setFactor by translating - JAVA 3D - PickMouseBehavior

    843799
      Hello,

      Does anyone know how to overwrite PickMouseBehavior or PickTranslateBehavior to use setFactor();
      my 3D objects are moving to fast on my canvas3D.

      There is an example on : http://deven3d.free.fr/java3d/chap07.htm
      but they also change the mouse click from right click to left click .. .and I don't need that option... And they overwrite MouseBehavior, not PickMouseBehavior.

      my Behavior classes goes like this :

      public class MyPickCallback extends PickMouseBehavior implements PickingCallback, MouseListener {
      public void transformChanged(int type, TransformGroup tg) {}
      }

      I'd like to owerwrite PickMouseBehavior as a way to be able to use the setFactor Method from MouseTranslate...
      or can I some how slow down x and y in the transformChanged method?

      Kind Regards, vys

      Edited by: svy on Sep 26, 2008 5:36 AM
        • 1. Re: setFactor by translating - JAVA 3D - PickMouseBehavior
          843799
          Hi, probably little late but hope it helps someone in the future.

          I m quite new to java3d so forgive any overlookings
          Regarding overiding PickMouseBehavior, the following code, implements a custom pickTranslateBehaviour for moving objects on all three axis (by using MouseTranslate and MouseZoom behaviours), also providing the option to use setFactor().
          import javax.media.j3d.Bounds;
          import javax.media.j3d.BranchGroup;
          import javax.media.j3d.Canvas3D;
          import javax.media.j3d.Transform3D;
          import javax.media.j3d.TransformGroup;
          
          import com.sun.j3d.utils.picking.behaviors.PickMouseBehavior;
          
          
          import com.sun.j3d.utils.behaviors.mouse.MouseBehaviorCallback;
          import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
          import com.sun.j3d.utils.picking.PickResult;
          import com.sun.j3d.utils.picking.PickTool;
          import com.sun.j3d.utils.picking.behaviors.PickingCallback;
          import com.sun.j3d.utils.behaviors.mouse.MouseBehavior;
          import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
          
          /**
           * A mouse behavior that allows user to pick and translate scene graph objects.
           * Common usage: 1. Create your scene graph. 2. Create this behavior with
           * the root and canvas. See PickRotateBehavior for more details.
           */
          
          public class CustomPickTranslateBehavior extends PickMouseBehavior implements MouseBehaviorCallback {
               
              MouseTranslate translate;
              MouseZoom zoom;
               private PickingCallback callback = null;
               private TransformGroup currentTG;
          
          
          
          
               /**
                * Creates a pick/translate behavior that waits for user mouse events for
                * the scene graph.
                * @param root   Root of your scene graph.
                * @param canvas Java 3D drawing canvas.
                * @param bounds Bounds of your scene.
                **/
          
               public CustomPickTranslateBehavior(BranchGroup root, Canvas3D canvas, Bounds bounds){
                    super(canvas, root, bounds);
                    translate = new MouseTranslate(MouseBehavior.MANUAL_WAKEUP);
                    
                  // constant setup of scaling Factor, the second constructor offers this capablitity to calling methods
                  translate.setFactor(1);
                    translate.setTransformGroup(currGrp);
                    currGrp.addChild(translate);
                    translate.setSchedulingBounds(bounds);
                    this.setSchedulingBounds(bounds);
                  
                  
                  //code for mouseZoom behavior can be ommited if only interested on x,y azis movement
                  zoom = new MouseZoom(MouseBehavior.MANUAL_WAKEUP);
              zoom.setTransformGroup(currGrp);
              currGrp.addChild(zoom);
              zoom.setSchedulingBounds(bounds);
               }
          
               /**
                * Creates a pick/translate behavior that waits for user mouse events for
                * the scene graph with the addition of a scaling factor.
                * @param root   Root of your scene graph.
                * @param canvas Java 3D drawing canvas.
                * @param bounds Bounds of your scene.
               * @param factor Factor for scaling
                * @param pickMode specifys PickTool.BOUNDS, PickTool.GEOMETRY or
                * PickTool.GEOMETRY_INTERSECT_INFO.
                * @see PickTool#setMode
                **/
               public CustomPickTranslateBehavior(BranchGroup root, Canvas3D canvas,
                         Bounds bounds,float factor, int pickMode) {
                    super(canvas, root, bounds);
                    translate = new MouseTranslate(MouseBehavior.MANUAL_WAKEUP);
                    translate.setFactor(factor);
                    translate.setTransformGroup(currGrp);
                    currGrp.addChild(translate);
                    translate.setSchedulingBounds(bounds);
                    this.setSchedulingBounds(bounds);
                 this.setMode(PickTool.BOUNDS);
          
                       //code for mouseZoom behavior can be ommited if only interested on x,y azis movement
          
                 zoom = new MouseZoom(MouseBehavior.MANUAL_WAKEUP);
              zoom.setTransformGroup(currGrp);
              currGrp.addChild(zoom);
              zoom.setFactor(factor);
              zoom.setSchedulingBounds(bounds);
          
          
               }
          
          
          
               /**
                * Update the scene to manipulate any nodes. This is not meant to be
                * called by users. Behavior automatically calls this. You can call
                * this only if you know what you are doing.
                *
                * @param xpos Current mouse X pos.
                * @param ypos Current mouse Y pos.
                **/
               public void updateScene(int xpos, int ypos){
          //          System.err.println("update scene pick translate");
                    TransformGroup tg = null;
          
                  
                  //Change this to define which mouse buttons implement the behavior, here it is right mouse button draged
                    if (!mevent.isAltDown() && mevent.isMetaDown()){
          
                         pickCanvas.setShapeLocation(xpos, ypos);
                         PickResult pr = pickCanvas.pickClosest();
                         if ((pr != null) &&
                                   ((tg = (TransformGroup)pr.getNode(PickResult.TRANSFORM_GROUP))
                                             != null) &&
                                             (tg.getCapability(TransformGroup.ALLOW_TRANSFORM_READ)) &&
                                             (tg.getCapability(TransformGroup.ALLOW_TRANSFORM_WRITE))){
          
          
          
                              translate.setTransformGroup(tg);
                              translate.wakeup();
                              currentTG = tg;
                              // Need to clean up Issue 123 --- Chien
                              // freePickResult(pr);
                         } else if (callback!=null)
                              callback.transformChanged( PickingCallback.NO_PICK, null );
                    }
          
                  
                  //this affects the mouseZoomBehavior for movement on the 3rd axis, by holding the middle mouse button
                  else if (mevent.isAltDown() && !mevent.isMetaDown()){
          
                pickCanvas.setShapeLocation(xpos, ypos);
                PickResult pr = pickCanvas.pickClosest();
                if ((pr != null) &&
                    ((tg = (TransformGroup)pr.getNode(PickResult.TRANSFORM_GROUP))
                                                  != null) &&
                 (tg.getCapability(TransformGroup.ALLOW_TRANSFORM_READ)) &&
                 (tg.getCapability(TransformGroup.ALLOW_TRANSFORM_WRITE))){
               zoom.setTransformGroup(tg);
               zoom.wakeup();
               currentTG = tg;
                  // Need to clean up Issue 123 --- Chien
               // freePickResult(pr);
                } else if (callback!=null)
                    callback.transformChanged( PickingCallback.NO_PICK, null );
              }
            }
          
          
               
          
               /**
                * Callback method from MouseTranslate
                * This is used when the Picking callback is enabled
                */
               public void transformChanged( int type, Transform3D transform ) {
                    callback.transformChanged( PickingCallback.TRANSLATE, currentTG );
               }
          
               /**
                * Register the class @param callback to be called each
                * time the picked object moves
                */
               public void setupCallback( PickingCallback callback ) {
                    this.callback = callback;
                    if (callback==null)
                         translate.setupCallback( null );
                    else
                         translate.setupCallback( this );
               }
          
          }
          Edited by: akis100 on Nov 14, 2009 2:37 AM
          • 2. Re: setFactor by translating - JAVA 3D - PickMouseBehavior
            880151
            Can you please help me out in this one. I am trying to use PickMouseBehavior but unfortunately MouseTranslate does not work.

            I have got two cylinders and i rotate them using mouserotate setfactors, but when i rotate one cylinder, the other cylinder rotates with it. Is there a way around this. I want to rotate the cylinders separately (only when i click on the cylinder to rotate it).

            Thanks.
            • 3. Re: setFactor by translating - JAVA 3D - PickMouseBehavior
              886490
              Hi,

              My solution is to create an independent controller of events.
              In each detected event to calculate the rotation and translation for the object or total universe

              I have programmed in Java3D a framework for virtual worlds:
              See web demo: www.ceintra.com
              See second web demo: www.metricworld3d.com

              This it is the code skeleton that I have programmed:

              // Class extends ''Behavior''
              public class MyNavigator extends Behavior {
                   private WakeupCriterion[ ] cOb_WakeupCriterion = null;
                   private WakeupOr cOb_WakeupOr = null;

              // Constructor
              public MyNavigator( ) throws Exception {     

                   // List events of interaction with class
                   cOb_WakeupCriterion = new WakeupCriterion[ 4 ];
                   cOb_WakeupCriterion[ 0 ] = new WakeupOnAWTEvent( KeyEvent.KEY_PRESSED );
                   cOb_WakeupCriterion[ 1 ] = new WakeupOnAWTEvent( MouseEvent.MOUSE_CLICKED );
                   cOb_WakeupCriterion[ 2 ] = new WakeupOnAWTEvent( MouseEvent.MOUSE_DRAGGED );
                   cOb_WakeupCriterion[ 3 ] = new WakeupOnAWTEvent( MouseEvent.MOUSE_RELEASED );
                   // Registered events
                   cOb_WakeupOr = new WakeupOr( cOb_WakeupCriterion );
              }

              public void processStimulus( Enumeration lOb_Enumeration ) {

                   while ( lOb_Enumeration.hasMoreElements( ) ) {

                        WakeupCriterion cOb_WakeupCriterion = ( WakeupCriterion ) lOb_Enumeration.nextElement( );

                        // Is AWTEvent ?
                        if ( cOb_WakeupCriterion instanceof WakeupOnAWTEvent ) {
                   
                             // Obtain AWT event ( is a Matrix )
                             AWTEvent[ ] lOb_AWTEvent = ( ( WakeupOnAWTEvent ) cOb_WakeupCriterion ).getAWTEvent( );
                             
                             // Iteration matrix all awt events and case event type
                             int lin_EventIndex = 0;
                             for( lin_EventIndex = 0; lin_EventIndex < lOb_AWTEvent.length; lin_EventIndex++ ) {

                                  // Evento click sobre un botón del mouse
                                            
                                  if ( lOb_AWTEvent[ lin_EventIndex ].getID( ) == MouseEvent.MOUSE_CLICKED ) {

                                       Call Other Methode on calculate the rotation/translation for the object or total universe
                                       and apply changes via Transform3D
                                  
                                  } else if ( lOb_AWTEvent[ lin_EventIndex ].getID( ) == MouseEvent.MOUSE_DRAGGED ) {
                                  
                                       Equal:
                                       Call Other Methode on calculate the rotation/translation for the object or total universe
                                       and apply changes via Transform3D
                   
                                  } etc.. KeyEvent.KEY_PRESSED, MouseEvent.MOUSE_CLICKED, MouseEvent.MOUSE_DRAGGED, MouseEvent.MOUSE_RELEASED
                                  
                             }
                        }
                   }
                   // IMPORTANT: Also registered events !!
                   this.wakeupOn( cOb_WakeupOr );
              }
              }


              Regards,
              Manel
              • 4. Re: setFactor by translating - JAVA 3D - PickMouseBehavior
                Darryl Burke
                883487 wrote:
                This it is the code ...
                Moderator advice: Please read the announcement(s) at the top of the forum listings and the FAQ linked from every page. They are there for a purpose.

                Then edit your post and format the code correctly.

                db
                • 5. Re: setFactor by translating - JAVA 3D - PickMouseBehavior
                  883487 wrote:
                  Hi,
                  This is a thread from 2008.
                  It would have been so very much better if you'd left it alone in its repose.

                  Moderator Action:
                  The thread is locked to prevent multiple resurrections.