In cocos2d you can move a sprite in a Bezier path using ccBezierConfig. That's not my problem though, I have a missile and am trying to make it rotate perpendicular to that point on the curve. I couldn't figure it out for a while and then my friend told me about derivatives. Now I need to find the derivative of a bezier curve apparently. I searched on Google and found it on this page: http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/Bezier/bezier-der.html. So then I tried implementing rotating the missile with 3 methods here they are:
-(float)f:(int)x {
if (x == 0)
return 1.0f;
float result = 1;
while (x>0) {
result = result*x;
x--;
}
return result;
}
-(float)B:(float)u i:(int)i n:(int)n {
return ([self f:n])/([self f:i]*[self f:(n-i)])*pow(u, (float)i)*pow(1-u, (float)n-i);
}
-(void)rotateMissile:(float)delta {
//Get bezier derivative...
float y = [self B:missileP1.controlPoint_1.x i:0 n:2]+[self B:missileP1.controlPoint_2.x i:1 n:2]
*2*(missileP1.controlPoint_1.x - missileP1.controlPoint_2.x);
//Take the y and rotate it...
missile1.rotation = atanf(y);
}
The first method is for factorials, the second is supposed to find B in the equation derivative. The 3rd method is supposed to find the actual derivative and rotate the missile by converting slope to degrees using atanf.
The rotateMissile is being called continuously like such:
[self schedule:@selector(rotateMissile:)];
missileP1 is the ccBezierConfig object. missile1 is the missile I am trying to rotate. I'm just really confused about this whole derivative thing (in other words, I'm really lost and confused). I need help trying to figure out whats wrong... Sorry that the code is messy, the equations were long and I could figure out a way to make it less messy.