正弦波方程式用于魔兽世界插件。

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

我有这个:

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

但是只给出一个错误。

我该如何做到这一点?

点赞
用户3238611
用户3238611

不需要将所有内容都放在一行中。例如,在以下一行之后:

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 的语法帮助)

2015-08-21 12:20:12
用户2969217
用户2969217

你的第二行代码使用了分号而不是逗号来分隔 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
2015-08-21 21:46:01