SFML community forums

Help => General => Topic started by: mathme on July 16, 2013, 09:37:22 pm

Title: Angle between two vectors
Post by: mathme on July 16, 2013, 09:37:22 pm
I have been trying out different methods all day to calculate the angle between 2 vectors. I've browsed countless forums and read way too many stackoverflow questions. No matter what method I implement I keep getting the same result.

The angle I'm getting is only good in the bottom right quadrant of the player, and is only exactly correct at 45 degrees.

I've tried the dot product way with acos, I've tried the atan2 method with delta X and delta Y. I just can't seem to get a working function.

The goal is to be able to aim a bullet from the player to the mouse.

Does anyone have a function that works to do this?
Title: Re: Angle between two vectors
Post by: Nexus on July 16, 2013, 09:48:24 pm
The goal is to be able to aim a bullet from the player to the mouse.
Why would you need an angle for that? Just take the difference vector from both positions.


Does anyone have a function that works to do this?
Polar angle of single vector: thor::polarAngle()
Signed angle between two vectors: thor::signedAngle()

Check the API documentation (http://www.bromeon.ch/libraries/thor/v2.0/doc/_vector_algebra2_d_8hpp.html) of Thor.
Title: Re: Angle between two vectors
Post by: cpolymeris on July 17, 2013, 01:20:44 am
Does anyone have a function that works to do this?

The Law of Cosines works. If it doesn't, you must be doing something wrong (or not be in Euclidian space, but you would have said if that was the case  :)) ). Since you don't go into detail, here are some things to check:

I know it's kinda obvious stuff, but since you don't supply code, I am not sure what else you could be doing wrong.

Edit: If you need to know the direction (which point is behind the other), remember to use acos2, or that atan2 function you mention.
Title: Re: Angle between two vectors
Post by: mateandmetal on July 19, 2013, 04:37:37 pm
The goal is to be able to aim a bullet from the player to the mouse.

You need a direction vector (mouse position - player position)
Then normalize that vector (make it a unit vector)
Now, add this normalized vector to your bullet velocity and multiply by the bullet movement speed (a float) and the delta time (as float)

Title: Re: Angle between two vectors
Post by: Gobbles on July 19, 2013, 06:46:04 pm
Quote from: mateandmetal
You need a direction vector (mouse position - player position)
Then normalize that vector (make it a unit vector)
Now, add this normalized vector to your bullet velocity and multiply by the bullet movement speed (a float) and the delta time (as float)

depending on what hes using for a bullet image, he might also need to rotate the image to match that of the player. However this is fairly simple, just use the players current angle at the time of the shot and set your bullet to that.