java3d viewplatform_Attach an object to the view platform in Java3D

In my Java3D application, I have a movable viewing platform (thanks to "OrbitBehavior" and "KeyNavigatorBehavior" behaviors for example) so I can change my point of view to the scene.

Now, I would like to add an object which is "static" over my view, i.e. always viewed the same way when I move my view (for example, like the gun of the player in a FPS).

By now, I tried the 2 following approaches to this problem, but none of them worked.

1/ Attach directly my object to the ViewingPlatform (via a dedicated BranchGroup plus a TransformGroup).

In this case, my object is simply not displayed (I'm absolutely not sure it's allowed to perform such an operation actually...). This is this part of my code:

Code : (with view the ViewingPlatform)

BranchGroup fixedBG = new BranchGroup();

TransformGroup fixedTG = new TransformGroup();

fixedTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

fixedTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

Transform3D transfo = new Transform3D();

transfo.setTranslation(new Vector3f(0.2f, 0.0f, -1.0f));

fixedTG.setTransform(transfo);

ColorCube fixedCube = new ColorCube(0.2);

fixedTG.addChild(fixedCube);

view.addChild(fixedBG);

2/ Apply to my object the same transformation (rotation + translation ; retrieved via getTransform() on getViewPlatformTransform() - I also tried something more complex here, with operations between these transformations and initial positions of view PF and object) as viewing platform's one. I do all this in real time, with a dedicated behavior (same wake-up conditions as behaviors modifying the view location).

Here, my object disappears when I move the view, and then appears again sometimes, when I use the zoom/de-zoom action of the KeyNavigator (butin a really unstable way).

I'm sure that what I'm trying to do is doable and probably quite simple, but now, I don't have a clue...

Thanks by advance for your help

(PS: As English is not my native language, I may be quite messy in explaining my problem: do not hesitate to ask me to write again un-understandable parts)

JAVA 3D开发工具包(3个版本),java3d-1_3、java3d-1_4、java3d-1_5,加一个例子。例子代码如下: import java.awt.GraphicsConfiguration; import javax.media.j3d.Alpha; import javax.media.j3d.BoundingSphere; import javax.media.j3d.BranchGroup; import javax.media.j3d.Canvas3D; import javax.media.j3d.RotationInterpolator; import javax.media.j3d.Transform3D; import javax.media.j3d.TransformGroup; import javax.swing.JApplet; import javax.vecmath.Point3d; import com.sun.j3d.utils.geometry.ColorCube; import com.sun.j3d.utils.universe.SimpleUniverse; import com.sun.j3d.utils.universe.ViewingPlatform; public class HelloUniverse extends JApplet { public BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); //新建一个变形组结点,初始化它 //变形组结点指定一个单一的空间变形,通过一个Transform3D对象, //能对它的子结点定位,定向,定尺寸 TransformGroup objTrans = new TransformGroup(); //指定结点可以写入它的对象的变形信息。这样我们的行为代码就能在运行进修改它。 objTrans.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE); //根节结中加入这个变形组结点 objRoot.addChild(objTrans); //新建一个例子Shape3D结点,并把它加入到场景图表中 objTrans.addChild(new ColorCube(0.4)); Transform3D yAxis = new Transform3D(); /* * 提供转换一个时间值到一个0到1之间的alpha值的方法 * 第一个参数loopCount:运行循环次数,-1表示无限循环 * 第二个参数increasingAlphaDuration:alpha值从0到1的时间周期 */ Alpha rotationAlpha = new Alpha(-1, 4000); //新建旋转者,它将执行变形操作 RotationInterpolator rotator = new RotationInterpolator( rotationAlpha, objTrans, yAxis, 0.0f, (float) Math.PI*2.0f); //用一个中心点和半径定义一个球状范围区域。 BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0); //设置旋转者的运行范围 rotator.setSchedulingBounds(bounds); //加它到根结点 objRoot.addChild(rotator); //执行优化 objRoot.compile(); return objRoot;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值