苹果选择只允许在世界范围内进行碰撞检测,而不是允许单个物体与其他物体碰撞时进行检测,是否有特殊原因?这似乎是一个可怕的设计选择,但由于我没有发现太多提示,我猜我错过了一些东西。那么,有吗?
最佳答案
这里有一些很棒的评论,但也不知道为什么。他们将整个事情基于节点和节点名称 - 似乎它可能会更好,因为事后查找事情会更慢。这是我的处理方式,与其他人所说的类似。
由于我有一堆导弹,我只是将节点命名为 = "Missi"+ String(format: "%04d", vIndex), name = "Explo"+ String(format: "%04d", vIndex)等等,这样我就可以在我要查找的内容前面添加前缀,然后直接转到我的节点数组并执行我的操作。我有数百个东西在进行,但碰撞函数非常小。
func physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact)
{
guard let nodeNameA = contact.nodeA.name else { return }
guard let nodeNameB = contact.nodeB.name else { return }
//print("CONTACT: \(nodeNameA) \(nodeNameB)")
if(nodeNameA.prefix(5) == "Explo" && nodeNameB.prefix(5) == "Enemy")
{
gameControl.defenseMissileHit(vIndex: Int(nodeNameB.suffix(4))!)
}
if(nodeNameA.prefix(5) == "Enemy" && nodeNameB.prefix(5) == "Explo")
{
gameControl.defenseMissileHit(vIndex: Int(nodeNameA.suffix(4))!)
}
if(nodeNameA.prefix(5) == "Explo" && nodeNameB.prefix(5) == "Missi")
{
//print("HIT")
gameControl.enemyMissileHit(vIndex: Int(nodeNameB.suffix(4))!)
}
if(nodeNameA.prefix(5) == "Missi" && nodeNameB.prefix(5) == "Explo")
{
//print("HIT")
gameControl.enemyMissileHit(vIndex: Int(nodeNameA.suffix(4))!)
}
}
我不会对节点进行子类化,而是使用我想要的内容创建 DefenseObject 等类,然后创建一个可以直接访问或循环访问的数组。
var defenseObjects: [Int:DefenseObject] = [:]
class DefenseObject: ObjectBase
{
var index: Int = 0 // Index of object
var name: String = "" // Object name
var isActive: Bool = false // Object is active
init(vGameType: gameTypes, vCount: Int, position: SCNVector3)
{
super.init(vGameType: vGameType, vIndex: vCount)
isActive = true
name = "Enemy" + String(format: "%04d", vIndex)
}
}
然后我可以直接进入索引并执行 DefenseObjects[NDex].call()。我也不会尝试在 Wave 过程中清理节点,我设置了一个 isActive 开关并隐藏它们。在浪潮结束时,我清理它们。
希望对您有所帮助。
关于ios - 为什么无法进行物体内碰撞检测?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56223628/