I, too think that an important feature missing in java3D is the ability to
guarantee the execution order of behaviors -- something analogous to
OrderedGroup. For what it's worth, I discovered that I can affect the order
in
which behaviors are executed by the order in which I add them to the scene
graph. I have created a couple of classes that automate this process. I
haven't
tested them for behaviors that have wakeupConditions other than
wakeupOnElapsedFrames( 0 ) but they work for me and could be expanded to be
more
general purpose. This is definitely not an ideal solution and it relies on a
perhaps undependable quirk in the Java3D engine.


import javax.media.j3d.*;

public class OrderedBehaviorBranchGroup extends BranchGroup{
  BranchGroup mainBG = new BranchGroup();
  OrderedBehavior behavior = null;
  int level;//unimplemented

  protected OrderedBehaviorBranchGroup(){}

  public OrderedBehaviorBranchGroup( OrderedBehavior b, int l ){
    super();
    b.parent = this;
    behavior = b;
    level = l;

    setCapability( Group.ALLOW_CHILDREN_EXTEND );
    setCapability( Group.ALLOW_CHILDREN_WRITE );

    mainBG.setCapability( BranchGroup.ALLOW_DETACH );
    mainBG.addChild( b );
    addChild( mainBG );
  }
  public void order(){
    //make sure this behavior is added last to the sceneGraph
    mainBG.detach();
    addChild( mainBG );
  }
}

import javax.media.j3d.*;

public abstract class OrderedBehavior extends Behavior{
  OrderedBehaviorBranchGroup parent;
  boolean needsOrdering = false;
  int elapsedLoops = 0;

  public OrderedBehavior(){
    super();
  }
  final public void processStimulus( java.util.Enumeration criteria ){
    if( needsOrdering ){
      elapsedLoops++;
      if( elapsedLoops > 1 ){
        parent.order();
        needsOrdering = false;
        elapsedLoops = 0;
      }
    }
    doProcessStimulus( criteria );
  }
  public void order(){
    needsOrdering = true;
  }
  abstract public void doProcessStimulus( java.util.Enumeration criteria );
}

Clearly OrderedBehavior must be subclassed, for example:

  public class SensorBehavior extends OrderedBehavior{
    WakeupOnElapsedFrames wakeup = new WakeupOnElapsedFrames( 0 );

    public SensorBehavior(){}
    public void initialize(){
      wakeupOn( wakeup );
    }
    public void doProcessStimulus( java.util.Enumeration criteria ){
      wakeupOn( wakeup );
      doSomething();//relies on another behavior having been executed
    }
  }

It then would be used something like this:

  SensorBehavior sensorBehavior = new SensorBehavior();
  OrderedBehaviorBranchGroup orderedBehaviorBranchGroup =
      new OrderedBehaviorBranchGroup( sensorBehavior, 1 );
  sceneBG.addChild( orderedBehaviorBranchGroup );

Later on during execution of the program when an event arises that may throw

the behavior out of order, simply say

  sensorBehavior.order();

This has worked so well for me, that I have no plans to do anything else
until
it breaks or until Java3D supports ordered behaviors.

Raffi

===========================================================================
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