2

我正在尝试在 SceneKit 中制作一个管子及其物理体。

let BoxGeometry = SCNTube(innerRadius: 5, outerRadius: 12.5, height: 4)
Box = SCNNode(geometry: BoxGeometry)
Box.pivot = SCNMatrix4MakeRotation(Float(M_PI_2/8), 0, 1, 0)
Box.physicsBody = SCNPhysicsBody(type: SCNPhysicsBodyType.Static, shape: nil)
Box.physicsBody?.mass = 5
Box.categoryBitMask = colorCategory
scene.rootNode.addChildNode(Box)

但是,当另一个物体落到这个物体上时,它不会穿过中心。相反,它似乎悬浮在空气中。它的作用就像物理体是一个完整的圆柱体,而不是中间有孔的管子。我该如何解决这个问题,以便物体可以通过中心?管子的外观与预期的一样。

谢谢!

4

2 回答 2

6

Dynamic bodies in SceneKit must be convex. (If you look into the general theory behind collision detection in games, i.e. not just SceneKit, you'll find that there are massive differences in speed and efficiency between collision detection on convex versus concave shapes.) A tube is a concave shape — it has a hole in it.

Luckily, your tube is being used as a static body. For static bodies only, there's an option to make the physics shape (an approximation of) a concave geometry:

let shape = SCNPhysicsShape(geometry: tube, 
    options: [SCNPhysicsShapeTypeKey: SCNPhysicsShapeTypeConcavePolyhedron])
box.physicsBody = SCNPhysicsBody(type: .Static, shape: shape)

There is a performance cost to this. If you find your framerate limited by CPU usage after this change, or if you want to have a dynamic body be (effectively) concave, you might get better performance by making a physics shape that's a compound of several other shapes — e.g. build a dummy node hierarchy (not one that's actually in your scene) containing a bunch of cylinders or boxes arranged into a ring, then make a physics shape from them with SCNPhycsicsShape(node:options:).

于 2014-12-10T22:00:38.503 回答
0

更改physicsBody 的形状以匹配管的几何形状。

于 2014-12-10T13:49:46.070 回答