GL套件。如何分配给 GLKVector3 的组件

GLKit. How do I assign to a component of a GLKVector3

分配给 GLKVector3(或 4)的字段的正确方法是什么?

这失败了:

这是有道理的,因为 .x.z 是吸气剂。解决方法是什么?

我相信在 Swift 中 GLKVector3 是不可变的,无论是 var 还是 let。使用 GLKVector3Make 创建一个具有任意 Float 值的;随后为新实例生成新结果。

所以:

let ballLocation = GLKVector3Make(
     (locationInBallCoordinates.x - ballCentre.x) / ballRadius,
     0.0,
     (locationInBallCoordinates.y - ballCentre.y) / ballRadius)

或者,如果 locationInBallCoordinatesballCentre 已经是 GLKVector3 的实例——以及我在第一次写这个答案时没有注意到的 y-to-z 组件切换不存在——然后:

let ballLocation = 
    GLKVector3DivideScalar(
        GLKVector3Subtract(locationInBallCoordinates, ballCentre),
        ballRadius)

y 移动到 z 可能是最简单的解包和重新打包,但如果您迫切希望保持高水平,也可以使用矩阵乘法。