Search code examples
sprite-kitxcode7skphysicsbodyskscene

Moving SKSpriteNodes collision behaviour with physics


I am making a game in which the player moves around a world by applying a force to the player (whether the player should move left or right is decided by the xVelocity value which is either -200 or 200) in the update method.

override func update(currentTime: NSTimeInterval) {
    player?.physicsBody?.applyForce(CGVectorMake(xVelocity, 0))
    enemy?.physicsBody?.applyForce(CGVectorMake(enemyxVelocity, 0))
}

I also have some enemies which move, also using force on the x axis. When the enemy and the player collide, the enemy gets knocked back and the player is able to keep moving, but this is the opposite of what I want. I would like the enemies to be a lot stronger momentum-wise and actually shove the player out of the way.

I am using the Scene Editor of Xcode to make this and I am not sure what parts of the physics bodies properties would be relevant to give me the desired effect?


Solution

  • Try adjusting the mass of each character in your game

    https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKPhysicsBody_Ref/index.html#//apple_ref/occ/instp/SKPhysicsBody/mass

    "The mass property determines how forces affect the body, as well as how much momentum the body has when it is involved in a collision."