I have an icon in my game with a set of sprites, which I intend to use as an animated projectile when one character shoots another. I am trying to orient this projectile to indicate a target.

I rotate it at the base angle pi / 4.0 to direct it right to the right. then I want the arrow to turn from this position and point to the target.
The code below almost works, but, in my opinion, it always looks as if the projectile is turned off at the right angle. If the angle is correct, the arrow will indicate the direction of movement when the arrow is given an action to go to the x, y coordinate.
How to correctly calculate the angle of fire from one (x, y) point to another (x, y) point?
EDIT: the code below works, just need to do zeroCheckedY / zeroCheckedX.
float baseRotation = M_PI/4.0;
float zeroCheckedY = destination.y - origin.y;
float zeroCheckedX = destination.x - origin.x;
SKAction* rotate = nil;
if(zeroCheckedX != 0)
{
float angle = atanf(zeroCheckedY/zeroCheckedX);
rotate = [SKAction rotateByAngle:baseRotation + angle duration:0];
}else
{
rotate = [SKAction rotateByAngle:baseRotation duration:0];
}
source
share