Apply Angular Impulse

Hi guys, this is my player code and the ball that interact with each other. What I want to do is use force on the ball, as if my player was shooting at him. I want the ball to move away from my player with strength. how can i apply momentum or force to this. I tried many times, but I'm new to the Sprite Kit.

- (void) Player { _Player = [SKSpriteNode spriteNodeWithImageNamed:@"player1"]; _Player.xScale = 0.09; _Player.yScale = 0.09; _Player.position = CGPointMake(self.size.width/4, self.size.height/2); _Player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_Player.size]; _Player.physicsBody.dynamic = NO; [self addChild:_Player]; } - (void) TheMethodForBall { SKSpriteNode *sprites = [SKSpriteNode spriteNodeWithImageNamed:@"ball"]; sprites.xScale = 0.19; sprites.yScale = 0.19; sprites.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprites.size]; sprites.physicsBody.dynamic = YES; sprites.physicsBody.affectedByGravity = NO; sprites.physicsBody.allowsRotation = YES; sprites.physicsBody.restitution = YES; sprites.physicsBody.angularVelocity = 4; sprites.physicsBody.usesPreciseCollisionDetection = YES; [self addChild:sprites]; } 
+8
physics sprite-kit
source share
1 answer

I think you want to apply momentum as a blow?

You need the following, maybe when you touch the screen / or button

 [_myBall.physicsBody applyImpulse:CGVectorMake(somePowerX, somePowerY)]; 

Here's another post to get you started.

It is also a good tutorial for beginners.

+7
source share

All Articles