I have a space ship that I'm applying a force vector to using a joystick for control. I would like this to smoothly fly around when I'm switching directions.. Exactly like minisquadron if you've ever played that..
Let's say I move the joystick to the far right and apply a force on the physics body of (1,0) and then I abruptly switch directions without circling the joystick around and apply a force of (-1,0). The ship will move to the right and then slowly lose speed before switching in a linear fashion.
What I want is for it to slowly transition between those two vectors so that it flies in a tight circle.
I'm trying to figure out the best way to write this.. maybe using a loop.. but I could use some help.
pseudo code:
func joystickMoved(){
let point = pointVector;
self.moveShip(pointVector);
}
func moveShip(pointVector: pointVector){
// get current angle
let curAngle = self.ship.physicsBody.velocity.toAngle()
// get joystick angle
let newAngle = pointVector.toAngle
// while there is a sizeable difference in angles..
// this loop needs to occur with some delay
// so transition doesnt occur too quickly
while fabsf(newAngle - curAngle) > 0.5 {
// make tiny transitions towards newAngle.. this needs more detail
curAngle += 1
let newVelocity = CGPoint(cos(curAngle), sin(curAngle))
self.ship.physicsBody.velocity = CGVector(dx: newVelocity.x, dy: newVelocity.y)
}
}