Binary operator '-' cannot be applied to two 'CGPoint' operands error

I'm working on a game for tvOS and the below works for iOS :


    func moveToward(target: CGPoint)
    {
       let targetVector = (target - position).normalized() * 1300.0
       
        physicsBody?.velocity = CGVector(point: targetVector)
    }


Does anyone have any ideas on how I can solve this issue ?

Basically it's throwing out the error on the let targetVector = ..... line.


Any and all help is surely appreciated.

Thanks

There isn't operator overloading for CGPoint addition but you can write your own:


func +(left: CGPoint, right: CGPoint) -> CGPoint {
    return CGPoint(x: left.x + right.x, y: left.y + right.y)
}


E: Subtraction:


func -(left: CGPoint, right: CGPoint) -> CGPoint {
    return CGPoint(x: left.x - right.x, y: left.y - right.y)
}

For normalized, you can create a CGPoint extension:


extension CGPoint {
     func normalized() -> CGVector {
        
     }
}


So you need helper functions to do calculation between different types: CGFloat, CGPoint, CGVector

Just make sure you make them global functions -- the docs all state this, but I missed that point the first time I tried implementing my own operators for CG stuff, and it took a bit of time to figure out why the compiler didn't like them... I had been trying to add them to the CGPoint class.

Binary operator '-' cannot be applied to two 'CGPoint' operands error
 
 
Q