use*_*506 4 game-physics ios scenekit
我对如何在Scenekit中处理节点空间坐标和旋转感到迷惑。
如何获得节点旋转后面向的方向,以便可以在该方向上施加力。
我假设沿-z轴施加到该节点的力将始终使它相对于该节点向前移动,但是事实并非如此。
我也无法convertPosition:toNode上班。变化ship.convertPosition(SCNVector3(0,0,-0.1), toNode: ship.parentNode)使节点以无法预测的方向和速度飞翔。
对我有用的是获取节点worldTransform矩阵的第三行,该行对应于其z前向轴:
func getZForward(node: SCNNode) -> SCNVector3 {
return SCNVector3(node.worldTransform.m31, node.worldTransform.m32, node.worldTransform.m33)
}
ship.position += getZForward(ship) * speed
// if node has a physics body, you might need to use the presentationNode, eg:
// getZForward(ship.presentationNode)
// though you probably don't want to be directly modifying the position of a node with a physics body
// overrides for SCNVector arithmetic
#if os(iOS)
typealias VecFloat = Float
#elseif os (OSX)
typealias VecFloat = CGFloat
#endif
func + (lhs: SCNVector3, rhs: SCNVector3) -> SCNVector3 {
return SCNVector3(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z)
}
func * (lhs: SCNVector3, rhs: VecFloat) -> SCNVector3 {
return SCNVector3(lhs.x * rhs, lhs.y * rhs, lhs.z * rhs)
}
func += (lhs: inout SCNVector3, rhs: SCNVector3) {
lhs = lhs + rhs
}
Run Code Online (Sandbox Code Playgroud)
如果节点具有物理实体,则可能必须将传递node.presentationNode给函数。
当iOS的11 /高塞拉利昂出来,就会有不太需要重写SCNVector3,因为所有的SCNNode属性都simd等同,所以还有.position有.simdPosition等,并且有很多内置的通用SIMD操作。
iOS 11增加了方便的便利功能,可用于获取节点的方向。在这种情况下,该worldForward属性就是您想要的属性。而且,SCNNode该返回SCNVector类型和矩阵类型上的所有属性现在都具有返回simd类型的版本。由于simd对于算术运算符已经有重载,因此您不再需要为SCNVector和SCNMatrix类型添加算术重写集。
因此,我们可以摆脱getZForward上面的out 方法,只需要一行代码:
ship.simdPosition += ship.simdWorldFront * speed
Run Code Online (Sandbox Code Playgroud)
iOS 11添加的另一套便捷方法是一组convertVector方法,以补充现有convertPosition方法。convertVector是等效由矢量使基质与乘以0在w位置上,从而使矩阵的平移被忽略。这些是用于将法线,方向等东西从一个节点的空间转换到另一个节点的适当方法。
因为接受的答案使用convertPosition,所以我相信它将仅对转换为0,0,0起点的节点产生正确的结果
负 z 是节点仅在其自己的坐标空间(即其子节点和几何体所在的坐标空间)中的“面向”方向。当您施加力时,您正在包含节点的坐标空间中工作。
因此,如果要在节点的前进方向上施加力,则需要使用类似 的方法将向量{0,0,-1}从节点的空间转换为其父节点的空间convertPosition:toNode:。
| 归档时间: |
|
| 查看次数: |
2880 次 |
| 最近记录: |