Skip to content

Commit

Permalink
Improved Spine bezier
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkemose committed Aug 10, 2014
1 parent b08b2d2 commit 4295ab1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions Extensions/CCSpine/CCSpineCurve.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#import <Foundation/Foundation.h>
#import "CCDictionary.h"

#define CCSpineCurveBezierAccuracy (0.005)
// ----------------------------------------------------------------------------------------------

typedef NS_ENUM(NSInteger, CCSpineInterpolation)
Expand Down
29 changes: 19 additions & 10 deletions Extensions/CCSpine/CCSpineCurve.m
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,25 @@ - (float)translate:(float)value
return(0);
case CCSpineInterpolationBezier:
{
float t2 = value * value;
float t3 = t2 * value;
float result = (_coefficient[0].y * t3) + (_coefficient[1].y * t2) + (_coefficient[2].y * value);




// TODO
// convert according to bezier curve
return(result);
float step = 0.25f;
float progress = 0.50f;
float t2, t3;
do
{
t2 = progress * progress;
t3 = t2 * progress;
float xPos = (_coefficient[0].x * t3) + (_coefficient[1].x * t2) + (_coefficient[2].x * progress);
// check if result is accurate enough
if (fabs(xPos - value) <= CCSpineCurveBezierAccuracy) break;
// else step again
if (xPos > value)
progress -= step;
else
progress += step;
step *= 0.5f;
} while (1);
// calculate y position
return((_coefficient[0].y * t3) + (_coefficient[1].y * t2) + (_coefficient[2].y * progress));
}
}
NSAssert(NO, @"[CCSpineCurve translate] Invalid interpolation");
Expand Down

0 comments on commit 4295ab1

Please sign in to comment.