简体   繁体   中英

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

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

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
  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

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.

Look at this : How to make a sprite point at the mouse. 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. I had done this years ago in Amos on Amiga, but I could not get the code to paste it here, sorry :)

#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. Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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