ios - Spritekit: move SKSpritekitNode in arc with SKPhysicsBody -
i'm trying move skspritenode in arc physics in spritekit so:
but unsure physic should apply (applyimpulse, applyforce, applytorque).
currently using applytorque, code not work, , produces no movement on object:
_boy.physicsbody.velocity = cgvectormake(1, 1); cgvector thrustvector = cgvectormake(0,100); [_boy.physicsbody applytorque:(cgfloat)atan2(_boy.physicsbody.velocity.dy, _boy.physicsbody.velocity.dx)];
applytorque
not right method this. torque twisting force causes node rotate around center point.
there no 1 easy command looking do. fail mention method of movement node. apply force, impulse, etc... going have come hack one.
the sample project below looking , point in right direction. going have modify code suit specific project's needs though.
tap/click screen once start moving node , tap/click screen second time start 90 degree movement change.
#import "gamescene.h" @implementation gamescene { int touchcounter; bool changedirection; skspritenode *node0; } -(void)didmovetoview:(skview *)view { self.backgroundcolor = [skcolor whitecolor]; node0 = [skspritenode spritenodewithcolor:[skcolor graycolor] size:cgsizemake(50, 50)]; node0.position = cgpointmake(150, 200); node0.physicsbody = [skphysicsbody bodywithrectangleofsize:node0.size]; node0.physicsbody.affectedbygravity = no; [self addchild:node0]; touchcounter = 0; changedirection = no; } -(void)update:(cftimeinterval)currenttime { if(changedirection) [self changemovement]; } -(void)touchesbegan:(nsset *)touches withevent:(uievent *)event { touchcounter++; (uitouch *touch in touches) { if(touchcounter == 1) [node0.physicsbody applyimpulse:cgvectormake(25, 0)]; if(touchcounter == 2) changedirection = yes; } } -(void)changemovement { if(node0.physicsbody.velocity.dy<200) { [node0.physicsbody applyimpulse:cgvectormake(-0.1, 0.1)]; } else { changedirection = no; node0.physicsbody.velocity = cgvectormake(0, node0.physicsbody.velocity.dy); } }
Comments
Post a Comment