在 Cocos2d 中获取贝塞尔曲线问题的导数

Getting the derivative of a Bezier curve problem in Cocos2d(在 Cocos2d 中获取贝塞尔曲线问题的导数)

本文介绍了在 Cocos2d 中获取贝塞尔曲线问题的导数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

解决方案

Actually I don't understand how are you taking a derivative and putting it into a float. That's because Bizier curve is two dimensional parametric curve (it has x and y components). It is not a function y(x). In cubic case it is:

x(t) = x0 + x1*t + x2*t*t + x3*t*t*t
y(t) = y0 + y1*t + y2*t*t + y3*t*t*t

Let's call it form1. So actually it's nothing more then two polynomials of third order. The traditional form of cubic Bezier curve is

Note, that B(t) here is a two dimensional vector (x(t), y(t)). So if you have a tradional way defined Bezier curve you can convert it to form1 by evaluating coefficients x0, x1 and son on.

If you now have your Bezier curve defined in form1 it is very easy to take the derivative:

x'(t) = x1 + 2*x2*t + 3*x3*t*t
y'(t) = y1 + 2*y2*t + 3*y3*t*t

Now the vector (x'(t), y'(t)) - is the velocity on your bezier curve. It also a tangent vector to your curve. The perpendicular vector will be (-y'(t), x'(t)) or ((y'(t), -(x'(t)).

Here are the coefficients:

For y coefficients the formula is totally identical. It just will py0, py1, py2, py3 instead of px0, ... .

这篇关于在 Cocos2d 中获取贝塞尔曲线问题的导数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 Cocos2d 中获取贝塞尔曲线问题的导数

基础教程推荐