ios - SpriteKit Generate Random Curl Line -
i working on project , need accomplish generate random curl line. @ point have have line randomly generated not curl. spawning points @ different x positions on every 0.5 seconds , connect points bezier. works except not curl. on picture below shown on 1) how have , on 2) how suppose make it. ideas how can ? using spritekit - objective c
basically need make , randomize a bézier (parametric) curve control points.
after read great answer , familiar terms, can try example (i assume scene , view size set correctly):
gamescene.m
#import "gamescene.h" @implementation gamescene -(void)didmovetoview:(skview *)view { } -(nsinteger)randomnumberbetween:(nsinteger)from to:(nsinteger)to { return (int)from + arc4random() % (to-from+1); } - (cgmutablepathref)generatepath { cgmutablepathref path = cgpathcreatemutable(); cgpoint p0 = cgpointmake(cgrectgetmidx(self.frame),self.scene.size.height-20.0f); //starting point little below upper edge of screen cgpathmovetopoint(path, nil, p0.x, p0.y); cgpoint p1 = cgpointmake([self randomnumberbetween:150 to:300], self.scene.size.height- [self randomnumberbetween:150 to:300]); cgpoint p2 = cgpointmake([self randomnumberbetween:150 to:300],[self randomnumberbetween:150 to:300]); cgpoint p3 = cgpointmake(cgrectgetmidx(self.frame),0 + 20.0f); //ending point, little above bottom edge of screen cgfloat v = 0.3; cgfloat cp1x = p1.x+v * (p1.x-p0.x); cgfloat cp1y = p1.y+v * (p1.y-p0.y); cgfloat cp2x = p2.x-v * (p3.x-p2.x); cgfloat cp2y = p2.y-v * (p3.y-p2.y); cgpathaddcurvetopoint(path,nil,cp1x,cp1y,cp2x,cp2y,p3.x,p3.y); /*debug - not needed*/ skspritenode *sp0 = [skspritenode spritenodewithcolor:[skcolor yellowcolor] size:cgsizemake(5.0f,5.0f)]; sp0.zposition = 5; sp0.position = p0; sklabelnode *lp0 = [sklabelnode labelnodewithfontnamed:@"arialmt"]; lp0.fontcolor = [skcolor whitecolor]; lp0.fontsize = 20.0f; lp0.text = @"p0"; lp0.position = cgpointmake(0.0f,-20.0f); [sp0 addchild:lp0]; [self addchild:sp0]; skspritenode *sp1 = [skspritenode spritenodewithcolor:[skcolor yellowcolor] size:cgsizemake(5.0f,5.0f)]; sp1.zposition = 5; sp1.position = p1; sklabelnode *lp1 = [sklabelnode labelnodewithfontnamed:@"arialmt"]; lp1.fontcolor = [skcolor whitecolor]; lp1.fontsize = 20.0f; lp1.position = cgpointmake(0.0f,15.0f); lp1.text = @"p1"; [sp1 addchild:lp1]; [self addchild:sp1]; skspritenode *sp2 = [skspritenode spritenodewithcolor:[skcolor yellowcolor] size:cgsizemake(5.0f,5.0f)]; sp2.zposition = 5; sp2.position = p2; sklabelnode *lp2 = [sklabelnode labelnodewithfontnamed:@"arialmt"]; lp2.fontcolor = [skcolor whitecolor]; lp2.fontsize = 20.0f; lp2.position = cgpointmake(0.0f,15.0f); lp2.text = @"p2"; [sp2 addchild:lp2]; [self addchild:sp2]; skspritenode *sp3 = [skspritenode spritenodewithcolor:[skcolor yellowcolor] size:cgsizemake(5.0f,5.0f)]; sp3.zposition = 5; sp3.position = p3; sklabelnode *lp3 = [sklabelnode labelnodewithfontnamed:@"arialmt"]; lp3.fontcolor = [skcolor whitecolor]; lp3.fontsize = 20.0f; lp3.position = cgpointmake(0.0f,15.0f); lp3.text = @"p3"; [sp3 addchild:lp3]; [self addchild:sp3]; skshapenode *p0p1 = [skshapenode node]; p0p1.zposition = 2; cgmutablepathref path1 = cgpathcreatemutable(); cgpathmovetopoint(path1, null, p0.x, p0.y); cgpathaddlinetopoint(path1, null, p1.x, p1.y); p0p1.path = path1; [p0p1 setstrokecolor:[uicolor greencolor]]; [self addchild:p0p1]; skshapenode *p2p3 = [skshapenode node]; p2p3.zposition = 2; cgmutablepathref path2 = cgpathcreatemutable(); cgpathmovetopoint(path2, null, p2.x, p2.y); cgpathaddlinetopoint(path2, null, p3.x, p3.y); p2p3.path = path2; [p2p3 setstrokecolor:[uicolor greencolor]]; [self addchild:p2p3]; return path; } -(void)touchesbegan:(nsset *)touches withevent:(uievent *)event { [self removeallchildren]; skshapenode *yourline = [skshapenode node]; yourline.zposition = 1; yourline.path = [self generatepath]; [yourline setlinewidth:5]; [yourline setstrokecolor:[skcolor redcolor]]; [self addchild:yourline]; }
most of code used visual debugging , not needed in order randomize curve. important part first part of method generatepath. here result:
Comments
Post a Comment