Saturday, July 14, 2012

Better Walking in Gladius

‹prev | My Chain | next›

Today I hope to make my Gladius-based avatar look more or less as though it is walking. I ran into a frame of reference problem last night, but eventually got it working. Well, except that instead of a simple back-and-forth motion, the leg spins completely around 360° Exorcist-style:


Rather than continuing around and around without stop, I add the concept of direction to my game task, and reverse direction whenever the angle gets too far away from straight up-and-down:
        var dir = 1;
        var task = new engine.FunctionTask( function() {
          var entity = space.findNamed("right-leg-frame")
            , transform = entity.findComponent("Transform")
            , rotation = new engine.math.Vector3(transform.rotation)
            , change = dir * space.clock.delta * 0.001;

          if (Math.abs(rotation[2]) > Math.PI/8) {
            dir = dir * -1;
            change = change * -2;
          }

          rotation = engine.math.vector3.add(rotation, [0, 0, change]);
          transform.setRotation(rotation);

        });
        task.start();
The problem with this approach is that eventually the leg gets stuck. Even if I make the change very large, sometimes it is not enough to overcome a long space.clock.delta.

In the end, I switch to a less satisfactory, but less prone to being stuck solution:
        var pos = 0, dir = 1;
        var task = new engine.FunctionTask( function() {
          var entity = space.findNamed("right-leg-frame")
            , transform = entity.findComponent("Transform")
            , rotation = new engine.math.Vector3(transform.rotation);

          pos = pos + dir * space.clock.delta;
          if (pos > 1000) {
            dir = -1;
            pos = 1000;
          }
          if (pos < -1000) {
            dir = 1;
            pos = -1000;
          }

          var angle = Math.PI / 8;
          transform.setRotation([0,0, (pos/1000)*(Math.PI/8) ]);

        });
        task.start();
Instead of attempting to toggle the direction, I explicitly check for moving past negative and positive boundaries and then hard reset the direction and extent past the boundary.

It is a bit more code, but it works. And it should also serve as the basis for the motion of other limbs. So unless I can come up with a better solution, I will likely stick with this for now.

Day #447

No comments:

Post a Comment