获取Cocos2d中Bezier曲线的导数问题 [英] Getting the derivative of a Bezier curve problem in Cocos2d

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

问题描述

在cocos2d中,您可以使用ccBezierConfig在Bezier路径中移动精灵。这不是我的问题,但我有一个导弹,我试图使它垂直于曲线上的点旋转。我不知道一会儿,然后我的朋友告诉我有关衍生品。现在我需要找到一个贝塞尔曲线的导数。我在Google上搜索,并在此网页上找到: http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/Bezier/bezier-der.html 。所以我试着实现旋转导弹与3种方法,他们是:

   - (float)f: 
if(x == 0)
return 1.0f;
float result = 1;
while(x> 0){
result = result * x;
x--;
}
return result;
}

- (float)B:(float)ui:(int)in:(int)n {
return([self f:n])/ self f:i] * [self f:(ni)])* pow(u,(float)i)* pow(1-u,
}

- (void)rotateMissile:(float)delta {
//获取bezier导数...
float y = [self B:missileP1.controlPoint_1 .xi:0 n:2] + [self B:missileP1.controlPoint_2.xi:1 n:2]
* 2 *(missileP1.controlPoint_1.x - missileP1.controlPoint_2.x);

//取y并旋转它...
missile1.rotation = atanf(y);

}

第一种方法是用于阶乘,第二种方法是在等式导数中找到B.第三种方法应该通过使用atanf将斜率转换为度来找到实际的导数和旋转导弹。



rotateMissile正在被这样连续调用:

  [self schedule:@selector(rotateMissile :)]; 

missileP1是ccBezierConfig对象。 missile1是我试图旋转的导弹。我只是很困惑这个整个派生的东西(换句话说,我真的迷路,困惑)。我需要帮助试图找出什么错误...对不起,代码是凌乱的,方程是长的,我可以找出一种方法,使其不那么混乱。

解决方案

实际上我不明白你是如何拿一个衍生品,把它放在一个浮动。这是因为Bizier曲线是二维参数曲线(它有x和y分量)。它不是函数y(x)。在立方体情况下是:

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

让我们称之为form1。所以实际上它不是两个多项式的三阶。立方贝塞尔曲线的传统形式是





如果你现在有Bezier曲线定义form1很容易得到导数:

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

现在矢量(x'(t),y'(t)) - 是贝塞尔曲线上的速度。它也是你的曲线的切线向量。垂直向量将是(-y'(t),x'(t))或(y'(t), - (x'(t))。

以下是系数:



对于y系数,公式是完全相同的,它只是将py0,py1,py2,py3而不是px0,...。


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中Bezier曲线的导数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆