I can't manage to set a proper impulse to a Sprite depending on the touched location

So I'm a beginner in Swift and I'm doing my first simple game, a ball that you can touch and make it jump with impulse. Thing is, as the title says, if I touch on the left side, it would impulse it to the right, depending also on the angle, the same applies for the right side, it would impulse it to the left.


I got my ball that starts with affectedByGravity on false until you touch the screen.


ball = SKSpriteNode(imageNamed: "ball")
ball.name = ballCategoryName 
ball.userInteractionEnabled = false 
ball.setScale(0.3)
ball.zPosition = 2 
ball.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2) 
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width / 2) 
ball.physicsBody?.friction = 0 ball.physicsBody?.restitution = 0.7 
ball.physicsBody?.linearDamping = 0 
ball.physicsBody?.allowsRotation = true 
ball.physicsBody?.categoryBitMask = PhysicsCategory.ball 
ball.physicsBody?.dynamic = true 
ball.physicsBody?.affectedByGravity = false


And then, I check for the touchesBegan:


for touch in touches {
     let location = touch.locationInNode(self)
     let node = self.nodeAtPoint(location)
}


If the node.name is equal to "ball" means i'm touching the ball.


ball.physicsBody?.velocity = CGVectorMake(0, 0);
let ballCenter = CGVectorMake(ball.position.x, ball.position.y)
let reflectedPoint = CGVectorMake(2 * ballCenter.dx - location.x, 2 * ballCenter.dy - location.y)
print(location.x, location.y, " vs ", reflectedPoint.dx, reflectedPoint.dy)

ball.physicsBody?.applyImpulse(reflectedPoint, atPoint: location)


This is always pushing the ball to right, even though the math is correct to give me the opposite X,Y point, always impulsing to right and due to the atPoint: location, the ball spins super fast sometimes.


Can anyone give me some hints, please? This is driving me crazy.


I attempted so many other things that I add but the post would end up huge.


ADDED

The best example of what I'm doing is like the new Facebook mini-game in Messenger from the phone, with the Soccer Ball icon. If tapped on bottom, ball goes up towards Y coordinate, X remains 0 or almost 0. If barely tap on left side, it would barely move to right, when I start to go further through the left, the wider it gets the impulse to right but if you go farther than the half of the left side, it would take it as if you tap on bottom left. Same applies for Right side.


Here's a link to the video to demonstrate what I'm trying to achieve

https://www.youtube.com/watch?v=QQBFzUA92B0


Thanks.

Hiya, I would use something like this to work out the vector from the touch location to the ball location.

func vectorFrom(point pointA: CGPoint, to pointB: CGPoint) -> CGVector {
    let vector = CGVector(dx: pointB.x - pointA.x, dy: pointB.y - pointA.y)
    return vector
}


You could use this as follows:

let dirVector = vectorFrom(point: location, to: ball.position)
ball.physicsBody?.applyImpulse(dirVector)


If needed you can always normalize the dirVector and multiply it by a magnitude to get better control over the impulse velocity.

I can't manage to set a proper impulse to a Sprite depending on the touched location
 
 
Q