我有一个具有复杂几何图形的太空船对象,并且由于SceneKit的物理不适用于复杂的物体,因此我采用了一种解决方法:我使用了一些基本形状和立方体如此模拟整个飞船的身体。在搅拌机我创建一组对象近似于飞船的形状:碰撞没有正确检测到多个几何形状
然后,当予加载予删除这些对象的场景,但使用它们的几何形状来构造SCNPhysicsShape用作飞船的物理体:
// First I retrieve all of these bodies, which I named "Body1" up to 9:
let bodies = _scene.rootNode.childNodes(passingTest: { (node:SCNNode, stop:UnsafeMutablePointer<ObjCBool>) -> Bool in
if let name = node.name
{
return name.contains("Body")
}
return false
})
// Then I create an array of SCNPhysicsShape objects, and an array
// containing the transformation associated to each shape
var shapes = [SCNPhysicsShape]()
var transforms = [NSValue]()
for body in bodies
{
shapes.append(SCNPhysicsShape(geometry: body.geometry!, options: nil))
transforms.append(NSValue(scnMatrix4:body.transform))
// I remove it from the scene because it shouldn't be visible, as it has
// the sole goal is simulating the spaceship's physics
body.removeFromParentNode()
}
// Finally I create a SCNPhysicsShape that contains all of the shapes
let shape = SCNPhysicsShape(shapes: shapes, transforms: transforms)
let body = SCNPhysicsBody(type: .dynamic, shape: shape)
body.isAffectedByGravity = false
body.categoryBitMask = SpaceshipCategory
body.collisionBitMask = 0
body.contactTestBitMask = RockCategory
self.spaceship.physicsBody = body
的SCNPhysicsShape对象应包含我在搅拌机文件创建的形状。但是当我测试这个程序时,宇宙飞船就像一个空的物体,并且没有发现碰撞。
PS:我的目标只是检测碰撞。我不希望物理引擎模拟物理。
我怀疑其他人曾试图自己在做什么。很少有人使用SceneKit。似乎更少用于游戏。其他人不太可能导入物理体。我认为您需要为两到三名在此闲逛的SceneKit员工提供一个示例打包项目。他们可能是唯一可以帮助解决这个问题的人。 – Confused
如果你看看那些回答了很多SceneKit问题的家伙,我想你可以弄清楚他们是谁。而且你有这么多的代表,你可以跟他们聊聊聊天室,或者其他一些直接交流的方式。 – Confused
@Confused你有什么建议用于游戏? –