Search code examples
scenekitscnvector3

Creating an array of SCNVector3 using Float in Swift


I have a bunch of Z coordinates in an array. I want to create geometry using these values, thus I created an array of SCNVector3. X and Y values will be calculated using a simple nested loop, but the Z values will come from my array.

Xcode is telling me: Cannot assign value of type '(x: Float, y: Float, z: Float)' to type SCNVector3

Am I making a bonehead mistake here?

My code right now is:

var width = 10
var height = 20
var zDepth = [Float]()  
var verts = [SCNVector3]()  
var i = 0  
for y in 0 ..< height {  
    for x in 0 ..< width {  
        if i < (width*height) {  
            vert[i] = (x: Float(x), y: Float(y), z: zDepth[i])  
            i += 1  
        }
    }
}

Solution

  • for what platform is this?

    if it is for mac os, then you have to use CGFloat not Float

    EDIT:

    you can't assign a tuple to vector3. in your code you need to initialize a new vector3 and assign it to your array

    var width = 10
    var height = 20
    var zDepth = [Float]()
    var verts = [SCNVector3]()
    var i = 0
    for y in 0 ..< height {
        for x in 0 ..< width {
            if i < (width*height) {
                verts[i] = SCNVector3(x: Float(x), y: Float(y), z: zDepth[i])
                i += 1
            }
        }
    }
    

    EDIT2: i took out cgfloat (i keep thinking in cgfloats)