围绕轴旋转SCNVector3

时间:2017-08-30 17:23:45

标签: ios linear-algebra scenekit trigonometry

这可能更像是一个线性代数问题,但我说我有一个SCNVector,我想要一个新的SCNVector,它与y轴(或任何轴)的原始角度成一定角度。理想情况下:

extension SCNVector3 {  
    // assume dot, cross, length, +, - functions are available.
    enum Axis {
        case x, y, z
    }
    func rotatedVector(aroundAxis: Axis, angle: Float) -> SCNVector3 {
        // code from smart person goes here
    }
}

e.g。 (0,0,-1).rotatedVector(aroundAxis: y, angle: pi/2) = (1,0,0)

谢谢!

3 个答案:

答案 0 :(得分:1)

一般情况下使用Rodrigues' rotation formula

  

Rodrigues的'旋转公式是一种有效的旋转算法   给定轴和旋转角度的空间矢量

对于初始矢量 v ,旋转轴单位矢量 k 和角度θ结果

v rot = v * cos(theta)+( k x v )sin(theta)+ k *( k .dot。 v )*(1 - cos(theta))

答案 1 :(得分:1)

尝试使用四元数 https://developer.apple.com/documentation/accelerate/working_with_quaternions

extension SCNVector3 {
    enum Axis {
        case x, y, z
        
        func getAxisVector() -> simd_float3 {
            switch self {
            case .x:
                return simd_float3(1,0,0)
            case .y:
                return simd_float3(0,1,0)
            case .z:
                return simd_float3(0,0,1)
            }
        }
    }

    func rotatedVector(aroundAxis: Axis, angle: Float) -> SCNVector3 {
        /// create quaternion with angle in radians and your axis
        let q = simd_quatf(angle: angle, axis: aroundAxis.getAxisVector())
        
        /// use ACT method of quaternion
        let simdVector = q.act(simd_float3(self))
        
        return SCNVector3(simdVector)
    }
}

使用:

let a = SCNVector3(5,0,0)

let b = a.rotatedVector(aroundAxis: SCNVector3.Axis.y, angle: -.pi/2)

// SCNVector3(x: 0.0, y: 0.0, z: 5.0)

您还可以围绕任何向量旋转:

let simdVector = q.act(simd_normalize(simd_float3(x: 0.75, y: 0.75, z: -0.2)))

答案 2 :(得分:0)

Well thanks to a comment on gamedev.net I think I have what I need. It doesn't work for arbitrary angles but as it turns out I only need 90° so this works for me. Here's the solution:

RANK
COMPANY
GROWTH
REVENUE
INDUSTRY
1
Skillz
50,058.92%
$54.2m
Software
2
EnviroSolar Power
36,065.06%
$37.4m
Energy