Search code examples
javarotationjava-3d

How to rotate a rectangle continously in a specific angle in Java 3D


I used the following code to rotate a rectangle along x-axis. But the problem is, I want to change the rotation angle continuously by loop, or by taking the values from user input. As I am new in java 3D, I have no idea how to do that. Any kind of help is great for me, THANKS IN ADVANCE!

  SimpleUniverse universe = new SimpleUniverse();
  BranchGroup group = new BranchGroup();
  ColorCube c = new ColorCube(0.3);

  Transform3D t1 = new Transform3D();
  t1.rotX(Math.PI/2.0d);    

  TransformGroup tg1 = new TransformGroup(t1);

  tg1.addChild(c);
  group.addChild(tg1);
  group.compile();

  universe.getViewingPlatform().setNominalViewingTransform();
  universe.addBranchGraph(group);

Solution

  • You must call TransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE) to allow your transform to be modified at runtime. Then, you must call TransformGroup.setTransform() later to modify its transform. It's up to you to pass it with a different transform to make it rotate, move, ... You can use the following example: http://www.java3d.org/animationinteraction.html

    You can use a timer to execute a task regularly. This task can modify the transform (by computing a new transform from the previous one) and you don't need to write a loop to do so.