hyperandroid / CAAT

Canvas Advanced Animation Toolkit
hyperandroid.github.com/CAAT
MIT License
727 stars 117 forks source link

Actor's final position being set incorrectly for path with multiple segments #95

Closed donaldducky closed 11 years ago

donaldducky commented 11 years ago

The final position of a caat actor is incorrect when adding a path behavior to the actor containing 3 paths.

The getPosition function for CAAT.LinearPath is being passed a time of 1.0000000000000002 and then sets time %= 1.

I'm guessing this is just a rounding error for numbers that don't divide evenly into 1 (this doesn't happen for paths with 1, 2 or 4 segments).

Here is an example below (using build/caat.js @ 65cb114bfb8d3c75b5cd7f59f8e5645fa55a6bbb ):

<canvas id="scene"></canvas>
<script src="build/caat.js"></script>
<script>
var director = new CAAT.Director().initialize(
  200,
  200,
  document.getElementById('scene')
);

var scene = director.createScene();

var path = new CAAT.Path()
  .beginPath(10,10)
  .addLineTo(50,50)
  .addLineTo(50,100)
  .addLineTo(100,100)
  .endPath()
  ;

var pathActor = new CAAT.PathActor()
  .setBounds(0,0, 200,200)
  .create()
  .setPath(path)
  .setInteractive(true)
  ;

var shapeActor = new CAAT.ShapeActor()
  .setLocation(10,10)
  .setSize(10,10)
  .enableEvents(false)
  .setCompositeOp('lighter')
  .setFillStyle('#f00')
  ;

var behavior = new CAAT.PathBehavior()
  .setPath(path)
  .setFrameTime(scene.time, 1500)
  ;

scene.addChild(pathActor);
scene.addChild(shapeActor);

CAAT.loop(20);

shapeActor.addBehavior(behavior);
</script>
hyperandroid commented 11 years ago

Fixed. Problem related to rounding on CAAT.PathUtil.Path.getPosition function. Now each segment's calculated time is clamped to a valid value which prevents this aliasing. Thanks.

donaldducky commented 11 years ago

Cool, thanks for quick update! Where can I get the fix? I don't see a commit related to this.

Thanks for your great work :D

hyperandroid commented 11 years ago

Hey, as a temporary patch, you can do the following:

add

              // Clamp this segment's time to a maximum since it is relative to the path.
                // thanks https://github.com/donaldducky for spotting.
                if (time>1) {
                    time=1;
                } else if (time<0 ) {
                    time= 0;
                }

in CAAT.Path.PathUtil.getPosition, just under this block:

        while( l!==r ) {

            m= ((r+l)/2)|0;
            psstv= psst[m];
            if ( psstv<=time && time<=psstv+psdt[m]) {
                time= psdt[m] ?
                        (time-psstv)/psdt[m] :
                        0;

and above:

           var pointInPath= ps[m].getPosition(time);
                np.x= pointInPath.x;
                np.y= pointInPath.y;
                return np;

that will set things up. I expect to release a new version today nov 18th, but just in case.

donaldducky commented 11 years ago

Cool, thanks!

Just wondering how you do "releases"...do you not just commit everything and push it to github?

hyperandroid commented 11 years ago

Yes. That's the final step. But also make sure all demos, tutorials and games I have still work after changes. This release includes retina display, which makes things a bit tougher to manage. Also more fine grained sprite animations, so new demos are on its way too. Feel free to send me some url pointing to your work in progress, either over here, or to my personal email ;)

Thanks.

2012/11/18 Don Chea notifications@github.com

Cool, thanks!

Just wondering how you do "releases"...do you not just commit everything and push it to github?

— Reply to this email directly or view it on GitHubhttps://github.com/hyperandroid/CAAT/issues/95#issuecomment-10498189.