0

我正在开发一个小型应用程序,它在 Java 3D SimpleUniverse 中渲染球体,然后用线连接一些球体。创建球体正在工作,也可以创建线条。当涉及到配方中的运动时,麻烦就来了。使用 Transform3D 对象并提供新坐标来移动球体非常容易。但是,我想更新连接两个遥远球体的线,重要的是要提到两个球体可能在任何方向上移动了任意数量的空间,并且计算每秒更新数十次,并且它们会持续更新相当长的时间长时间(超过 5 分钟)。有没有简单的方法来更新 Java3D 中的线坐标(我正在使用的 LineArray 对象)?这是我现在正在处理的代码的一部分:

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++;
    }
}

提前致谢。PS:也许我需要通过变换矩阵来做到这一点。在这种情况下,建议如何计算它会非常有帮助。同样在这种情况下,我想知道在任意大量迭代之后,是否由于精度损失,线的末端可能与球体的中心不匹配。

4

1 回答 1

1

1) 创建一个从 javax.media.j3d.Behavior 派生的类,例如:

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)在您的createLines()方法中,允许读取和写入坐标:

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

3)将行为附加到您的场景图:

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

BoundingSphere 定义了行为实际执行的子空间

于 2012-09-19T04:29:48.310 回答