swift - 如何比较场景套件中两个球体的大小?

标签 swift collision-detection scenekit renderer

我的项目中有 2 个球体在同一位置。 firstSphere 较小,tempSphere 较大。

编辑: firstShape 会比 tempShape 大。当我暂停 firstShape 时,我正在尝试像“if firstShape.scale == tempShape.scale”这样的测试

//Create Shape
let firstShapeGeo = SCNSphere(radius: shapeRadius)
firstShape.geometry = firstShapeGeo
let shapeMaterial = SCNMaterial()
shapeMaterial.diffuse.contents = UIColor(colorLiteralRed: 0.2, green: 0.8, blue: 0.9, alpha: 1.0)
firstShapeGeo.materials = [shapeMaterial]
firstShape.position = SCNVector3Make(0, 0, 0)
scene.rootNode.addChildNode(firstShape)
firstShape.name = "\(shapeNumber)"

// Create Temp Shape
tempShapeRadius = shapeRadius + 1.0

let tempShapeGeo = SCNSphere(radius: tempShapeRadius)
tempShape.geometry = tempShapeGeo
let tempShapeMaterial = SCNMaterial()
tempShapeMaterial.diffuse.contents = UIColor(colorLiteralRed: 0.2, green: 0.8, blue: 0.9, alpha: 0.5)
tempShapeGeo.materials = [tempShapeMaterial]
firstShape.position = SCNVector3Make(0, 0, 0)
scene.rootNode.addChildNode(tempShape)

这就是我种植 firstSphere 的方式

let grow = SCNAction.scale(to: tempShapeRadius * 2 + 1, duration: 1)
let shrink = SCNAction.scale(to: tempShapeRadius, duration: 1)

let sequence = SCNAction.sequence([grow, shrink])
firstShape.run(SCNAction.repeatForever(sequence))

较小的球体通过使用 SCNAction.scale 变大,所以我不相信半径实际上在改变。不确定这是否是您需要了解的内容。

任何帮助将不胜感激!谢谢!

最佳答案

为两个球体分配相同的半径。将 tempShapescale 设置为比 firstShape 的初始值大所需数量的任何值。现在您只需要比较 scale

假设您希望 tempShape 球体的大小是可调整球体的 3 倍。

let tempScale: CGFloat = 3.0
tempShape.scale = SCNVector3(tempScale, tempScale, tempScale)
// grow from 1 to tempScale, then back to 1
let grow = SCNAction.scaleTo(tempScale, duration: 1)
let shrink = SCNAction.scaleTo(1, duration: 1)
let sequence = SCNAction.sequence([grow, shrink])
firstShape.run(SCNAction.repeatForever(sequence))

关于swift - 如何比较场景套件中两个球体的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38735152/

相关文章:

javascript - 如何使用 KineticJS 检测可拖动圆形何时与另一个圆形重叠?

ios - 用于从 cloudkit 中检索单列的代码模式/片段

swift - Firebase 存储

ios - Swift tableView.dequeueReusableCell 从不返回 Nil

c# - 如何测试一个矩形是否在另一个矩形中?

javascript - 算法 - 旋转矩形内矩形的命中检测

ios - 角色在场景包中没有移动

ios - 尝试在触摸开始时向上旋转小鸟,并在触摸结束时向下旋转(SceneKit)

ios - iOS场景中如何将多个节点加入一个节点

swift - “链接”仅在 macOS 11.0 或更高版本中可用