简体   繁体   English

如何在Java3D中旋转一组对象

[英]How to rotate a group of objects in Java3D

I'm new to Java as well as Java3D. 我是Java和Java3D的新手。 I've made a cube out of 3D spheres and now I want to rotate it like a normal cube. 我已经用3D球体制作了一个立方体,现在我想像普通立方体一样旋转它。 I mean that on a simple switch case or any other means of input the cube should rotate right, left, top, bottom. 我的意思是,在简单的开关盒或任何其他输入方式上,多维数据集应向右,向左,向上,向下旋转。 I tried using the lookAt function but that just changes the point in focus . 我尝试使用lookAt函数,但这只是改变焦点。 Any help in the matter would be much appreciated. 在这方面的任何帮助将不胜感激。 Thank you for your time. 感谢您的时间。

Here's my Code: 这是我的代码:

public LEDCube3d() 
 {
    for (float x = -.5f,xco=0; x <= .5f; x = x + 0.2f,++xco)
    {
       for(float y=-.5f,yco=0;y<.5f;y=y+0.2f,++yco)
       {     
            for(float z=-.5f,zco=0;z<.5f;z=z+0.2f,++zco)
           {
                     Sphere sphere = new Sphere(0.015f);
                     TransformGroup tg = new TransformGroup();
                     Transform3D transform = new Transform3D();
                     Vector3f vector = new Vector3f( x, y, z);
                     matrix[(int) xco][(int) yco][(int) zco]=0;       
                     transform.setTranslation(vector);
                     tg.setTransform(transform);
                     tg.addChild(sphere);
                     group.addChild(tg);
           }
     }
  }


   Color3f light1Color = new Color3f(.1f, 1.4f, .1f); 
   BoundingSphere bounds =new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
   Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
   DirectionalLight light1= new DirectionalLight(light1Color, light1Direction);
   light1.setInfluencingBounds(bounds);
   group.addChild(light1);
   universe.getViewingPlatform().setNominalViewingTransform();
   universe.addBranchGraph(group);
   System.out.println("constructor");
}

这是代码:

package examples; import javax.media.j3d.Alpha; import javax.media.j3d.BoundingSphere; import javax.media.j3d.BranchGroup; import javax.media.j3d.DirectionalLight; import javax.media.j3d.RotationInterpolator; import javax.media.j3d.Transform3D; import javax.media.j3d.TransformGroup; import javax.vecmath.Color3f; import javax.vecmath.Point3d; import javax.vecmath.Vector3f; import com.sun.j3d.utils.geometry.Sphere; import com.sun.j3d.utils.universe.SimpleUniverse; public class LEDCube3d { public static void main(final String[] args) { new LEDCube3d(); } public LEDCube3d() { final TransformGroup group = new TransformGroup(); { //to enable rotation group.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); group.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); } final SimpleUniverse universe = new SimpleUniverse(); // for (float x = -.5f, xco = 0; x <= .5f; x = x + 0.2f, ++xco) { for (float y = -.5f, yco = 0; y < .5f; y = y + 0.2f, ++yco) { for (float z = -.5f, zco = 0; z < .5f; z = z + 0.2f, ++zco) { final Sphere sphere = new Sphere(0.015f); final TransformGroup tg = new TransformGroup(); final Transform3D transform = new Transform3D(); final Vector3f vector = new Vector3f(x, y, z); transform.setTranslation(vector); tg.setTransform(transform); tg.addChild(sphere); group.addChild(tg); } } } // final Transform3D transform = new Transform3D(); transform.rotX(1); final long milis = 5000; final Alpha alpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, milis, 0, 0, 0, 0, 0); final RotationInterpolator ri = new RotationInterpolator(alpha, group, transform, 0.0f, (float) Math.PI * 2.0f); ri.setSchedulingBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0)); group.addChild(ri); final BranchGroup bg = new BranchGroup(); bg.addChild(group); // final Color3f light1Color = new Color3f(.1f, 1.4f, .1f); final BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); final Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f); final DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction); light1.setInfluencingBounds(bounds); bg.addChild(light1); universe.getViewingPlatform().setNominalViewingTransform(); universe.addBranchGraph(bg); System.out.println("constructor"); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM