How to place sprites along a curve in cocos2d

205 views Asked by At

The set up of my game relies on placing several sprites along a curve that may look like a bow , ellipse, or be a bit more complex (assume it would be bezier curve then). I want to be able to place my sprites in somewhat equal distances apart.

Can anyone share how this could be done ?

Using cocos2d 2.1

1

There are 1 answers

0
Abhineet Prasad On

I dont know if there is an easier method to do this ,but we could arrange the sprite along any curve by using the mathematical equation for that curve.

For a parabolic curve (advantage: symmetrical,so easy to place equidistant points) Find out a equation that satisfies your start and EndPoints and obtain the y values for equidistant x points between start and end using the function below.

example: y = -x^2 + 20x - 1 (general equation : y = ax^2 + bx + c)

static inline parabolicYValue(float x, float a,float b, float c){
     return (powf(a*x,2) + b*x + c);

}

You could come up with a similar function for Bezier curves : (Bezier cubic curve)

static inline CGFloat bezierYValue( float a, float b, float c, float d, float x )
{
    return (powf(1-x,3) * a +
            3*x*(powf(1-x,2))*b +
            3*powf(x,2)*(1-x)*c +
            powf(x,3)*d );
}

However, obtaining equidistant points on a bezier curve is a bit of a chore. On the other hand if you by equidistant you mean , distance only along the x axis then this should not be problem.