简体   繁体   English

贝塞尔曲线样例代码在触摸时移动

[英]Bezier curve sample code on touches moved

I am searching for sample code to draw bezier curve by using touches, if any one has links or sample code please help me. 我正在寻找通过触摸来绘制贝塞尔曲线的示例代码,如果有人有链接或示例代码,请帮助我。 I am developing the game for flight control in which I have to move my ccsprite along the path which user draw with finger 我正在开发用于飞行控制的游戏,其中必须沿着用户用手指绘制的路径移动ccsprite

EDIT:Thanks for Suggestion,Now i am Making Curve With CAShapeLayer and CAKeyframeAnimation.now i need to remove the Curve one user pass our it.like in filght control app if ccsprite moves along the path the back side path is delete like that,thankyou very much 编辑:感谢您的建议,现在我正在使用CAShapeLayer和CAKeyframeAnimation制作曲线。现在我需要删除一个用户通过它的曲线。如果ccsprite沿该路径移动,则删除该曲线,就像在filght控制应用程序中那样,谢谢非常

Bezier curves are drawn using two endpoints and two anchors. 贝塞尔曲线使用两个端点和两个锚点绘制。 Drawing a Bezier curve with this information is very easy. 使用此信息绘制贝塞尔曲线非常容易。 Getting these point from a hand drawn curve is called vectorization (image tracing). 从手绘曲线中获取这些点称为矢量化(图像跟踪)。 I is a quite complex task. 我的任务很复杂。

I think you can get much better results by using a simple algorithm like this : 我认为您可以使用以下简单算法获得更好的结果:

  1. Store all coordinate in a FIFO list 将所有坐标存储在FIFO列表中
  2. move your sprite in direction of the next point of the list 将精灵移至清单的下一个点
  3. once the sprite is clause enough to the point discard the point and continue with next point 一旦精灵足够从句到点,则丢弃该点并继续下一个点
  4. Ease the movement by using simple smoothing algorithm (xSpeed, ySpeed, damping), just ask me if you need help for this 使用简单的平滑算法(xSpeed,ySpeed,damping)来简化运动,只是问我是否需要帮助

EDIT : 编辑:

I just checked flight control video, I would do it like this. 我刚刚查看了飞行控制视频,我会这样做。 Once the user press on one it select it. 一旦用户按下它,就将其选中。 Then user start drawing, plot point one by one as close as possible to the user finger (movements are limited by plane turning radius). 然后,用户开始绘图,将点尽可能靠近用户的手指一点绘制(运动受平面转弯半径的限制)。 This will record a chain of steps. 这将记录一系列步骤。 Each step is defined by its angle (I think speed is constant), this angle must be equal to the previous one +/- turning capability. 每个步骤均由其角度定义(我认为速度是恒定的),该角度必须等于前一个+/-旋转能力。

Use simple trigonometry to compute x,y coordinates. 使用简单的三角函数来计算x,y坐标。

Look at this : How to make a sprite point at the mouse. 看一下: 如何使鼠标指向精灵。 XNA C# XNA C#

You'll find better help for that on https://gamedev.stackexchange.com/ sure someone have a ready to paste code similar to what you need. 您可以在https://gamedev.stackexchange.com/上找到更好的帮助, 确保有人愿意粘贴与您所需类似的代码。 I had done this years ago in Amos on Amiga, but I could not get the code to paste it here, sorry :) 我几年前曾在Amiga的Amos上完成此操作,但对不起,我无法获得将其粘贴到此处的代码:)

#include <math.h>

    CGFloat radius = 100;
    //CGFloat pi = 3.1415927; //comes for free in math.h

    UIBezierPath *basecircle = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,radius*2,radius*2)];
    [[UIColor blackColor] set];
    [basecircle fill];

This is the code for drawing bezierPath.You need to implement this in drawRect Method. 这是绘制bezierPath的代码。您需要在drawRect方法中实现它。 Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM