My bullet is finally being removed after touching the enemy however the enemy isnt being removed. Is something wrong with my physicscontact code? My player is also now going out of the screen border but i just fixed that by removing both bodyB and bodyA nil lines. I dont know if thats the best way to fix the 2nd problem and if it will mess up my enemy code in the future.
func didBegin(_ contact: SKPhysicsContact) {
if (contact.bodyA.categoryBitMask == bulletCategory) {
contact.bodyA.node?.physicsBody?.collisionBitMask = 0
contact.bodyA.node?.physicsBody?.categoryBitMask = 0
} else if (contact.bodyB.categoryBitMask == bulletCategory) {
contact.bodyB.node?.physicsBody?.collisionBitMask = 0
contact.bodyB.node?.physicsBody?.categoryBitMask = 0
}
if contact.bodyA.categoryBitMask == enemyCategory {
contact.bodyB.node?.removeFromParent()
contact.bodyB.node?.physicsBody = nil
contact.bodyB.node?.removeAllActions()
} else if contact.bodyB.categoryBitMask == enemyCategory {
contact.bodyA.node?.removeFromParent()
contact.bodyA.node?.physicsBody = nil
contact.bodyA.node?.removeAllActions()
}
}
Related
I am trying to get a player to collide with a SKNode. Doing so, I want a particle to display an exposition where the SKNode just was(player) and display the effect. When I tried to create this, the emitter is not displaying where the player is. How can I fix this?
func didBegin(_ contact: SKPhysicsContact) {
let bodyA = contact.bodyA
let bodyB = contact.bodyB
if bodyA.categoryBitMask == 1 && bodyB.categoryBitMask == 2 || bodyB.categoryBitMask == 1 && bodyA.categoryBitMask == 2 {
let player = SKNode(fileNamed: "player")
let explostion = SKEmitterNode(fileNamed: "explosion.sks")
let addExplosion = SKAction.run {
player?.addChild(explostion!)
}
let seq = SKAction.sequence([addExplosion])
self.addChild(explostion!)
self.run(seq)
}
}
Probably one of physics body belongs to player's node? So you just can access it from body. For example let's player's body - is bodyA:
if bodyA.categoryBitMask == 1 && bodyB.categoryBitMask == 2 || bodyB.categoryBitMask == 1 && bodyA.categoryBitMask == 2 {
let player = bodyA.node
let explostion = SKEmitterNode(fileNamed: "explosion.sks")
player.addChild(explostion!)
}
}
I am new to iOS development and I made an infinite runner game. As you can see in the function below, the player dies when it collides with an obstacle. However, the player dies whenever it collides with any side of the obstacle. How can I limit this collision detection to a specific side of the frame of the obstacle?
func didBegin(_ contact: SKPhysicsContact) { //so the player can't jump while in the air
var firstBody = SKPhysicsBody()
var secondBody = SKPhysicsBody()
if contact.bodyA.node?.name == "Player" {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if firstBody.node?.name == "Player" && secondBody.node?.name == "Ground" { //if colliding with ground
canJump = true //can jump when on ground
}
if firstBody.node?.name == "Player" && secondBody.node?.name == "Bus" || firstBody.node?.name == "Player" && secondBody.node?.name == "Obstacle" {
// kill player and prompt buttons
playerDied()
}
}
You may try to calculate the intersection between two colliding nodes, then just checking width and height of the resulting frame should be enough to detect the collision side, a rough explanation:
collision left/right
collision top/down
for a infinite runner game this check should be enough:
if firstBody.node?.name == "Player" && secondBody.node?.name == "Obstacle" {
let intersection = firstBody.node!.frame.intersection(secondBody.node!.frame)
if intersection.height > intersection.width { // collision left/right
playerDied()
} else { // collision top/down
//...//
}
}
Creating a game in Swift and SprieKit.
I have 2 nodes, a BridgeNode and WaterNode. One on top of the other.
Both have physics bodies to detect when the player is either on the bridge on in the water. Both nodes are added independently as child nodes of the Scene.
When the player node jumps onto the Bridge, DidBegin detects contact with
both the Water and Bridge nodes. I only want it to detect the Bridge node as the player is safely on the Bridge OR if the player is in the water.
func didBegin(_ contact: SKPhysicsContact) {
// Did Begin Contact - Contact Testing and actions
let player1 = (contact.bodyA.categoryBitMask == player1Mask) ? contact.bodyA : contact.bodyB
let other = (player1 == contact.bodyA) ? contact.bodyB : contact.bodyA
if other.categoryBitMask == bridgeMask {
print("BRIDGE CONTACT")
}
else if other.categoryBitMask == waterMask {
// Contacted Water
print("WATER CONTACT")
}
}
The console is printing both print statements always in a random order.
Hope someone can help me to just detect one or the other.
You mentioned that it is a top-down game, so when you have the bridge on top of the water the player will obviously contact both at the same time, there is no "blocking" of the physicsBody underneath the bridge. You need to do something like this in your SKPhysicsContactDelegate:
var playerIsOnBridge = false
func didBegin(_ contact: SKPhysicsContact) {
let player1 = (contact.bodyA.categoryBitMask == player1Mask) ? contact.bodyA : contact.bodyB
let other = (player1 == contact.bodyA) ? contact.bodyB : contact.bodyA
if other.categoryBitMask == bridgeMask || playerIsOnBridge {
playerIsOnBridge = true
print("BRIDGE CONTACT")
} else if other.categoryBitMask == waterMask {
print("WATER CONTACT")
}
}
func didEnd(_ contact: SKPhysicsContact) {
let bitmaskA = contact.bodyA.categoryBitMask
let bitmaskB = contact.bodyB.categoryBitMask
if bitmaskA == bridgeMask || bitmaskB == bridgeMask {
playerIsOnBridge = false
}
}
I'm using SpriteKit and Swift to build an iPhone game, and my collision detection system is not functioning as I want it to. When my "bullet" physics body and my "player" physics body collide, the collision detection function is being called multiple times, usually 4-12 times. I have tried setting "usesPreciseCollisionDetection" to true, but it is still problematic. Also, it seems the method is called more times when the bullet hits the player at an angle rather than straight on. Any thoughts how to fix this?
Collision Types:
enum ColliderType:UInt32 {
case Player = 0b1
case Bullet = 0b10
}
Player Physics Body Settings:
playerBody!.categoryBitMask = ColliderType.Player.rawValue
playerBody!.contactTestBitMask = ColliderType.Bullet.rawValue
playerBody!.collisionBitMask = ColliderType.Bullet.rawValue
Bullet Physics Body Settings:
bulletBody!.categoryBitMask = ColliderType.Bullet.rawValue
bulletBody!.contactTestBitMask = ColliderType.Player.rawValue
bulletBody!.collisionBitMask = ColliderType.Player.rawValue
Collision Detection Method:
func didBeginContact(contact: SKPhysicsContact) {
if (contact.bodyA.categoryBitMask == 0b1) && (contact.bodyB.categoryBitMask == 0b10) {
playerVbullet(contact.bodyA, bullet: contact.bodyB)
}
if (contact.bodyA.categoryBitMask == 0b10) && (contact.bodyB.categoryBitMask == 0b11) {
playerVbullet(contact.bodyB, bullet: contact.bodyA)
}
}
Function Called on Collision:
func playerVbullet(player:SKPhysicsBody, bullet:SKPhysicsBody) {
bullet.node?.removeFromParent()
collisions++
println(collisions)
}
Give your bullet node a name:
let bullet = SKSPriteNode()
bullet.name = "bullet"
Then check your physics contact
if (contact.bodyA.node?.name == "bullet") {
contact.bodyA.node?.removeFromParent()
} else if contact.bodyB.node?.name == "bullet" {
contact.bodyB.node?.removeFromParent()
}
Good luck!
func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyA.categoryBitMask == PhysicsCategory.overPow || contact.bodyB.categoryBitMask == PhysicsCategory.overPow{
let sk = delegateForCollision!.view as! SKView
let newScene = GG(fileNamed: "GG")
newScene!.delegateFor = delegateForCollision
newScene?.scaleMode = .AspectFill
sk.presentScene(newScene)
}
if contact.bodyA.categoryBitMask == PhysicsCategory.glem && contact.bodyB.categoryBitMask == PhysicsCategory.kappa{
contact.bodyA.node?.removeFromParent()
glem++
glemLabel.text = "SKAŁY: \(glem)"
}
if contact.bodyB.categoryBitMask == PhysicsCategory.glem && contact.bodyA.categoryBitMask == PhysicsCategory.kappa{
contact.bodyB.node?.removeFromParent()
glem++
glemLabel.text = "SKAŁY: \(glem)"
}
}
glem and kappa categoryBitMask nodes are colliding so glem variable should be ++ one time then node is removed from scene but it looks like this method is called more times during next frames. I see it in logs because I added print("\(glem)") in glem didSet. Why does it happen?
I changed glem++ to if contact.bodyB.node?.parent != nil{glem++}
and now it works as it should.