Thursday, July 5, 2012

Gladius Full Camera Rotation (Almost)

‹prev | My Chain | next›

I think I understand how all of the input actors and keyboard input stuff in Gladius. Tonight I going to round out my implementation to allow for full movement.

First up, I add new states to pan left and right (in addition to up and down), as well as moving the camera forward and backwards:
{
  "States": {
    "PanUp":        [ "I" ],
    "PanDown":      [ "M" ].
    "PanLeft":      [ "J" ],
    "PanRight":     [ "K" ],
    "MoveForward":  [ "O" ],
    "MoveBackward": [ "L" ]
  }
}
The complete set of actor controls for rotation then becomes:
    var cameraLogic = {
      "Update": function(event) {
        if (!this.owner.hasComponent("Controller")) return;

        var controller = this.owner.findComponent("Controller")
          , transform = this.owner.findComponent("Transform");

        var rotation;
        if (controller.states["PanUp"])
          rotation = [space.clock.delta * 0.0005, 0, 0];
        if (controller.states["PanDown"])
          rotation = [-space.clock.delta * 0.0005, 0, 0];
        if (controller.states["PanLeft"])
          rotation = [0, space.clock.delta * 0.0005, 0];
        if (controller.states["PanRight"])
          rotation = [0, -space.clock.delta * 0.0005, 0];

        if (rotation)
          transform.setRotation(math.vector3.add(rotation, transform.rotation));

        // ...
      }
    };
As I found last night, the Update function in this object is invoked for every event that occurs. If the input controller is in one of the listed states, then the camera entity moves accordingly.

Along those same lines, movement is accomplished moving by manipulating the position of the camera entity:
    var cameraLogic = {
      "Update": function(event) {
        if (!this.owner.hasComponent("Controller")) return;

        var controller = this.owner.findComponent("Controller")
          , transform = this.owner.findComponent("Transform");

        // ...
        var position;
        if (controller.states["MoveForward"])
          position = [0, 0, space.clock.delta * 0.01];
        if (controller.states["MoveBackward"])
          position = [0, 0, -space.clock.delta * 0.01];

        if (position)
          transform.setPosition(math.vector3.add(position, transform.position));
      }
    };
With that, I can start high above the solar system simulation:


I can then zoom in:

And get quite close to the Sun:

And then rotate the camera to watch Mars and the Earth.

The problem is that I have not quite accounted for rotating when I am no longer on the z-axis. If I try to pan at this point, I end rotated at and awkward angle with no means to recover:


Dang it. Maybe that is why the original example did so much 4-dimensional matrix manipulation. I think I save that for another day.


Day #438

No comments:

Post a Comment