Monday, June 18, 2012

Rotating Frame of Reference

‹prev | My Chain | next›

I thought that I had figured out rotation and revolution in Gladius / CubicVR.js, but a comment from Alan Kligman, one of the Gladius maintainer, makes me think I may be mistaken.

Specifically, he indicates that revolution of the "moon" in my simulation is not tied to a global center of reference. Rather, it seems that the following code ties the moon (named "light-marker") to another object responsible for revolving:
space.findNamed( "light-marker" ).setParent( space.findNamed( "light-center" ) );
When this "light-center" rotates, the light market (and any other objects with "light-center" as the parent) rotate with it.

At least that is what I think Alan means.

So I simplify the remnants of the Gladius example to:
    space.add( new engine.simulation.Entity( "camera", /* ... */ ));
    space.add( new engine.simulation.Entity( "moon", /* ... */ ));
    var planet = new engine.simulation.Entity( "planet", /* ... */ );
    space.add( planet );

    var task = new engine.FunctionTask( function() {
      // rotate planet
      // rotate moon
    }, {
      tags: ["@update"]
    });
The result is that the moon now rotates about its z-axis but has no other motion:



In other words, this is how I had expected things to work yesterday before I convinced myself that Gladius / CubicVR.js worked otherwise.

I am much happier with this way of thinking. It is much easier to move individual objects without having to sync to a single frame of reference. But now that I have removed my previous parent object, the question becomes how can I make my moon more moon-like—without resorting to a light-center?

Now that I understand the dual-purpose of the light center, I think it makes a certain amount of sense. Instead of calling a light center, I relabel it a center of mass:
    space.add( new engine.simulation.Entity( "moon-center-of-mass",
      [
        new engine.core.Transform( [0, 0, 5], [engine.math.TAU, engine.math.TAU, engine.math.TAU] )
      ]
    ));
It is not a true center of mass, but it better captures what I want it to do. Perhaps something capturing the frame-of-reference nature of this point-without-material would be better. I will worry about naming another day. For now, I make this this parent to my "moon":
space.findNamed( "moon" ).setParent( space.findNamed( "moon-center-of-mass" ) );
And, instead of rotating the moon, I rotate the center of mass:
      var lightRotation = new engine.math.Vector3( space.findNamed( "moon-center-of-mass" ).findComponent( "Transform" ).rotation );
      lightRotation = engine.math.vector3.add( lightRotation, [0, 0, space.clock.delta * 0.001] );
      space.findNamed( "moon-center-of-mass" ).findComponent( "Transform" ).setRotation( lightRotation );
With that, I have my moon again revolving about its planet:



Much thanks to Alan for preventing me from laboring under a false assumption. Much better to correct that now than later when I had built much more understanding on faulty foundations.


Day #421

No comments:

Post a Comment