I'm creating a game, and I have a canon that shoots bullets. I have already the code to change the orientation of the canon, :
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if self.nodeAtPoint(location) == self {
if location.x < self.size.width / 2 {
let x = location.x
let y = location.y
let AB = self.size.width / 2 - x
let angleRadians = atan(AB/y)
let angleDegrees = angleRadians * (180 / CGFloat(M_PI))
canon.zRotation = angleDegrees * (CGFloat(M_PI) / 180)
}
if location.x > self.size.width / 2 {
let x = location.x
let y = location.y
let AB = x - self.size.width / 2
let angleRadians = atan(AB/y)
let angleDegrees = angleRadians * (180 / CGFloat(M_PI))
canon.zRotation = -(angleDegrees * (CGFloat(M_PI) / 180))
}
}
}
and I want to determine the direction of the bullet when the user shoots, I tried with an action (moveBy) but I didn't find a relation between the orientation of the canon, and the vector of the bullet's direction. If the location of the touch is CGPoint(x : 100, y : 100), the vector cannot be CGVectorMake(100, 100), because it is too big values !
I tested with a moveTo action, the direction of the bullet was perfect, but the bullet stopped on the touch location, and I want that the bullet continue to move forever !
Thanks for your help !