我正在开发一个小应用程序,它在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++;
}
}
提前致谢。 P.S。:也许我需要通过变换矩阵来完成它。在这种情况下,建议如何计算它将是非常有帮助的。同样在这种情况下,我想知道在经过任意大的迭代后,由于精确度的损失,线的末端可能与球体的中心不匹配。
答案 0 :(得分: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定义了一个实际执行行为的子空间