2012-09-17 31 views
0

我正在开发一个小应用程序,它在Java 3D SimpleUniverse中呈现球体,然后用线连接一些球体。创建球体正在工作,也创造线条。涉及配方中的运动时会出现麻烦。 使用Transform3D对象并给出新坐标,移动球体非常容易。但是,我想更新连接两个遥远领域的线条,重要的是要提及这两个领域可能已经向任意方向移动了任意数量的空间,并且计算每秒更新几十次,并且他们不断更新很长时间(超过5分钟)。 有什么简单的方法来更新Java3D(我使用的LineArray对象)的坐标? 下面是代码的一些部分,我现在很努力:Java3D转换线

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

在此先感谢。 P.S .:也许我需要通过变换矩阵来完成。在这种情况下,建议如何计算它将会非常有帮助。同样在这种情况下,我想知道如果经过任意大的迭代,由于失去精度,线的末端可能不匹配球体的中心。

回答

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定义了一个子空间e行为实际执行

+0

谢谢。这确实比我所寻找的要多。测试和工作:) 真正帮助我的是,您可以直接转换行而无需转换它们所在的容器(TransformGroup)。设置'ALLOW_COORDINATE_READ&WRITE'就足以在我的代码中进行一些更改。谢谢! – Azurlake