如何将 SCNPhysicsBody 定位在 scnnode 的中心

How to position a SCNPhysicsBody at the center of a scnnode

我有一个 SCNNode 和一个完整的工作物理体,但物理体 center/resting-point 位于 SCNNode 的底部。

这是我正在尝试做的事情:

到目前为止,Apple Developer 还没有帮助我。这是我拥有的:

let nodePhysicsBody = SCNPhysicsBody(type: .dynamic, shape: SCNPhysicsShape(geometry: SCNCapsule(capRadius: 2.0, height: 2.0), options: nil))

我找到了这个 init 方法,但我不确定它是否有效,或者如何实现它。

.init(circleOfRadius r: CGFloat, center: CGPoint()

我建议尝试使用 SCNPhysicsShape init(shapes:transforms:) 初始值设定项——而不是用多个形状构造一个物理形状(并使用 transforms 参数将它们相对定位), 你也许能够传递一个单一的形状和一个从父坐标中心偏移它的变换 space.

举一个完整的例子(我的物理以我的 .dae 动画的基础为中心,我需要以 SCNNode 的质心为中心的物理体):

// refNode is the SCNReferenceNode loaded from the .dae animation
 let kCharacterScale = SCNVector3(20.0,20.0,20.0)   //Set the node and physics scales
 let boundingBox = refNode.boundingBox
 let min = boundingBox.min
 let max = boundingBox.max
 let w = (max.x - min.x) 
 let h = (max.y - min.y) 
 let l = (max.z - min.z) 
 let boxShape = SCNBox (width: CGFloat(w) , height: CGFloat(h) , length: CGFloat(l), chamferRadius: 0.0)
 var shape = SCNPhysicsShape(geometry: boxShape, options: [SCNPhysicsShape.Option.scale : kCharacterScale, SCNPhysicsShape.Option.type : SCNPhysicsShape.ShapeType.boundingBox])
 let translate = SCNMatrix4MakeTranslation(0.0, (h*kCharacterScale.y/2.0), 0.0)
 let translateMatrix = NSValue.init(scnMatrix4: translate)
 shape = SCNPhysicsShape.init(shapes: [shape], transforms: [translateMatrix])
        refNode.physicsBody = SCNPhysicsBody(type: .kinematic, shape: shape)