SceneKit Object between two points

前端 未结 2 1632
不知归路
不知归路 2021-02-11 09:58

Given 2 points in 3D (a,b) and an SCNCapsule, SCNTorus, SCNTube etc., how to go about to position the object, so that the object starts at poin

2条回答
  •  醉梦人生
    2021-02-11 10:17

    I've good news for you ! You can link two points and put a SCNNode on this Vector !

    Take this and enjoy drawing line between two points !

    class   CylinderLine: SCNNode
    {
        init( parent: SCNNode,//Needed to line to your scene
            v1: SCNVector3,//Source
            v2: SCNVector3,//Destination
            radius: CGFloat,// Radius of the cylinder
            radSegmentCount: Int, // Number of faces of the cylinder
            color: UIColor )// Color of the cylinder
        {
            super.init()
    
            //Calcul the height of our line
            let  height = v1.distance(v2)
    
            //set position to v1 coordonate
            position = v1
    
            //Create the second node to draw direction vector
            let nodeV2 = SCNNode()
    
            //define his position
            nodeV2.position = v2
            //add it to parent
            parent.addChildNode(nodeV2)
    
            //Align Z axis
            let zAlign = SCNNode()
            zAlign.eulerAngles.x = CGFloat(M_PI_2)
    
            //create our cylinder
            let cyl = SCNCylinder(radius: radius, height: CGFloat(height))
            cyl.radialSegmentCount = radSegmentCount
            cyl.firstMaterial?.diffuse.contents = color
    
            //Create node with cylinder
            let nodeCyl = SCNNode(geometry: cyl )
            nodeCyl.position.y = CGFloat(-height/2)
            zAlign.addChildNode(nodeCyl)
    
            //Add it to child
            addChildNode(zAlign)
    
            //set constraint direction to our vector
            constraints = [SCNLookAtConstraint(target: nodeV2)]
        }
    
        override init() {
            super.init()
        }
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
        }
    }
    
    private extension SCNVector3{
        func distance(receiver:SCNVector3) -> Float{
            let xd = receiver.x - self.x
            let yd = receiver.y - self.y
            let zd = receiver.z - self.z
            let distance = Float(sqrt(xd * xd + yd * yd + zd * zd))
    
            if (distance < 0){
                return (distance * -1)
            } else {
                return (distance)
            }
        }
    }
    

提交回复
热议问题