Ricky Pulford
Ricky Pulford

Reputation: 11

Sine wave equation for World of Warcraft add-on

I am creating an add-on for World of Warcraft.

I have this:

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

This is working fine, but I need to put cutoff points at 100 and -100.

This is because my character's energy is based on a sine wave starting at 0 going down to -100 staying there for a few seconds coming back up to 0 going up to 100 and staying for a few seconds and returning to 0.

This works because the sine wave is for 105, -105 energy but there is a max and min player energy of 100.

I tried:

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

This just gives an error.

How can I do this?

Upvotes: 1

Views: 121

Answers (2)

Everyone_Else
Everyone_Else

Reputation: 3264

There's no need to do this all in one line. For example, after the line

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

do something like

if sffem >= 100 then sffem = 100 end
if sffem <= -100 then sffem = -100 end

(Thank you to Henrik Ilgen for syntax help)

Upvotes: 3

JWT
JWT

Reputation: 407

Your second line of code is using a semicolons instead of a commas to separate the arguments to MAX and MIN.

Your code after that change and using math.min and math.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

You might find it useful to make a clamping helper function:

function clamp(value, min, max)
  return math.max(min, math.min(max, value))
end

In which case your code becomes this:

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

Upvotes: 0

Related Questions