Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Trouble manipulating an angle based on the mouse position.  (Read 2033 times)

0 Members and 1 Guest are viewing this topic.

Snizzlenose

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Trouble manipulating an angle based on the mouse position.
« on: June 02, 2015, 06:28:04 pm »
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;
                        }
                }
        }
}

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Trouble manipulating an angle based on the mouse position.
« Reply #1 on: June 02, 2015, 06:41:50 pm »
Atan is usually x over y because it is opposite over adjacent and adjacent is vertical. Also, atan is in degrees, so why the *180/pi?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Trouble manipulating an angle based on the mouse position.
« Reply #2 on: June 02, 2015, 06:44:50 pm »
std::atan2. Yes, the std is required in correct C++.

But you could use thor::polarAngle() which would greatly simplify your code. See my signature (Thor -> API doc -> Vectors module) :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Trouble manipulating an angle based on the mouse position.
« Reply #3 on: June 02, 2015, 06:46:06 pm »
Is there really any advantage of std::atan2(y,x) over std::atan(y/x)? Also, yes I see it is in radians, I misread cplusplus.com documentation.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Trouble manipulating an angle based on the mouse position.
« Reply #4 on: June 02, 2015, 06:52:58 pm »
Yes, exactly because it considers the quadrant. And it doesn't fail for x==0.

You should read the docs more carefully, or also consider cppreference.com... ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Snizzlenose

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Trouble manipulating an angle based on the mouse position.
« Reply #5 on: June 02, 2015, 07:41:49 pm »
std::atan2. Yes, the std is required in correct C++.

But you could use thor::polarAngle() which would greatly simplify your code. See my signature (Thor -> API doc -> Vectors module) :)

Could you explain how thor::polarAngle( ) is defined? 
I don't understand the result I got when I searched for it in the github repo...
template <typename T>
T polarAngle(const sf::Vector2<T>& vector)
{
        assert(vector != sf::Vector2<T>());
        return TrigonometricTraits<T>::arcTan2(vector.y, vector.x);
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Trouble manipulating an angle based on the mouse position.
« Reply #6 on: June 02, 2015, 10:55:33 pm »
You answer your own question by providing the function definition... So what do you mean?

If you mean how it is used:
sf::Vector2f playerPos = ...;
sf::Vector2f mousePos = ...;
sf::Vector2f direction = mousePos - playerPos;

float angle = thor::polarAngle(direction);
float distance = thor::length(direction);

Here is the link to the documentation, by the way.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything