我正在开发一个小应用程序,它在Java3D 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++;
}
}提前谢谢。附言:也许我需要通过一个变换矩阵来实现。在这种情况下,建议如何计算它将是非常有用的。同样在这种情况下,我想知道在任意大的迭代次数之后,由于精确度的损失,直线的末端可能与球体的中心不匹配。
发布于 2012-09-19 12:29:48
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定义了实际执行行为的子空间
https://stackoverflow.com/questions/12466211
复制相似问题