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

Author Topic: Angle between 2 points  (Read 12074 times)

0 Members and 1 Guest are viewing this topic.

electrux

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Angle between 2 points
« on: August 21, 2014, 08:08:32 am »
Hello everyone,
i am trying to get the angle between 2 points (1 of player and other of mouse).
i use this:

float getangle(float _x1,float _y1, float _x2, float _y2) // x1 and y1 are pos of mouse and x2 and y2 are pos of player
{
   float _ang, _pi = 3.1428;
   _ang = atan2(_y2 - _y1,_x2 - _x1) * (180/_pi);
   return _ang;
}

but it only gives me 4rth quadrant angle about 45 degree.
i saw another topic about it but was unable to understand... please help  :(

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Angle between 2 points
« Reply #1 on: August 21, 2014, 08:49:28 am »
How do you get the mouse coordinates?

PS: I don't know where you got your PI value, but it's wrong
Laurent Gomila - SFML developer

electrux

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Angle between 2 points
« Reply #2 on: August 21, 2014, 10:53:33 am »
i used windows calculator for value of pi (ripped off some values after point(.) and i used this for mouse:
sf::Vector2i mousepos = sf::mouse::getPosition(window);

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Angle between 2 points
« Reply #3 on: August 21, 2014, 11:47:31 am »
It won't make any difference if you're not using views, but a more correct code is:

sf::Vector2f mousepos = window.pixelToCoords(sf::mouse::getPosition(window));

Quote
i used windows calculator for value of pi (ripped off some values after point(.)
You should double-check what you copied :P
Laurent Gomila - SFML developer

electrux

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Angle between 2 points
« Reply #4 on: August 21, 2014, 01:43:55 pm »
Error: class sf::RenderWindow has no member pixelToCoords :/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Angle between 2 points
« Reply #5 on: August 21, 2014, 02:04:40 pm »
Sorry, it's mapPixelToCoords.

You can also search a little bit by yourself in the API doc, instead of blindly copying and compiling what we tell you ;)
Laurent Gomila - SFML developer

MadMartin

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Angle between 2 points
« Reply #6 on: August 21, 2014, 02:05:46 pm »
It's mapPixelToCoords:
http://sfml-dev.org/documentation/2.1/classsf_1_1RenderTarget.php#a2b0cab0e4c6af29d4efaba149d28116d

Why didn't you read in the API-docs? They are there for a reason...



'EDIT: Laurent was faster.

electrux

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Angle between 2 points
« Reply #7 on: August 21, 2014, 02:11:36 pm »
ok ok sorry guys i will refer the api... tyvm btw :) lemme try it :D

paupav

  • Full Member
  • ***
  • Posts: 156
    • View Profile
    • Email
Re: Angle between 2 points
« Reply #8 on: August 21, 2014, 02:15:12 pm »
Sorry, it's mapPixelToCoords.

You can also search a little bit by yourself in the API doc, instead of blindly copying and compiling what we tell you ;)
Or he could simply write "window." and his IDE would suggest him. Its easier than asking it again.

electrux

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Angle between 2 points
« Reply #9 on: August 21, 2014, 02:20:50 pm »
ok apparantly my angle never changes... i cout'ed the sin(angle) and its always -0.8... now lemme see where i did the mistake that the angle isnt calculated properly :/

math1992

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
    • Email
Re: Angle between 2 points
« Reply #10 on: August 21, 2014, 02:31:40 pm »
Be careful with trigonometric function, they only works with rectangle-triangles (As one 90 degres angle).

If your triangle is not rectangle then use scalar product.

Also to calculate an angle you need three points, not 2.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Angle between 2 points
« Reply #11 on: August 21, 2014, 02:37:55 pm »
Quote
Be careful with trigonometric function, they only works with rectangle-triangles (As one 90 degres angle).

If your triangle is not rectangle then use scalar product.

Also to calculate an angle you need three points, not 2.
Take a look at the documentation of the atan2 function and come back to correct this very wrong & confusing answer please ;)
Laurent Gomila - SFML developer

math1992

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
    • Email
Re: Angle between 2 points
« Reply #12 on: August 21, 2014, 04:01:23 pm »
I do know what is an arctan. My english is not very good and I probably didn't said what I wanted to say.

I will rephrase:

atan2 is waiting for two value x and y and after compute arctan(y/x), however the y and x value must come from
a rectangle triangle.

Try computing the angle between pointA = (0,3), pointB  = (1/2, sqrt(3)/2) using the formula of electrux gives an angle of 90... but the true angle is 30 degres between them.

On the other hand the scalar product gives the correct angle:

<A,B> = Ax Bx + Ay By = ||A|| ||B|| cos (angle) => angle = acos( (Ax Bx + Ay By) / (||A|| ||B||) )

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Angle between 2 points
« Reply #13 on: August 21, 2014, 04:22:05 pm »
X and Y values, by definition, will always be "perpendicular", since they represent values on perpendicular axes. I think that you're overthinking the problem.

The OP computes the vector between two points, and passes its components to atan2 to get the corresponding angle. This is how everyone does it, there's nothing wrong here.
Laurent Gomila - SFML developer

electrux

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Angle between 2 points
« Reply #14 on: August 21, 2014, 05:52:32 pm »
ah this degree thing dont work for me... ima see degrees later but atm,
can i create an equation of straight line and use it to move my projectile along the vector? using 2 point formula and then entering a varying (i++ or something) in x coordinate to get y?
will it work?

 

anything