简体   繁体   中英

Is this the correct method of using dot product to find the angle between two vectors? C++ SFML

So in another question I was told that you could use a simplified formula of the dot product to find the angle between two vectors:

angle = atan2(mouseY, mouseX) - atan2(yPos, xPos); //xPos, yPos is position of player

Except, from what I understood it simply takes points as vectors. Which is why the mouse and player position is plugged in the parameters. However when I move the mouse around the player in a 360 degree radius, the angle and therefore rotation of the player is a value from around -0.3... to -1.4... Is this not the correct method? Am I supposed to be representing vectors not as X, Y positions but as something else?

There's also another formula I found which also doesn't seem to work for me:

angle = atan2(mouseY - yPos, mouseX - xPos);

The first method is correct, the second is wrong. The function atan2(x,y) computes the angle in radians of a vector pointing from (0,0) to (x,y) with respect to the x-axis. It is thus correct to calculate two angles (in radians) the vectors have wih respect to the x-axis and then subtract these from each other to obtain the angles between the two vectors.

The result of the function atan2 is a value in the half-open interval (-pi,pi] . Expressed in degrees, this corresponds to (-180°,180°) . 0° thereby denotes a vector pointing right (along the x-axis), 90° a vector pointing up, 180° a vector pointing left and -90° a vector pointing down.

You can transform the result in radians to angles via the formula

atan(x,y)*180/pi

So, if you want to transform your resulting values into angles, just multiply them by 180/pi .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM