《魔兽世界》插件的正弦波方程



我正在为魔兽世界创建一个插件。

我有这个:

if edirection == "moon" then sffem = 105*math.sin(math.pi - math.asin(cpower/105) + math.pi/20 * sfcasttime) end

这工作得很好,但我需要将截断点设置为100和-100。

这是因为我的角色的能量是基于一个正弦波,从0开始,下降到-100,停留几秒钟,回到0,上升到100,停留几秒钟,回到0。

这是有效的,因为正弦波是105,-105能量,但玩家能量的最大值和最小值为100。

我试着:

if edirection == "moon" then sffem = (MAX(-100;MIN(100;105*math.sin(math.pi - math.asin(cpower/105) + math.pi/20 * sfcasttime)))) end

这只是给出一个错误。

我该怎么做?

不需要在一行中完成所有这些操作。例如,在

行之后
if edirection == "moon" then sffem = 105*math.sin(math.pi - math.asin(cpower/105) + math.pi/20 * sfcasttime) end

做一些类似

的事情
if sffem >= 100 then sffem = 100 end
if sffem <= -100 then sffem = -100 end

(感谢Henrik Ilgen提供的语法帮助)

第二行代码使用分号而不是逗号来分隔MAXMIN的参数。

更改后的代码并使用math.minmath.max:

if edirection == "moon" then sffem = math.max(-100,math.min(100,105*math.sin(math.pi - math.asin(cpower/105) + math.pi/20 * sfcasttime))) end
您可能会发现创建一个夹紧辅助函数很有用:
function clamp(value, min, max)
  return math.max(min, math.min(max, value))
end

在这种情况下,你的代码变成这样:

if edirection == "moon" then sffem = clamp(105*math.sin(math.pi - math.asin(cpower/105) + math.pi/20 * sfcasttime), -100, 100) end

最新更新