how to make Sprite follow bezier curve

后端 未结 1 974
慢半拍i
慢半拍i 2021-02-06 05:58

Im fairly new to objective-c and sprite kit but i\'ve done games development for a while. Im currently working on a 2d game and i have enemy ships moving from right to left on t

相关标签:
1条回答
  • 2021-02-06 06:14

    Bezier curves are used to generate a smooth curve between two points. To move the path from left to right, choose a starting point on the left and choosing an ending point on the right. The two control points determine the shape of the path to take from left to right. Varying the startpoint and endpoint in the following code will control where the bezier curve starts and where it ends. Varying the control points vary the shape of the curve. You can see that with the attached GIF.

    CGMutablePathRef cgpath = CGPathCreateMutable();
    
    CGPoint startingPoint = CGPointMake(50, 100);
    
    CGPoint controlPoint1 = CGPointMake(160, 250);
    CGPoint controlPoint2 = CGPointMake(200, 140);
    
    CGPoint endingPoint = CGPointMake(303, 100);
    
    
    CGPathMoveToPoint(cgpath, NULL, startingPoint.x, startingPoint.y);
    CGPathAddCurveToPoint(cgpath, NULL, controlPoint1.x, controlPoint1.y,
                          controlPoint2.x, controlPoint2.y,
                          endingPoint.x, endingPoint.y);
    
    
    SKAction *enemyCurve = [SKAction followPath:cgpath asOffset:NO orientToPath:YES duration:5];
    
    [enemy runAction:[SKAction sequence:@[[SKAction waitForDuration:1],enemyCurve]]];
    

    enter image description here

    P0 and P3 are the starting and ending points and P1 and P2 are the control points.

    Check out this page to play more with bezier curves. http://www.jasondavies.com/animated-bezier/

    0 讨论(0)
提交回复
热议问题