Hi,

  You can use the new API in v1.3

  Behavior setSchedulingInterval(interval)

to use 2 behaviors instead of one without jitters.

See attachment for an example.

- Kelvin
---------------
Java 3D Team
Sun Microsystems Inc.


>Date: Thu, 11 Apr 2002 14:37:30 -0600
>From: SUBSCRIBE JAVA3D-INTEREST Anonymous <[EMAIL PROTECTED]>
>Subject: [JAVA3D] URGENT : question on processStimulus method
>To: [EMAIL PROTECTED]
>Delivered-to: [EMAIL PROTECTED]
>Delivered-to: [EMAIL PROTECTED]
>
>Hi,
>
>I have a behaviour and during the processStimulus method I change the position
of the camera (and the position of an avatar attached under the camera's
transformGroup), the LinesStripArray geometry created BY_REF of a shape3D and I
notice that the update of camera position and the shape3D is not synchronized.
>
>Is this normal ?
>
>How can I synchronize multiple updates on differents objects on screen using a
behaviour ?
>
>Thanks in advance !
>
>===========================================================================
>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".

/*
 *
 *    "@(#)BehaviorIntervalTest.java 1.1 01/06/04"
 *
 * Copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 *
 * 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.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;

public class BehaviorIntervalTest extends Applet implements ItemListener {
    CameraFollowBehavior camera;
    RotationInterpolator rotator;
    Choice rotationChoiceBox = new Choice();
    Choice cameraChoiceBox = new Choice();


    public BranchGroup createSceneGraph(SimpleUniverse u) {
        // Create the root of the branch graph
        BranchGroup objRoot = new BranchGroup();

        // Create the transform group node and initialize it to the
        // identity.  Enable the TRANSFORM_WRITE capability so that
        // our behavior code can modify it at runtime.  Add it to the
        // root of the subgraph.
        TransformGroup objTrans = new TransformGroup();
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot.addChild(objTrans);
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

        // Create a simple shape leaf node, add it to the scene graph.
        objTrans.addChild(new ColorCube(0.4));

        // Create a new Behavior object that will perform the desired
        // operation on the specified transform object and add it into
        // the scene graph.
        Transform3D yAxis = new Transform3D();
        Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
                                        0, 0,
                                        4000, 0, 0,
                                        0, 0, 0);

        rotator =  new RotationInterpolator(rotationAlpha, objTrans, yAxis,
                                            0.0f, (float) Math.PI*2.0f);
        BoundingSphere bounds =
            new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
        rotator.setSchedulingBounds(bounds);
        rotator.setSchedulingInterval(5);
        objTrans.addChild(rotator);

        camera = new CameraFollowBehavior(objTrans,
                                          
u.getViewingPlatform().getMultiTransformGroup().getTransformGroup(0));
        // set this to run AFTER rotation intepolator
        camera.setSchedulingInterval(6);

        camera.setSchedulingBounds(bounds);
        objTrans.addChild(camera);

        // Have Java 3D perform optimizations on this scene graph.
        objRoot.compile();

        return objRoot;
    }

    public BehaviorIntervalTest() {
        setLayout(new BorderLayout());
        GraphicsConfiguration config =
           SimpleUniverse.getPreferredConfiguration();

        Canvas3D c = new Canvas3D(config);
        add("Center", c);

        Panel intervalPanel = new Panel();

        for (int i=0; i < Behavior.getNumSchedulingIntervals(); i++) {
            rotationChoiceBox.add("" + i);
            cameraChoiceBox.add("" + i);
        }
        rotationChoiceBox.addItemListener(this);
        cameraChoiceBox.addItemListener(this);

        intervalPanel.setLayout(new FlowLayout());
        intervalPanel.add(new Label("Rotation behavior"));
        intervalPanel.add(rotationChoiceBox);
        intervalPanel.add(new Label("Camera follow behavior"));
        intervalPanel.add(cameraChoiceBox);
        add("South", intervalPanel);
        add("North", new Label("Behavior scheduling interval test"));

        SimpleUniverse u = new SimpleUniverse(c);
        // Create a simple scene and attach it to the virtual universe
        BranchGroup scene = createSceneGraph(u);

        // This will move the ViewPlatform back a bit so the
        // objects in the scene can be viewed.
        u.getViewingPlatform().setNominalViewingTransform();

        u.addBranchGraph(scene);

        rotationChoiceBox.select(rotator.getSchedulingInterval());
        cameraChoiceBox.select(camera.getSchedulingInterval());
    }

    public void itemStateChanged(ItemEvent e) {

        int interval = Integer.parseInt((String) e.getItem());

        if (e.getSource() == rotationChoiceBox) {
            rotator.setSchedulingInterval(interval);
        } else {
            camera.setSchedulingInterval(interval);
        }
    }

    //
    // The following allows BehaviorIntervalTest to be run as an application
    // as well as an applet
    //
    public static void main(String[] args) {
        System.out.println("This example demonstrate a camera chasing a rotating 
cube.");
        System.out.println("You will see the cube will jitter if rotation interpolator 
behavior scheduling interval level is same or larger than camera behavior.\n");
        new MainFrame(new BehaviorIntervalTest(), 512, 512);
    }
}
/*
 *
 *    "@(#)CameraFollowBehavior.java 1.1 01/06/04"
 *
 * Copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 *
 * 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.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */


import javax.vecmath.*;
import javax.media.j3d.*;
import java.util.*;

public class CameraFollowBehavior extends Behavior
{
    WakeupCondition wakeup = new WakeupOnElapsedFrames(0);
    TransformGroup rotTarget;
    TransformGroup camera;
    Transform3D trans = new Transform3D();
    Matrix4d zTrans = new Matrix4d();
    Matrix4d mat = new Matrix4d();

    public CameraFollowBehavior(TransformGroup rotTarget, TransformGroup camera) {
        this.rotTarget = rotTarget;
        this.camera = camera;

    }

    public void initialize()  {
        mat.setIdentity();
        zTrans.setIdentity();
        zTrans.setTranslation(new Vector3d(0, 0, 2));
        wakeupOn(wakeup);
    }

    public void processStimulus(Enumeration criteria) {

        if ((camera != null) && (rotTarget != null)) {
            rotTarget.getTransform(trans);
            trans.get(mat);
            mat.mul(zTrans);
            trans.set(mat);
            camera.setTransform(trans);
        }
        wakeupOn(wakeup);
    }
}

Reply via email to