在 Cocos2d 中获取贝塞尔曲线问题的导数
在 cocos2d 中,您可以使用 ccBezierConfig 在贝塞尔曲线路径中移动精灵。但这不是我的问题,我有一枚导弹,并试图让它垂直于曲线上的那个点旋转。我一时想不通,然后朋友给我讲了衍生品。现在我显然需要找到贝塞尔曲线的导数。我在Google上搜索并在此页面上找到了它: http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/Bezier/bezier-der.html。然后我尝试用 3 种方法来实现导弹的旋转:
-(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);
}
第一种方法用于阶乘,第二种方法应该在方程导数中找到 B。第三种方法应该找到实际的导数,并通过使用 atanf 将斜率转换为度数来旋转导弹。
rotateMissile 被连续调用,如下所示:
[self schedule:@selector(rotateMissile:)];
missileP1 是 ccBezierConfig 对象。 Missile1 是我要旋转的导弹。我只是对这整个衍生的事情感到非常困惑(换句话说,我真的很迷失和困惑)。我需要帮助来找出问题所在...抱歉,代码很混乱,方程很长,我可以找到一种方法来让它不那么混乱。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上我不明白你如何获取导数并将其放入浮点数中。这是因为 Bizier 曲线是二维参数曲线(它具有 x 和 y 分量)。它不是函数 y(x)。在三次方的情况下,
我们称其为 form1。所以实际上它只不过是两个三阶多项式。三次贝塞尔曲线的传统形式是
注意,这里的 B(t) 是一个二维向量 ( x(t), y(t))。因此,如果您有传统方式定义的贝塞尔曲线,您可以通过评估系数 x0、x1 等将其转换为 form1。
如果您现在在 form1 中定义了贝塞尔曲线,则很容易求导:
现在向量 (x'(t), y'(t)) - 是贝塞尔曲线上的速度。它也是曲线的切向量。垂直向量将为 (-y'(t), x'(t)) 或 ((y'(t), -(x'(t)))。
以下是系数:

对于 y 系数,公式完全相同。它只是 py0, py1, py2, py3 而不是 px0, ... 。
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:
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:
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, ... .