Ray
Ray

Reputation: 161

How to do impulse physics

Okay guys, I have two sprites one the enemy who moves to pick up items around the map as the items appear and it does that using SKActions then also I have the wall which he shouldn't go through. anyway. all I am trying to do is to make him bounce out if he hits the wall. How would I go about that?

Here is the code below:

// for the Enemy wall I have this code:

- (void) EnemyBelongings {
EnemyWall = [SKSpriteNode spriteNodeWithImageNamed:@"EnemyWall@2x"];
EnemyWall.name = @"hisWall";
EnemyWall.position = CGPointMake(512, 260);
EnemyWall.xScale = 0.09;
EnemyWall.yScale = 0.09;
EnemyWall.physicsBody.categoryBitMask = PhysicsCategoryEnemyWall;
EnemyWall.physicsBody.contactTestBitMask = PhysicsCategoryEnemy;
EnemyWall.physicsBody.collisionBitMask = 0;
[self addChild:EnemyWall];

}

// For the enemy character I have this code

- (void) Enemy {
    _Enemy = [SKSpriteNode spriteNodeWithImageNamed:@"enemy"];
    _Enemy.position = CGPointMake(520, _Enemy.size.height/1.50);
    _Enemy.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.size.width];
    _Enemy.physicsBody.usesPreciseCollisionDetection = YES;
    _Enemy.physicsBody.categoryBitMask = PhysicsCategoryEnemy;
    _Enemy.physicsBody.contactTestBitMask = PhysicsCategoryEnemyWall;
    _Enemy.physicsBody.collisionBitMask = 0;
    [self addChild:_Enemy];
}

// For the enemy movement I have this code

- (void) EnemySpawner {
    [ForEnemy runAction:appear2 completion:^{
    SKAction *wait = [SKAction waitForDuration:1.0];
    [_Enemy runAction:wait];
    SKAction *actionXMove2 = [SKAction moveToX:ForEnemy.position.x duration:0.14];
    SKAction *actionYMove2 = [SKAction moveToY:ForEnemy.position.y duration:0.14];
    [_Enemy runAction:actionYMove2];
    [_Enemy runAction:actionXMove2];
}];

}

-(void)didBeginContact:(SKPhysicsContact *)contact
{
if (contact.bodyA.categoryBitMask == PhysicsCategoryEnemy && contact.bodyB.categoryBitMask 
    == PhysicsCategoryEnemyWall)
{
    SKNode *enemy = contact.bodyA.node;  // a is the enemy here
       // Code here
    SKNode *wall = contact.bodyB.node; // b is the enemy's wall here
       // Code here
}
else if (contact.bodyB.categoryBitMask == PhysicsCategoryEnemy && 
    contact.bodyA.categoryBitMask == PhysicsCategoryEnemyWall)
{
    SKNode *enemy = contact.bodyB.node;
       // Code here
    SKNode *wall = contact.bodyA.node;
       // Code here
}

}

Upvotes: 0

Views: 636

Answers (2)

ZeMoon
ZeMoon

Reputation: 20274

You need to applyImpulse on the node's physicsBody to make it simulate naturally and interact with other physicsBodies.

Please look at my answer here on how to do the same.

Copy the rwAdd, rwSub, rwMult, rwLength and the rwNormalize methods from this tutorial by Ray Wenderlich.

Then, try using this code:

[ForEnemy runAction:appear2 completion:^{
        SKAction *wait = [SKAction waitForDuration:1.0];

        CGPoint offset = rwSub(location, ForEnemy.position);
        CGPoint direction = rwNormalize(offset);
        float forceValue = 200; //Edit this value to get the desired force.
        CGPoint shootAmount = rwMult(direction, forceValue);

        CGVector impulseVector = CGVectorMake(shootAmount.x, shootAmount.y);

        [_Enemy.physicsBody applyImpulse:impulseVector];


         }];

Upvotes: 1

JKallio
JKallio

Reputation: 903

PhysicsWorld should simulate bounces automatically. Just set your nodes' physics properties accordingly. Look for the 'restitution' property. It will determine the bounciness of your objects.

And don't use SKAction to move your enemies. This will just "teleport" your nodes around. They don't get to have any real velocity in the physicsWorld (and hence don't bounce). Instead, apply forces to your bodies (or set velocity vector manually).

Upvotes: 0

Related Questions