Creating path using CGMutablePath creates line to wrong CGPoint

≯℡__Kan透↙ 提交于 2019-12-04 05:55:42

问题


I was planning to display information of AR object in screen with arrow in 2D. So I used projectPoint to get corresponding position of object in screen. I have this function to return convert 3D position of node to 2D and CGPoint to display info text in.

func getPoint(sceneView: ARSCNView) -> (CGPoint, CGPoint){
    let projectedPoint = sceneView.projectPoint(node.worldPosition)
    return (point, CGPoint(x: CGFloat(projectedPoint.x), y: CGFloat(projectedPoint.y)) )
}

and this to draw line using SpriteKit:

let (f,s) = parts[3].getPoint(sceneView: sceneView) 
line.removeFromParent()
let path = CGMutablePath()
path.move(to: f)
path.addLine(to: s)
line = SKShapeNode(path: path)
spriteScene.addChild(line)

This is what i get

What I expect is another end of line to be fixed in node (blue mesh).
Is there something I am missing? Or does projectPoint works some other way?

edit: It seems projectPoint is returning correct value but while creating path path.addLine(to: s) this point is shifting to different position.


回答1:


path.addLine(to: s) s here had reversed y so this did the trick

let frame = self.sceneView.frame
let sInversed = CGPoint(x: from.x, y: frame.height - s.y)

path.addLine(to: sInversed)

Here origin of SKScene was in bottom left of screen instead of top left.



来源:https://stackoverflow.com/questions/49443228/creating-path-using-cgmutablepath-creates-line-to-wrong-cgpoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!