Degree between 2 vectors?
What's wrong with this degree calculation?
I've chosen 2 vectors, which should have a degree of 90°.
I tried it with cos, cos^-1 and without cos calcualtion. but the output of my code is:
0 (without cos calculation) 1 (cosinus) 1.5707963267949 (cosinus^-1(acos))local square = math.sqrt;
local weaponVector, hitVector = , {x = 0, y = 0, z = 1,}, {x = 0, y = 1, z = 0,};
local xW, yW, zW = weaponVector.x, weaponVector.y, weaponVector.z;
local xH, yH, zH = hitVector.x, hitVector.y, hitVector.z;
local angleBetweenWeaponAndHitDirection = math.acos(math.abs(xW*xH+yW*yH+zW*zH)
/ (square(xW*xW+yW*yW+zW*zW) * square(xH*xH+yH*yH+zH*zH)));
if (angleBetweenWeaponAndHitDirection>180) then
开发者_Python百科 angleBetweenWeaponAndHitDirection = 360-angleBetweenWeaponAndHitDirection;
end
print(angleBetweenWeaponAndHitDirection)
print(math.cos(angleBetweenWeaponAndHitDirection))
print(math.acos(angleBetweenWeaponAndHitDirection))
1.57079 is the right answer, but in Radians.
To convert from radians to degrees, multiply by 180/pi.
1.5707963267949 (cosinus^-1(acos))
That looks like 90 degrees ( pi/2)
Math functions work with radians, so your comparisons and operations must be in radians as well, not degrees.
精彩评论