Azurlake
Azurlake

Reputation: 632

Java3D transforming lines

I am developing a small application that renders spheres in a Java 3D SimpleUniverse, and then, connects some of the spheres with lines. Creating the spheres is working, also creating lines. Trouble comes when involving movement in the recipe. Moving spheres is quite easy using Transform3D objects and giving new coordinates. But, I want to update the lines connecting two distant spheres, and it's important to mention that both spheres may have moved an arbitrary amount of space in any direction, and calculations are updated dozens of times per second, and they keep updating for a rather long time (more than 5 minutes). Is there any simple way to update line coordinates in Java3D (LineArray objects I am using)? Here is some part of the code I am working on right now:

TransformGroup [] segments;

public void createLines(SphereSet sphereSet, BranchGroup branchGroup) {
    segments = new TransformGroup[sphereSet.getEdgeSet().size()]; //Each edge connects two spheres in the sphereSet.
    Point3f [] source_dest = new Point3f[2];
    int lineIndex = 0;
    for (Edge e : sphereSet.getEdgeSet()) {
        segments[lineIndex] = new TransformGroup();
        segments[lineIndex].setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
/**
 *  Now create the Point3f pair that represents a segment connecting two spheres.
 */
        source_dest[0] = new Point3f();
        source_dest[1] = new Point3f();
        source_dest[0].setX(e.getSource().getCoordinates()[0]);
        source_dest[0].setY(e.getSource().getCoordinates()[1]);
        source_dest[0].setZ(e.getSource().getCoordinates()[2]);
        source_dest[1].setX(e.getTarget().getCoordinates()[0]);
        source_dest[1].setY(e.getTarget().getCoordinates()[1]);
        source_dest[1].setZ(e.getTarget().getCoordinates()[2]);

        LineArray line = new LineArray(2, LineArray.COORDINATES);
        line.setCoordinates(0, source_dest);

        Appearance lineApp = new Appearance();
        LineAttributes lineAttr = new LineAttributes(2, LineAttributes.PATTERN_SOLID, true);
        lineApp.setLineAttributes(lineAttr);

        Shape3D lineShape = new Shape3D(line, lineApp);
        segments[lineIndex].addChild(lineShape);

        branchGroup.addChild(segments[lineIndex]);

        lineIndex++;
    }
}

//Now, spheres' coordinates are updated... and we need to update lines' coordinates.

public void updateLines(SphereSet sphereSet) {

    int segmentIndex = 0;
    for (Edge e : sphereSet.getEdgeSet()) {
        //MYSTERIOUS CODE GOES HERE
        segmentIndex++;
    }
}

Thanks in advance. P.S.: Maybe I need to do it via a transform Matrix. In such case, advise on how to calculate it will be very helpful. Also in such case, I'd like to know if after an arbitrary big nuber of iterations, due to loss of precission, lines' ends are likely to not match the spheres' centers.

Upvotes: 0

Views: 455

Answers (1)

tnorgd
tnorgd

Reputation: 1602

1) Create a class derived from javax.media.j3d.Behavior, e.g.:

public class MyBehavior extends Behavior {

    private WakeupCondition wc = new WakeupOnElapsedTime(100); // Every 0.1 sec.

    public void initialize() {

      wakeupOn(wc);
    }

    public void processStimulus(Enumeration criteria) {

      double[] p = { 0, 0, 0 };
      for(int i = 0;i < nLinePoints;i++) {
          line.getCoordinate(i,p);
          p[0] += RandGenerator.randUniform(-0.1,0.1);
          p[1] += RandGenerator.randUniform(-0.1,0.1);
          p[2] += RandGenerator.randUniform(-0.1,0.1);
          line.setCoordinate(i,p);
      }
      wakeupOn(wc);
    }
}

2) In your createLines() method, allow coordinates to be read and written:

line.setCapability(LineArray.ALLOW_COORDINATE_READ);
line.setCapability(LineArray.ALLOW_COORDINATE_WRITE);

3) Attach the behavior to your scene graph:

MyBehavior randomEffect = new MyBehavior();
randomEffect.setSchedulingBounds(new BoundingSphere(new Point3d(0.0, 0.0,0.0), 100.0));
rootBranchGroup.addChild(randomEffect);

BoundingSphere defines a subspace where the behavior is actually executed

Upvotes: 1

Related Questions