简体   繁体   English

如何将 SCNPhysicsBody 定位在 scnnode 的中心

[英]How to position a SCNPhysicsBody at the center of a scnnode

I have a SCNNode and a full working physics body attached to it, but the physics bodies center/resting-point is at the bottom of the SCNNode .我有一个SCNNode和一个完整的工作物理体,但物理体中心/静止点位于SCNNode的底部。

Here's what I'm trying to do:这是我想要做的:

  • Figure out how to position the physics body at the center of the node.弄清楚如何将物理体定位在节点的中心。
  • Figure out how to control its positioning in some way.弄清楚如何以某种方式控制其定位。

Apple Developer hasn't help me so far.到目前为止,Apple Developer 还没有帮助我。 Here is what I have:这是我所拥有的:

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

I found this init method, but I'm not sure if it will work, or how to implement it.我找到了这个 init 方法,但我不确定它是否有效,或者如何实现它。

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

我建议尝试SCNPhysicsShape init(shapes:transforms:)初始值设定项——而不是从几个形状中构建一个物理形状(并使用transforms参数将它们相对于彼此定位),您可能能够传递单个形状以及将其从父坐标空间的中心偏移的变换。

For a full example (my physics were centered about the base of my .dae animation, and I needed the physics body centered at the center of mass of the SCNNode):举一个完整的例子(我的物理以我的 .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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM