SFML community forums

Help => Graphics => Topic started by: electrux on August 21, 2014, 08:08:32 am

Title: Angle between 2 points
Post by: electrux 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  :(
Title: Re: Angle between 2 points
Post by: Laurent 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
Title: Re: Angle between 2 points
Post by: electrux 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);
Title: Re: Angle between 2 points
Post by: Laurent 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
Title: Re: Angle between 2 points
Post by: electrux on August 21, 2014, 01:43:55 pm
Error: class sf::RenderWindow has no member pixelToCoords :/
Title: Re: Angle between 2 points
Post by: Laurent 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 ;)
Title: Re: Angle between 2 points
Post by: MadMartin 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.
Title: Re: Angle between 2 points
Post by: electrux on August 21, 2014, 02:11:36 pm
ok ok sorry guys i will refer the api... tyvm btw :) lemme try it :D
Title: Re: Angle between 2 points
Post by: paupav 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.
Title: Re: Angle between 2 points
Post by: electrux 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 :/
Title: Re: Angle between 2 points
Post by: math1992 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.
Title: Re: Angle between 2 points
Post by: Laurent 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 ;)
Title: Re: Angle between 2 points
Post by: math1992 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||) )
Title: Re: Angle between 2 points
Post by: Laurent 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.
Title: Re: Angle between 2 points
Post by: electrux 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?
Title: Re: Angle between 2 points
Post by: zsbzsb on August 21, 2014, 06:14:24 pm
If all you want to do is move something between two points you don't even need to calculate the angle. Something as simple as the following works.

sf::Vector2f norm = pt1 - pt2;
myObject.setPosition(pt1);

while (window.isOpen())
{
   myObject.move(norm * deltaTime);
}
Title: Re: Angle between 2 points
Post by: electrux on August 21, 2014, 06:39:09 pm
huh?! it was that easy?! lol and i was even bothering my math teacher for a way >_<.. anyways thanks very much :D SFML rocks!!!! best library i seen to start with game programming (btw m new to game programming so sorry i dont know almost everything...) thanks again :D
Title: Re: Angle between 2 points
Post by: electrux on August 22, 2014, 07:21:55 am
ok sorry but how did u define deltaTime? i tried Vector2f but it give me error that no function matches the operand *
Title: Re: Angle between 2 points
Post by: electrux on August 22, 2014, 07:40:53 am
nevermind... got it working by multiplying by float thanks :D and again, SFML FTW!!!!
Title: Re: Angle between 2 points
Post by: AlexAUT on August 22, 2014, 07:55:24 am
ok sorry but how did u define deltaTime?

deltaTime is the frametime, which you should multiply with nearly every movement to prevent that your movements will be faster when you have more frames per seconds. Here is a great article about this topic (http://gafferongames.com/game-physics/fix-your-timestep/)



AlexAUT
Title: Re: Angle between 2 points
Post by: Nexus on August 23, 2014, 09:06:34 pm
If you don't want to reinvent the wheel, you could use Thor.Vectors (http://www.bromeon.ch/libraries/thor/v2.0/doc/_vector_algebra2_d_8hpp.html) that I have written. The module provides a lot of vector-related functionality, including the computation of angles in various situations.