Getting the derivative of a Bezier curve problem in 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 中获取贝塞尔曲线问题的导数
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01