Simple rotation use case

Note: This is a transfer from the java gaming forums to continue the discussion here.





Is there a mechanism that does simple Rotation behaviors like Java3D? If on each frame I want an object to rotate on its Y axis by a specified amount, how do I do that? SpatialTransformer, suggested in another thread, seems more complicated than I would think necessary for a replacement to RotationBehavior.



I’m looking at SpatialTransformer and I have some basic questions about why it works the way it does. If I have geometry attached to a node, I would want to attach a SpatialTransformer to it. Not sure why I have to tell it the number of objects it would be transforming - it would be all of the nodes that are the children of the node that the Transformer is attached to.



Root Node
|_ Transformer
|_ Node
    |_ Planet
    |_ Node
     |_ Moon
 



By Attaching the transformer to the 'root node' I expect I should be transforming everything underneath.

Similarly, why would I be attaching objects to the transformer? setObject just seems awkward in this sense. I have to attach the transformer to the rootNode (which IS my pivot point) and then attach all of its children to the Transformer as well?

The other sets (position, rotation, scale) make sense to me. But I should be able to set a rotation based off a Vector3f as well (0,1,0) -> rotate along the y axis.

I guess what I'm saying here is that this use case is very complex when it really should be obvious and simple but in this case appears to be neither and will be harder to learn.

Have fun :slight_smile:


package jmetest.TutorialGuide;

import com.jme.app.SimpleGame;
import com.jme.scene.TriMesh;
import com.jme.scene.Node;
import com.jme.scene.state.TextureState;
import com.jme.scene.shape.Sphere;
import com.jme.animation.SpatialTransformer;
import com.jme.math.Vector3f;
import com.jme.math.Quaternion;
import com.jme.math.FastMath;
import com.jme.bounding.BoundingSphere;
import com.jme.util.TextureManager;
import com.jme.image.Texture;

import java.net.URL;

/**
 * Started Jan 25, 2005
 *
 *
 * @author Jack Lindamood
 */
public class HelloSolarSystem extends SimpleGame {
   public static void main(String[] args) {
      HelloSolarSystem app = new HelloSolarSystem();
      app.setDialogBehaviour(SimpleGame.ALWAYS_SHOW_PROPS_DIALOG);
      app.start();
   }

   TriMesh sun, earth, moon;
   Node centSolar, centEarth, centMoon;

   protected void simpleInitGame() {
      BoundingSphere.useExactBounds = true;

      sun = new Sphere("Sun", 15, 15, 20);
      sun.setModelBound(new BoundingSphere());
      sun.updateModelBound();

      earth = new Sphere("earth", 15, 15, 4);
      earth.setModelBound(new BoundingSphere());
      earth.updateModelBound();

      moon = new Sphere("moon", 15, 15, 2);
      moon.setModelBound(new BoundingSphere());
      moon.updateModelBound();

      centSolar = new Node("Center of solar system");
      centEarth = new Node("Center of the earth");
      centMoon = new Node("Center of the moon");

      centSolar.attachChild(sun);
      centSolar.attachChild(centEarth);

      centEarth.attachChild(earth);
      centEarth.attachChild(centMoon);

      centMoon.attachChild(moon);

//      centEarth.setLocalTranslation(new Vector3f(60,0,0));
//      centMoon.setLocalTranslation(new Vector3f(8,0,0));

      rootNode.attachChild(centSolar);

      Quaternion qzero = new Quaternion();
      qzero.fromAngleAxis(0, new Vector3f(0, 1, 0));
      Quaternion qone = new Quaternion();
      qone.fromAngleAxis(FastMath.DEG_TO_RAD * 120, new Vector3f(0, 1, 0));
      Quaternion qtwo = new Quaternion();
      qtwo.fromAngleAxis(FastMath.DEG_TO_RAD * 240, new Vector3f(0, 1, 0));
      Quaternion qthree = new Quaternion();
      qthree.fromAngleAxis(FastMath.DEG_TO_RAD * 360, new Vector3f(0, 1, 0));


      SpatialTransformer years = new SpatialTransformer(1);
      years.setObject(centSolar, 0, -1);
      years.setRotation(0, 0, qzero);
      years.setRotation(0, 118, qone);
      years.setRotation(0, 236, qtwo);
      years.setRotation(0, 356, qthree);
      years.interpolateMissing();
      centSolar.addController(years);

      centEarth.setLocalTranslation(new Vector3f(60, 0, 0));

      SpatialTransformer days = new SpatialTransformer(1);
      days.setObject(earth, 0, -1);
      days.setRotation(0, 0, qzero);
      days.setRotation(0, .333f, qone);
      days.setRotation(0, .666f, qtwo);
      days.setRotation(0, 1, qthree);
      days.interpolateMissing();
      earth.addController(days);

      SpatialTransformer lunarMonths = new SpatialTransformer(1);
      lunarMonths.setObject(centMoon, 0, -1);
      lunarMonths.setRotation(0, 0, qzero);
      lunarMonths.setRotation(0, 1, qone);
      lunarMonths.setRotation(0, 2, qtwo);
      lunarMonths.setRotation(0, 3, qthree);
      lunarMonths.interpolateMissing();
      centMoon.addController(lunarMonths);

      moon.setLocalTranslation(new Vector3f(8, 0, 0));

      URL monkeyImage = HelloSolarSystem.class.getClassLoader().getResource("jmetest/data/images/Monkey.png");

      TextureState ts = display.getRenderer().createTextureState();
      ts.setTexture(TextureManager.loadTexture(monkeyImage, Texture.MM_LINEAR, Texture.FM_LINEAR, true));
      earth.setRenderState(ts);
      moon.setRenderState(ts);

      display.getRenderer().getCamera().setFrame(new Vector3f(30, 0, -50), new Vector3f(1, 0, 0), new Vector3f(0, 1, 0), new Vector3f(0, 0, 1));

      lunarMonths.setSpeed(5);
      days.setSpeed(5);
      years.setSpeed(5);
   }
}

I would like to work with you guys on building a ‘behavior’ for these types of things as additions to the scenegraph so that it would be more of a ‘expected’ scenegraph mechanism ala open inventor and similar.

This isn’t a jME thing. If you try to do something similar with Java3d, you’ll run into the same problem. Planet rotations aren’t simple child/parent relationships.

I would like to work with you guys on building a 'behavior' for these types of things as additions to the scenegraph so that it would be more of a 'expected' scenegraph mechanism ala open inventor and similar.


We are actually working on the new controller system design, what I'll do when we have it fleshed out all the way is publish it, and you are welcome to make comments, but we are already pretty far along in its design. The new design will work more inline with "expected" scenegraph behavior.
"Cep21" wrote:
This isn't a jME thing. If you try to do something similar with Java3d, you'll run into the same problem. Planet rotations aren't simple child/parent relationships.

My concern isn't the accuracy of the parent/child relationships of a solar system (and we can get into defining this as a simple hierarchical animation case over PM). That is just an example to illustrate a point. It could just as easily be two boxes attached to a truck :)

What I'm looking for is what mojomonk is apparently describing. I just want to describe behaviors on the scenegraph to people who are more familiar with traditional scenegraph APIs. The mechanics of getting this to work a particular way is an excercise for the user.