how to draw an arrow in all direction using Two Points, Start point vector and end point vector in SceneKit Swift?

151 Views Asked by At

how can draw an arrow in all direction in sceneview using Two Points( Start point vector and end point vector) see attachment draw arrow like Up side , Downside , left side up and down, right side up and down enter image description here. i have tried to draw arrow using UIBezierPath but arrow shape not looks exactly same as arrow image also it doesn't work for all direction

check below code i have applied , any solution that how can i fix shape same as image or is there any solution that using two points how to draw directional Arrow in sceneview

    let path = UIBezierPath.arrow(from: CGPoint(x: arrowstartx, y: arrowstarty), to: CGPoint(x: arrowendx, y: arrowendy), tailWidth: 0.02, headWidth: 0.02, headLength: 0.02)
    path.close()
    let arrowShape = SCNShape(path: path, extrusionDepth: 0.001)
    let arrowNode = SCNNode(geometry: arrowShape)
    arrowNode.geometry?.firstMaterial?.diffuse.contents = UIColor(hexString: selectedItem ?? myMarkupDefaultColor)
 
    arrowNode.position = SCNVector3((vector1.x+vector3.x)/2.0, (vector1.y+vector3.y)/2.0, (vector1.z+vector3.z)/2.0)
    
    arrowNode.scale = SCNVector3(1.0,1.0, 1.0)

    
    self.sceneViewARMarkup.scene.rootNode.addChildNode(arrowNode)


class func arrow(from start: CGPoint, to end: CGPoint, tailWidth: CGFloat, headWidth: CGFloat, headLength: CGFloat) -> Self {
    let length = hypot(end.x - start.x, end.y - start.y)
    let tailLength = length - headLength
    
    func p(_ x: CGFloat, _ y: CGFloat) -> CGPoint { return CGPoint(x: x, y: y) }
    let points: [CGPoint] = [
        p(0, tailWidth / 2),
        p(tailLength, tailWidth / 2),
        p(tailLength, headWidth / 2),
        p(length, 0),
        p(tailLength, -headWidth / 2),
        p(tailLength, -tailWidth / 2),
        p(0, -tailWidth / 2)
    ]
    
    let cosine = (end.x - start.x) / length
    let sine = (end.y - start.y) / length
    let transform = CGAffineTransform(a: cosine, b: sine, c: -sine, d: cosine, tx: start.x, ty: start.y)
    
    let path = CGMutablePath()
    path.addLines(between: points, transform: transform )
    path.closeSubpath()
    return self.init(cgPath: path)
1

There are 1 best solutions below

0
Voltan On

One way you can do this is to create the arrow with Blender or find an an arrow model. Orient your model inside Blender like this: Stack O:[56183036], load it, and point it at the target.

func setTarget()
    {
        node.constraints = []
        let vConstraint = SCNLookAtConstraint(target: targetNode)
        vConstraint.isGimbalLockEnabled = true
        node.constraints = [vConstraint]
    }