7 Replies Latest reply on Oct 5, 2008 9:24 PM by Mylenium

    Negative zero?

      I'm trying to tie the rotation of a layer to the direction that it is going (a pacman type object which should munch in the direction it is traveling).

      I'm trying to use an expression on the rotation property, but for some reason I'm getting negative zero values when subtracting position vectors. For instance, while the layer is traveling in one direction, [x1, y1] - [x2, y2] = [-0.0, -9.7] sometimes and = [0.0, -9.7] other times.

      I guess 0.0 != -0.0 and -0.0 < 0.0 ? Any ideas how to resolve this?

      Here's the code:
      prevPos = position.valueAtTime(framesToTime(timeToFrames()-1));
      currPos = position;

      delta = sub(currPos,prevPos);
      if(delta[0] == 0 && delta[1] >= 0) rotation = -90;
      else if(delta[0] == 0) rotation = 90;
      else if(delta[1] == 0 && delta[0] >= 0) rotation = -180;
      else rotation = 0;
        • 1. Re: Negative zero?
          Andrew Yoole MVP & Adobe Community Professional
          Sorry if it's a silly question, but is there any reason why the layer's auto-orient function is unsuitable for the task?
          • 2. Re: Negative zero?
            Level 1
            You might try using lookAt position value at time. If you set your layer to 3D you can apply the following expression to Orientation.
            lookAhead = thisComp.layer("Master Null").effect("lookAhead")("Slider");
            lookAt(position, position.value_at_time(time+lookAhead))
            A slider control is added to a master null to control how far ahead or behind the current position the layer will point.

            To keep the layer square to the scene you'll have to rotate it on y 90º.

            This gives you more control over the orientation than orient to path does. I hope it helps.
            • 3. Re: Negative zero?
              Mylenium Most Valuable Participant
              >I guess 0.0 != -0.0 and -0.0 < 0.0 ? Any ideas how to resolve this?

              It's a precision problem. Apart from your code being otherwise sort of *eek*, a 1 frame look ahead is not that much once you come to think of it. Depending on the actual speed, especially on the corners, where the positional difference might be insufficient to determine a criteria, this could pose a problem. Also you may wish to round your values instead of operating on the subpixel precision level as you do currently.

              • 4. Re: Negative zero?
                Level 1
                Thanks for all your replies.

                I just tried auto-orient along path, and it looks promising, but I can't get the same side of the layer to face the same direction on each leg of the motion path. In other words, if I set auto-orient, and at time=0 I set the layer so that it is oriented correctly towards the camera by using Y and Z rotation, when it makes a 90 degree turn, the layer disappears because it's edge is now facing the camera.

                I also just tried the lookAt() function, and it works fine, but it's being affected by the negative zeros so that only at certain times is the layer facing the camera correctly (it flips back and forth somewhat randomly).

                The motion path is just a rectangle.
                • 5. Re: Negative zero?
                  Level 1
                  I forgot to mention that if you don't want a 3D layer to flip over when it changes direction from left to right you must make the motion path in the x z plane and not in the default x y plane. You then move the camera to the top and have it shoot down.

                  This applies to both the lookAt() expression and orient to a path in 3D.
                  • 6. Re: Negative zero?
                    Level 1
                    That made it work better(auto-orient now changes the rotation corectly when turning a corner), but for some reason it still flips the wrong direction during random frames when traveling along a straight motion path. If I leave the paths as auto-bezier instead of switching to linear, the layer kind of "leans" around the turns.

                    This is kind of odd. I don't like it when things are non-deterministic...
                    • 7. Re: Negative zero?
                      Mylenium Most Valuable Participant
                      Eulers v. Quarternions again, I suppose. If you check the value readouts, I'm sure at these frames the orientation completely changes its values in the opposite direction. So you'd probably have to create a "stable" solver with custom expressions. Might not be worth the effort, so why not simply convert the expression to keyframes and remove the offending parts (assuming your path does not change anymore)?