Tube Physics Body 像 SCNCylinder 一样工作,但如何让它像 SCNTube 一样工作? [英] Tube Physics Body acting like SCNCylinder, but how to make it act like SCNTube?

查看:49
本文介绍了Tube Physics Body 像 SCNCylinder 一样工作,但如何让它像 SCNTube 一样工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 SceneKit 中制作一个管子和它的物理体.

I am trying to make a tube and its physics body like so in 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)

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

However, when another object falls onto this object it does not pass through the center. Instead it sits appearing to be levitating in air. It is acting like the physics body is a complete cylinder, not like the tube with the hole in the middle. How can I fix this so that objects can pass through the center? The tubes appearance looks as expected.

谢谢!

推荐答案

SceneKit 中的动态物体必须是凸面的.(如果您研究游戏中碰撞检测背后的一般理论,即不仅仅是 SceneKit,您会发现凸面形状和凹面形状的碰撞检测在速度和效率上存在巨大差异.)管是凹面形状 - 它里面有个洞.

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)

这样做会产生性能成本.如果您发现在此更改后帧率受到 CPU 使用率的限制,或者如果您想让动态物体(有效地)凹入,您可以通过制作由其他几种形状组成的物理形状来获得更好的性能 - 例如构建一个虚拟节点层次结构(不是实际在您的场景中的一个),其中包含一堆排列成环的圆柱体或盒子,然后使用 SCNPhycsicsShape(node:options:) 将它们制成物理形状.

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:).

这篇关于Tube Physics Body 像 SCNCylinder 一样工作,但如何让它像 SCNTube 一样工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆