I'm trying to rotate a rectangle, representing an gun, around the center of the player position. It's rotating normally, aiming towards the mouse, as long as the mouse position x is less than the arm position x. If it goes above that it rotates 180 degrees and instead aims in the opposite direction. I made a hacky fix by add 180 to the angle reactively, but it's pretty buggy and I'm wondering if there's any better and more proper solution?
void Player::StartShooting( float mouse_x, float mouse_y, float timestep )
{
for( unsigned int i = 0; i < bullet.size( ); i++ )
{
if( bullet[i].GetActive( ) == false )
{
bullet[i].SetOrigin( GetPosition( ).x + GetHitbox( ).x / 2, GetPosition( ).y + GetHitbox( ).y / 2 );
sf::Vector2f mousePos = { mouse_x, mouse_y };
sf::Vector2f projEndNorm = vectorNormal( mousePos - bullet[i].GetOrigin( ) );
bullet[i].SetPosition( bullet[i].GetOrigin( ) + projEndNorm );
float angle = atan( projEndNorm.y / projEndNorm.x ) * 180.f / 3.141592f;
if( mouse_x >= GetPosition( ).x )
{
angle += 180;
}
shapeArm.setRotation( angle );
if( projEndNorm.x != 0 &&
projEndNorm.y != 0 )
{
bullet[i].SetActive( true );
bullet[i].SetStep( 0 );
bullet[i].SetVelocity( vectorNormal( bullet[i].GetPosition( ) - bullet[i].GetOrigin( ) ).x * bullet[i].GetInitialVelocity( ) + GetVelocity( ).x / 5,
vectorNormal( bullet[i].GetPosition( ) - bullet[i].GetOrigin( ) ).y * bullet[i].GetInitialVelocity( ) + GetVelocity( ).y / 5 );
break;
}
}
}
}