Reputation: 69
Trying to trigger shooting towards tap position in SpriteKit
.
Having trouble calculating CGVector
s, 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
Reputation: 1708
let vector = CGVectorMake(tap.location.x - player.location.x, tap.location.y - player.location.y)
self.bullet.physicsBody?.applyImpulse(vector)
Upvotes: 4