Lua实现angle角
Posted heyuchang666
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lua实现angle角相关的知识,希望对你有一定的参考价值。
function i3k_vec3(x, y, z)
return x = x, y = y, z = z ;
end
function i3k_vec3_normalize1(v)
return i3k_vec3_normalize2(v.x, v.y, v.z);
end
function i3k_vec3_normalize2(x, y, z)
local _x = x;
local _y = y;
local _z = z;
local d = math.sqrt(_x * _x + _y * _y + _z * _z);
if d ~= 0 then
_x = _x / d;
_y = _y / d;
_z = _z / d;
end
return i3k_vec3(_x, _y, _z);
end
-- only x, z
function i3k_vec3_angle1(p1, p2, p3)
return i3k_vec3_angle2(i3k_vec3(p1.x - p2.x, 0, p1.z - p2.z), p3);
end
function i3k_vec3_angle2(p1, p2)
local _v1 = i3k_vec3_normalize1(p1);
local _v2 = i3k_vec3_normalize1(p2);
local epsilon = 0.000001;
local dot = _v1.x * _v2.x + _v1.z * _v2.z;
local angle = 0;
if math.abs(dot - 1) <= epsilon then
angle = 0;
elseif math.abs(dot + 1) <= epsilon then
angle = math.pi;
else
angle = math.acos(dot);
local cross = _v1.x * _v2.z - _v2.x * _v1.z;
if cross < 0 then
angle = 2 * math.pi - angle;
end
end
return angle;
end
以上是关于Lua实现angle角的主要内容,如果未能解决你的问题,请参考以下文章