使用 SKPhysics body 检测碰撞
Using SKPhysics body for detecting collisions
所以我正在创建一个游戏,我只想检测玩家节点和敌人发射的子弹之间的碰撞。所以我为每个玩家和子弹设置了正确的参数和 categoryBitMask 和 contactTestBitMask。
通过实现 didBegin 和 didEnd 函数,我想在碰撞开始和结束时执行一些代码。问题是,当我构建和 运行 项目时,物理子系统将子弹移动到玩家周围而不是穿过玩家。这是因为 isDynamic 属性 对于玩家、子弹或两者都设置得太正确。显然,首先要尝试的是将 isDynamic 属性 设置为 false;但是,当我这样做时,发生碰撞时没有回调,并且没有执行 didBegin / didEnd 函数。
有人知道如何解决这个问题吗?
设置玩家和子弹物理体的代码;还有下面的didBegin函数供大家参考
playerNode!.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: playerNode!.size.width/2, height: playerNode!.size.height/2)) //sets the physics body for the player
playerNode!.physicsBody!.isDynamic = false //we dont want the physics to be simulated by the subsystem
playerNode!.physicsBody!.affectedByGravity = false
playerNode!.physicsBody!.pinned = false
playerNode!.physicsBody!.allowsRotation = false
playerNode!.physicsBody!.categoryBitMask = PhysicsCategory.Player
playerNode!.physicsBody!.contactTestBitMask = PhysicsCategory.Bullet
self.bullet?.physicsBody = SKPhysicsBody(circleOfRadius: bullet!.size.width * 0.5)
self.bullet?.physicsBody?.isDynamic = true
self.bullet?.physicsBody?.pinned = false
self.bullet?.physicsBody?.allowsRotation = false
self.bullet?.physicsBody?.affectedByGravity = false
self.bullet?.physicsBody?.categoryBitMask = PhysicsCategory.Bullet
self.bullet?.physicsBody?.contactTestBitMask = PhysicsCategory.Player
func didBegin(_ contact: SKPhysicsContact) {
let collision = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
if collision == PhysicsCategory.Player | PhysicsCategory.Bullet {
print("There was a collision with the player and a bullet")
}
}
//Defines the physics category for the game
struct PhysicsCategory {
static let None: UInt32 = 0
static let Player: UInt32 = 0b1
static let Bullet: UInt32 = 0b10
}
子弹在玩家周围“移动”而不是穿过玩家,因为您已打开碰撞。默认情况下,所有物理体都会与所有其他物理体发生碰撞,而默认情况下,物理体之间没有注册接触。
首先要做的是关闭玩家和子弹之间的碰撞,反之亦然,同时不影响所有其他碰撞:
playerNode.physicsBody?.collisionBitMask &= ~physicsCategory.bullet
bullet.physicsBody?.collisionBitMask &= ~physicsCategory.player
编辑:我的碰撞和接触分步指南:
碰撞和接触测试位掩码指南:
操纵位掩码以关闭和打开单个碰撞和接触。
所以我正在创建一个游戏,我只想检测玩家节点和敌人发射的子弹之间的碰撞。所以我为每个玩家和子弹设置了正确的参数和 categoryBitMask 和 contactTestBitMask。
通过实现 didBegin 和 didEnd 函数,我想在碰撞开始和结束时执行一些代码。问题是,当我构建和 运行 项目时,物理子系统将子弹移动到玩家周围而不是穿过玩家。这是因为 isDynamic 属性 对于玩家、子弹或两者都设置得太正确。显然,首先要尝试的是将 isDynamic 属性 设置为 false;但是,当我这样做时,发生碰撞时没有回调,并且没有执行 didBegin / didEnd 函数。
有人知道如何解决这个问题吗?
设置玩家和子弹物理体的代码;还有下面的didBegin函数供大家参考
playerNode!.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: playerNode!.size.width/2, height: playerNode!.size.height/2)) //sets the physics body for the player
playerNode!.physicsBody!.isDynamic = false //we dont want the physics to be simulated by the subsystem
playerNode!.physicsBody!.affectedByGravity = false
playerNode!.physicsBody!.pinned = false
playerNode!.physicsBody!.allowsRotation = false
playerNode!.physicsBody!.categoryBitMask = PhysicsCategory.Player
playerNode!.physicsBody!.contactTestBitMask = PhysicsCategory.Bullet
self.bullet?.physicsBody = SKPhysicsBody(circleOfRadius: bullet!.size.width * 0.5)
self.bullet?.physicsBody?.isDynamic = true
self.bullet?.physicsBody?.pinned = false
self.bullet?.physicsBody?.allowsRotation = false
self.bullet?.physicsBody?.affectedByGravity = false
self.bullet?.physicsBody?.categoryBitMask = PhysicsCategory.Bullet
self.bullet?.physicsBody?.contactTestBitMask = PhysicsCategory.Player
func didBegin(_ contact: SKPhysicsContact) {
let collision = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
if collision == PhysicsCategory.Player | PhysicsCategory.Bullet {
print("There was a collision with the player and a bullet")
}
}
//Defines the physics category for the game
struct PhysicsCategory {
static let None: UInt32 = 0
static let Player: UInt32 = 0b1
static let Bullet: UInt32 = 0b10
}
子弹在玩家周围“移动”而不是穿过玩家,因为您已打开碰撞。默认情况下,所有物理体都会与所有其他物理体发生碰撞,而默认情况下,物理体之间没有注册接触。
首先要做的是关闭玩家和子弹之间的碰撞,反之亦然,同时不影响所有其他碰撞:
playerNode.physicsBody?.collisionBitMask &= ~physicsCategory.bullet
bullet.physicsBody?.collisionBitMask &= ~physicsCategory.player
编辑:我的碰撞和接触分步指南:
碰撞和接触测试位掩码指南:
操纵位掩码以关闭和打开单个碰撞和接触。