Lua自身angle角を実現


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

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