创建与节点几何体大小相同的physicsShape

时间:2014-09-25 04:43:42

标签: ios swift scenekit

我有两个盒子。顶盒附有动态physicsBody,底盒附有静态主体。

使用:

创建physicShapes
topBox.physicsBody = SCNPhysicsBody(type: SCNPhysicsBodyType.Dynamic, shape: nil)
bottomBox.physicsBody = SCNPhysicsBody(type: SCNPhysicsBodyType.Static, shape: nil)

当它们停下来时,顶部的方框比它应该高,表明physicsShape比它应该大。这是静止的对象:

enter image description here

如何创建与您要将其附加到的节点的几何体相同大小/形状的physicsShape?

仅供参考:SCNScene来自导入的DAE文件,从Cheetah 3D导出。

3 个答案:

答案 0 :(得分:5)

尽管从头开始创建形状,但我遇到了同样的问题。我认为问题是SCNPhysicsShape.ShapeType的默认convexHull值会创建convex polyhedron roughly enclosing the geometry。这总是比一个盒子大。您可以通过启用.showPhysicsShapes调试选项来确认这一点:

sceneView.debugOptions = .showPhysicsShapes

所以下面的代码通过使用.boundingBox选项

来帮助我们
let boxBodyShape = SCNPhysicsShape(geometry: SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0),
                                    options: [SCNPhysicsShape.Option.type: SCNPhysicsShape.ShapeType.boundingBox])

let boxBody = SCNPhysicsBody(type: .kinematic, shape: boxBodyShape)

答案 1 :(得分:2)

Swift 4.1 / Xcode 9.3

不使用nil / NULL赋值,而是使用实际节点的几何。

let node = SCNNode()
node.geometry = SCNBox(width: 0.20, height: 0.25, length: 0.10, chamferRadius: 0)

let physicsShape = SCNPhysicsShape(geometry: node.geometry)
let physicsBody = SCNPhysicsBody(type: .dynamic, shape: physicsShape)
node.physicsBody = physicsBody

那就行了。

答案 2 :(得分:0)

 SCNBox* JustAHappyBox=[SCNBox boxWithWidth:1 height:1 length:1 chamferRadius:0];

 SCNNode* TopBoxNode=[SCNNode nodeWithGeometry:JustAHappyBox];

 SCNPhysicsShape* TopBoxPhysicsShape=[SCNPhysicsShape shapeWithGeometry:JustAHappyBox options:@{SCNPhysicsShapeTypeKey:SCNPhysicsShapeTypeConvexHull }];

 SCNPhysicsBody* TheBoxBody=[SCNPhysicsBody bodyWithType:SCNPhysicsBodyTypeDynamic shape:TopBoxPhysicsShape];

 [TopBoxNode setPhysicsBody:TheBoxBody];

希望这会有所帮助。

Btw场景套件在识别它们来自.dae的Colision体时出现问题,它与视觉几何体和碰撞几何体的转换有关。不好意思地玩弄它,如果我弄清楚生病让你知道。