Sunday, July 1, 2012

Dumb Keys Codes in Gladius

‹prev | My Chain | next›

Up tonight, I hope to add a bit of interactivity to my Gladius demonstration of retrograde motion. I recently hacked in the ability to switch active cameras. But the switching is done automatically ever 240 (or so) clock cycles:
     if (Math.round(space.clock.time) % 240 == 0) {
        space.setCamera(space.camera.name == "camera1" ? "camera2" : "camera1");
      }
This is not an entirely satisfactory situation because the primary task function is not always called on 240, so sometimes (more often than not), the cameras do not switch when desired. Worse still, they oftentimes switch when the retrograde motion is in full swing, making it tough to regain bearings.

Ultimately, I mean to do this the Gladius way (e.g. with gladius-input), but tonight I do it the dumb way—also know as my way. And my way is to add a document listener for key presses. If "1" is pressed, then I should view through camera-1, if "2" is pressed, then I should view from camera-2:
   document.addEventListener("keydown", function(event) {
      var code = event.which || event.keyCode;
      if (code == 0x31) { // 1
        space.setCamera("camera1");
      }
      else if (code == 0x32) { // 2
        space.setCamera("camera2");
      }
    });
And that actually works just fine. If I press "1", I see from "above" the solar system:


And pressing "2" gives me the "Earth-cam":


Now that I consider it, it is extremely hard to get good screenshots. When I press the capture button, there is inevitably a delay and the shot is gone. It would be nice to be able to pause the revolution of the planets around the Sun so that I can get the good stuff. So I add a "p" to the list of keys that are checked and make use of some function-task predicate methods:
    document.addEventListener("keydown", function(event) {
      var code = event.which || event.keyCode;
      if (code == 0x31) { // 1
        space.setCamera("camera1");
      }
      else if (code == 0x32) { // 2
        space.setCamera("camera2");
      }
      else if (code == 0x50) { // p
        if (task.isStarted()) {
          task.pause();
        }
        else {
          task.start();
        }
      }
    });
Now I can get a shot just as Mars is passing behind the sun from above


And from "Earth-cam":


That was fairly easy. I think tomorrow I will see what gladius-input has to offer. And I may just need to upgrade to gladius 0.2 for that.



Day #434

No comments:

Post a Comment