Cocos2D 围绕物体的贝塞尔曲线,就像通过重力一样
我正在尝试操纵一个物体。当它靠近另一个物体(比如说地球仪)时,我希望地球仪对原始物体产生引力。我知道我应该使用 CCBezierTo
,所以这与其说是一个编程问题,不如说是一个数学问题。
从数学上讲,我如何计算出贝塞尔曲线的三个点(1、2 和终点),并根据其距离赋予其权重(距离越远 = 拉力越小)。我已经在变量中映射了距离。
想象一下围绕月球弹弓的宇宙飞船。
代码:
ccBezierConfig bezier;
bezier.controlPoint_1 = ccp(projectile.position.x + 10, projectile.position.y + 20);
bezier.controlPoint_2 = ccp(projectile.position.x + 20, projectile.position.y + 40);
bezier.endPosition = ccp(projectile.position.x + 30, projectile.position.y+60);
id bezierAction = [CCBezierTo actionWithDuration:1 bezier:bezier];
[projectile stopAllActions];
[projectile runAction: bezierAction];
I'm trying to manipulate an object. When it gets near another object, let's say a globe, I want the globe to have a gravitational pull on the original object. I know I'm supposed to use CCBezierTo
, so this isn't so much a programming question as it is a math question.
Mathematically, how could I figure out the three points of the bezier curve (1, 2, and end) and give it a weight depending on its distance (further away = less pull). I already have the distance mapped out in a variable.
Think of a spaceship slingshotting around the moon.
Code:
ccBezierConfig bezier;
bezier.controlPoint_1 = ccp(projectile.position.x + 10, projectile.position.y + 20);
bezier.controlPoint_2 = ccp(projectile.position.x + 20, projectile.position.y + 40);
bezier.endPosition = ccp(projectile.position.x + 30, projectile.position.y+60);
id bezierAction = [CCBezierTo actionWithDuration:1 bezier:bezier];
[projectile stopAllActions];
[projectile runAction: bezierAction];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
轨迹将是圆锥曲线(直线、双曲线、抛物线、椭圆或圆)。
您可以将它们表示为有理贝塞尔曲线。
http://www.cs。 mtu.edu/~shene/COURSES/cs3621/NOTES/spline/NURBS/RB-conics.html
和
http://www.cs.unc.edu/~dm /UNC/COMP236/papers/farin.pdf。
如果你坚持使用二次贝塞尔曲线部分,我会使用这样的函数
http://www.netlib.org/minpack/lmder.f 找到最佳位置控制点数量
通过最小二乘最小化。
我认为如果您只计算圆锥曲线并将它们绘制为线环,那将是最简单的。
或者您实现一个 Verlet 积分器并求解运动方程。
The trajectory would be a conic section (line, hyperbola, parabola, ellipse or circle).
You can represent those as a rational Bezier curve.
http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/NURBS/RB-conics.html
and
http://www.cs.unc.edu/~dm/UNC/COMP236/papers/farin.pdf.
If you insist on using quadratic Bezier sections, I would use a function like this
http://www.netlib.org/minpack/lmder.f to find optimal positions of control points
by least-squares minimization.
I think it would be easiest if you just calculate the conic sections and draw them as line loops.
Or you implement a verlet integrator and solve the equations of motions.