Original Post
Hello for my game I have a sprite that rotates to face a target. Currently the sprite rotates and follows the target as it should. However now I am trying to get the sprite to fire a bullet in the direction it is facing. Im fairly certain I have the right idea but am just missing something in the trigonometry.
I find the angle to rotate by taking the arctan of the differences in x and y between the sprite and the target. I am thinking that once I have this angle I should be able to take the cosine of that same angle and multiple it by the speed to get the x velocity of the bullet and sine for the y. The bullets shoot off at angles sometimes but in all sorts of directions. I think it has to do with the differences in domain between arctan and sine and cosine. So things end up being 180 degree off what they should or something. Any ideas?
int targetX, targetY;
targetX = front.getX();
targetY = front.getY();
rotation = atan2((targetY-y),(targetX-x));
rotation = rotation * (180/3.14159265);
sprite.setRotation(1*rotation);
fireRate.update();
if(fireRate.check2())
{
bullets->push_back(Bullet(x,y,bulletSpeed*cos(rotation),bulletSpeed*sin(rotation),100));
}I find the angle to rotate by taking the arctan of the differences in x and y between the sprite and the target. I am thinking that once I have this angle I should be able to take the cosine of that same angle and multiple it by the speed to get the x velocity of the bullet and sine for the y. The bullets shoot off at angles sometimes but in all sorts of directions. I think it has to do with the differences in domain between arctan and sine and cosine. So things end up being 180 degree off what they should or something. Any ideas?