Attila Zalanyi
Attila Zalanyi

Reputation: 69

How to calculate Impulse direction towards tap position in SWIFT?

Trying to trigger shooting towards tap position in SpriteKit. Having trouble calculating CGVectors, need some help.

I have the player position : player.position I have the tap location: tap.location

Want to apply an impulse like this:

self.bullet.physicsBody?.velocity = CGVectorMake(19, 0)
self.bullet.physicsBody?.applyImpulse(CGVectorMake(19, 0))

Not sure how to write up the calculation of the vector in Swift. Could you guys point me in the right direction? Thank you.

Upvotes: 3

Views: 934

Answers (1)

Kendel
Kendel

Reputation: 1708

let vector = CGVectorMake(tap.location.x - player.location.x, tap.location.y - player.location.y)
self.bullet.physicsBody?.applyImpulse(vector)

Upvotes: 4

Related Questions