lua
lua

Reputation: 1

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
    angleBetweenWeaponAndHitDirection = 360-angleBetweenWeaponAndHitDirection;
end
print(angleBetweenWeaponAndHitDirection)
print(math.cos(angleBetweenWeaponAndHitDirection))
print(math.acos(angleBetweenWeaponAndHitDirection))

Upvotes: 0

Views: 249

Answers (3)

m0skit0
m0skit0

Reputation: 25873

Math functions work with radians, so your comparisons and operations must be in radians as well, not degrees.

Upvotes: 1

Thilo
Thilo

Reputation: 262684

1.5707963267949 (cosinus^-1(acos))

That looks like 90 degrees ( pi/2)

Upvotes: 1

Patrick
Patrick

Reputation: 1148

1.57079 is the right answer, but in Radians.

To convert from radians to degrees, multiply by 180/pi.

Upvotes: 9

Related Questions