Tuesday, 15 June 2010

swift - Get radius of a SCNSphere that is used to create a SCNNode -



swift - Get radius of a SCNSphere that is used to create a SCNNode -

how homecoming radius of sphere geometry(scnsphere) used set scnnode. want utilize radius in method move kid nodes in relation parent node. code below fails since radius appears unknown resulting node, should not pass node method?

also array index fails saying int not range.

i trying build this

import uikit import scenekit class primitivesscene: scnscene { override init() { super.init() self.addspheres(); } func addspheres() { allow spheregeometry = scnsphere(radius: 1.0) spheregeometry.firstmaterial?.diffuse.contents = uicolor.redcolor() allow spherenode = scnnode(geometry: spheregeometry) self.rootnode.addchildnode(spherenode) allow secondspheregeometry = scnsphere(radius: 0.5) secondspheregeometry.firstmaterial?.diffuse.contents = uicolor.greencolor() allow secondspherenode = scnnode(geometry: secondspheregeometry) secondspherenode.position = scnvector3(x: 0, y: 1.25, z: 0.0) self.rootnode.addchildnode(secondspherenode) self.attachchildrenwithangle(spherenode, children:[secondspherenode, spherenode], angle:20) } func attachchildrenwithangle(parent: scnnode, children:[scnnode], angle:int) { allow parentradius = parent.geometry.radius //this fails cause geometry not know radius. var index = 0; index < 3; ++index{ children[index].position=scnvector3(x:float(index),y:parentradius+children[index].radius/2, z:0);// fails saying int not convertible range. } } required init(coder adecoder: nscoder) { fatalerror("init(coder:) has not been implemented") } }

the issue radius parent.geometry returns scngeometry , not scnsphere. if need radius, you'll need cast parent.geometry scnsphere first. safe, it's best utilize optional binding , chaining that:

if allow parentradius = (parent.geometry as? scnsphere)?.radius { // utilize parentradius here }

you'll need when accessing radius on children nodes. if set , clean things little, this:

func attachchildrenwithangle(parent: scnnode, children:[scnnode], angle:int) { if allow parentradius = (parent.geometry as? scnsphere)?.radius { var index = 0; index < 3; ++index{ allow kid = children[index] if allow childradius = (child.geometry as? scnsphere)?.radius { allow radius = parentradius + childradius / 2.0 child.position = scnvector3(x:cgfloat(index), y:radius, z:0.0); } } } }

note though you're calling attachchildrenwithangle array of 2 children:

self.attachchildrenwithangle(spherenode, children:[secondspherenode, spherenode], angle:20)

if that, you're going runtime crash in for loop when accessing 3rd element. you'll either need pass array 3 children every time phone call function, or alter logic in for loop.

swift scenekit scnnode

No comments:

Post a Comment