Mathematics - Functions

martix

There is no spoon
Reaction score
49
Foreword
This tutorial is about all the mathematic functions and possibilities that exist withing the World Editor. Its aimed at begginers as well as advanced users. And its also an effort to prove that what you learn(ed) in math classes in school can be useful.

Table of Contents:
I. Introduction
II. Basic Definitions and General Stuff
III. Algebraic Funtions
IV. Geometric/Trigonometric Functions




I. Introduction
This is a comprehensive tutorial that (if you are willing to read it through) will teach you about all the possibilities for working with numbers when using the World Editor. I guess the algebra(on the level WE offers it) will seem easy enough, but trigonometry may be a diffrent thing(I wasn't good at it either). One thing I'm saying from the beggining - I'm not going to (re)teach all the maths - thats something you learn in school, not in a WE tutorial! With that said lets start with the basics you need to know.

II. Basic Definitions and General Stuff

1. Math functions are used in triggers to calculate diffrent numbers and if used to the full potential can be a priceless asset in creating a great trigger enchanced spell or some other original and/or fancy feature in your map, that would make it outstanding. I guess anyone who has brains enough to understand this tutorial would also have enough sense of logic to be able to understand triggers quite well.

2. Variables - I'm gonna make a quick explanation, there are 2 types of numeric variables in the World Editor:

a) An Integer variable - this variable can take ONLY whole(positive or negative) numbers(like counting units and other things that can never have a decimal). Thats why there are less math functions with it and also no trigonometric functions at all. An integer can assume any value between -2147483648 to 2147483647.(Thanks wonderpriest).

b) A Real variable - this variable can take any real value, be it positive, negative, whole or decimal. The accuracy is up to the second digit(one hundredth). But since this is math and everything is equal to something else, in some cases there is a simple way to achieve more accuracy(if needed), I'll talk about that later. Its also a reqirement for anything involving geometry in the World Editor.
Note: Here's something odd I am not sure of - in the editor, the accuracy goes to the 2nd digit after the decimal point, in the game it goes 3rd, and I'm not sure if this 3rd digit can actually be used.

3. Some Specifics and notes:
a) Watch the titles - Each window that reqires some sort of number - pay heed to the title of that window saying what type of number it requires (integer or real), so you don't have to wonder why you can't find the function you need. There are a lot of common functions, but each type has functions that are specific to it. Example - any type of counting function is restricted to integers, and most of the unit properties(life, movement speed, etc.) are Real-only accesible. I will be noting that troughout this tutorial, functions that are not marked as "Real-only" are accesible to every type.

b) Conversions - But there is a way around that - with the conversions, they can convert one number type to another. A word of warning though - not all conversions are reasonable, despite still being possible. One particular combination you should ALWAYS avoid is Integers+Trigonometry!
Note:When converting reals to integers the real is not rounded. Like 9.8 ≈ 10 - how normal rounding would work. Instead the decimal part is quite simply cut off! In this case 9.8 becomes 9. Remember this! (Credit to Luth for this specification)

c) Percents - a percent is also a predefined number. 100%=1 - you gotta remember that simple fact. You can get the percent of a number simply by multiplying it with some other number. For example - 69% of 246= 0.69*246. Its as simple as that.

d) When you encounter some difficulty of mathematical nature I strongly suggest you leave the computer be, take a sheet of paper and a pencil approach the problem not as a part of your trigger(or your map), but as a maths task you were given to solve. Believe me it helps. It is when you solve it that you can start thinking of a way to implement it in your map.
Ok, lets start with each specific function.

III. Algebraic Functions


1. Arithmetic
This is the first and most simple math operation in the editor. You got 2 number, no matter how you got them(by direct input or as a result of another function) and one of 4 operations you all know - addition, sustraction, multiplication and division. Nothing more to say about that really.
Example - 2+2=4 (or is it :) ) Well, as simple as that...

2. Math - Random Number
This fuction returns a random number in the numerical range you specify of the type that is required. A random real number will almost always return a number with a decimal part(there is only one hundredth - 1% chance it returs a non-decimal) :) It is the only function that returns a random number in a user-defined range.

3. Math - Random Angle *(Real-ONLY)
This one also returns a number, but in a predefined range - 0 to 360, the value of a full circle. And btw, in this case - 0=360

4. Math - Random Percentage *(Real-ONLY)
Returns a REAL value between 0 and 100 to be used as a percentage(nothing more to say I guess).

Note: I have one very imporant thing to add about randomness though - I've seen a lot of threads and questions asked about why I got the same hero N times in a row and why did that guy score 5 consecutive criticals(and probably killed you :) )
There are ways to generate absolutely random numbers, but computer algorithms cannot be used for that, since, by definition, no deterministic mathematical algorithm can generate a random result.
Do remember this whenever a question of the above type comes to your mind!

5. Math - Min
This is used to compare 2 numbers and give the lesser one. The position doesn't matter. It always involves at least variable(or you banged your head and don't know if 1 is bigger than 2) :)
Example: Min(5,2) would return 2 as it is lesser than 5.

6. Math - Max
The opposite of the above one, only returns the greater value of the values you input. Again, it doesn't matter if its value 1 or 2.
Example: Max(6,12) would return 12 as it is bigger than 6.
These two for example can be used ot avoid certain conditionals and save you a bit of time.

7. Math - Abs
It returns the absolute value of the given number.
3=3, 4.25=4.25, -7=7, -43.5=43.5
To illustrate imagine an axis with all numbers placed on it - this returns the distance to that number from the 0, no matter in which direction you go(positive or negative). (Wonder - did you imagine anything, and if you did, was it remotely similar to mine?) :D

8. Math - Sign
You can think of this as the opposite of the above function.
It gives you(returns) the sign of the number you input. But since it has to spit out a number, it gives you 1 or -1.
12=1 or -32=-1

9. Math - Modulo
Calculates the remainder of a devision.(That would be enough for me, but who knows what people are out there).
It gives you what is left behind when the division reaches the decimal point. Divisions that return a whole number do not have a remainder.
This is a very useful, yet underused function. It can be used for ex. when going from one numeric system to another(decimal->hex->octal).

10. Math - Power *(Real-ONLY)
You give a number and a power and it gives you back that number raised to that power. Any number and power can be used, the power value is also real, which means it can have a decimal part.

11. Math - Square Root *(Real-ONLY)
It calculates the square root of a number and only the square one(maybe blizzard doesn't like the other shapes :) )
sqrt(X^2)=X
As suggested you can use this to calculate whichever root you want. This can be achieved very simply by making use you certain exponential identities. Just look at the link(s) posted at the end.

These are pretty much all the algebraic functions, now comes Trigonometry(the advanced stuff).
Some notes - using the arithmetic function you can actually get past the 2 digit decimal limitation when using predefined numbers(ones you typed yourself, not changeable). For example - 943.89/100=9.4389 - thats how you can simulate a more accurate number. :)

IV. Trigonometry/Geometry
*(Real-ONLY)

Every function here is a definate REAL-only. Most of the trigonometric functions always return a decimal value. And for example sine and cosine ones are always between 1 and -1. So DO NOT EVER convert these to integers! You'll end up with weird results AT BEST.

1. A random definition from the web:
Trigonometry - Study of triangles, the measurements of their parts and of angle functions and relations.

2. Presets: You can set a real variable any value you want in the variables window. There are also presets, 2 of which are worth explaining - pi and e
a) pi≈3.1415926536 (Believe me or not, but I didn't look it up anywhere, I just remember it to the 10th digit :) The most accurate(if I remember correctly) is 22/7
b) e ≈ 2.71828 18284 59045 23536
This is also one of the most important mathematical constants, for more info visit wikipedia at http://en.wikipedia.org/wiki/E_(mathematical_constant)
There you will find a simple enough explanation(for me at least).

3. Conversions - Convert degrees to radians and vise versa. To understand these, you gotta understand both definitions.
Degrees - Any positive real number between 0 and 360
Radian - One radian is the angle of an arc created by wrapping the radius of a circle around its circumference. The radius 'r' fits around the circumference of a circle exactly 2p times. That is why the circumference of a circle is given by circumference = 2pi*r. So there are 2pi radians in a complete circle, and pi radians in a half circle. Degrees=Radians*180/pi


4. Math - Random Angle
Same as above, wanted to put all random things in one place.

5. Math - Distance between points
This function gives you the distance between 2 points in pixels, warcraft's default distance measurement unit(move speed is also measured in pixels per second a unit can travel).

6. Math - Angle between points
This may seem a little odd the first time(maybe impossible too :) ) But say you got point A and B. If point A is the center of a circle, the distance between the two points is the radius of that circle and point B is on the circumference of the circle, then the angle is equal to the position of B on that circle in reference to WC's coordinate system where 0 degrees is east, 90 is north, 180 is west.
So if point B is directly vertically positioned above A, then this function would return 90 degrees.

7. Math - Sine
This calculates the sine of a given angle. The sine of an angle in a right triangle equals the opposite side divided by the hypotenuse. See the attached picture.

8. Math - Cosine
This is the opposite of the above function, calculates the cosine of a given angle. In a right triangle the cosine of an angle equals the adjacent side divided by the hypotenuse. Again see the picture. These two functions are always in the range of -1 to 1, ALWAYS!
Oh and if you must know - sine^2+cos^2=1 always - this is the most basic trigonometric equation.

9. Math - Tangent
Calculates the tangent of a given angle. The tangent is the link between the above 2 functions and is equal to (sine of that angle)/(cosine of that angle). In a right triangle the tangent is the opposite side divided by the adjacent side.
Note: there is another function - the cotangent, which is the opposite(cosine/sine), but its not included since they are muttually interchangeable(1/tangent=cotangent).

These were the "straight" functions, now come their inverse counterparts, arc functions. They are usually denoted by a -1 superscript(like sin raised to power -1).

10. Math - Arcsine (or inverse sine)
This takes a sine value and converts it to its corresponding value in degrees. See Math - Sine for explanation of the function. Input range: -1 to 1, output range is -90 to 90 degrees,

11. Math - Arccosine (or inverse cosine)
Takes a cosine value and returns its value in degrees. Input range: -1 to 1, Output range - 0 to 180 degrees.

12. Math - Arctangent from angle (or inverse tangent)
Takes a tangent value and returns its value in degrees. Input range: -inf to inf, output range is -90 to 90 degrees.

13. Math - Arctangent from deltas (or inverse tangent)
This requires a diffrent input. It needs 2 deltas as input. A delta is essentialy one of the components(X or Y) of a vector. Input range: -inf to inf, output range is -180 to 180 degrees.
Er, how exacly to explain this simply...

Say you got one unit thats standing still and another unit thats running around it. The first unit is the beggining of the vector, the second is the end. If you put this function in the 1st units facing and supply the X and Y component of the resulting vector, this unit will always be facing the second one. Fair enough? (I'm a little rusty, so don't be mad at me).


Curious facts: I made the effort of searching for applications of these functions in Blizzards maps(campaign ones). I discovered that from the trigonometric ones only sine and cosine are used in the same trigger in several maps that involved caravans. It was just copied over and over for each map(even the name stayed the same). Actually I discovered that quite a few campaing triggers had their copies in a number of maps. :)

So, that would be all the mathematic functions in the editor and thus the end of my first tutorial. How you use all this is up to your imagination and knowledge. Post any comments and suggestions, there is always room for improvement.

As I said in the beggining, I wasn't planning on repeating your school curriculum(skipping that is/has been up to you alone), but it has been suggested that I include some formulas and stuff, so I'll just add a few links:
http://math2.org/ - Your general math tables. Here you can find everything from simple Polynomial Identities to Calculus.
http://www.lippsphysics.com/physics/formulas.html - Some physics and a funny comic strip.
http://www.lippsphysics.com/classes/syllabusdocs/equations.pdf - a PDF document containing anything math in nature you heart desires - syntesizing everything from nuclear physics and optics to trigonometry and calculus in one handy format.

Hope this has helped you :)
 

Attachments

  • SineCosineTangent.PNG
    SineCosineTangent.PNG
    15.8 KB · Views: 791

N2o)

Retired.
Reaction score
51
7. Math - Sine
This calculates the sine of a given angle angle.
I won't give you +rep because you made this typo and because you asked for it.
Well i will because it is a nice tut but i don't like people who ask for +rep it makes them unworthy.
 

uberfoop

~=Admiral Stukov=~
Reaction score
177
The (arc)sine/cosine/tangent stuff probably wont be all that useful to people that havent studied that yet (I never realised how manipulatable wavy lines were until I learned all about that stuff ^_^) but the rest of the stuff is pretty good.
I likes it.

I vote it goes in repository.
 

emjlr3

Change can be a good thing
Reaction score
395
second digit(one hundredth)

actually its the 1/1000, GUI has its limitations

but in a predefined range - 0 to 360

as you said, they are equal, so use .001-360. , hehe

Note: I have one very imporant thing to add about randomness though - I've seen a lot of threads and questions asked about why I got the same hero N times in a row and why did that guy score 5 consecutive criticals(and probably killed you )
Quote:
There are ways to generate absolutely random numbers, but computer algorithms cannot be used for that, since, by definition, no deterministic mathematical algorithm can generate a random result.
Do remember this whenever a question of the above type comes to your mind!

actually, I think that when using the test map feature in the WE, it may do the same random thing each time you test, say you want to start something at between 30-60 seconds, it may start it at 45 seconds each time, this may not be true though, but I find that it may be,a tleast for things like that, however something like GroupPickRandomUnit() is random each time

Math - Sine

as said before

For example - 943.89/100=9.4389 - thats how you can simulate a more accurate number.

why would you need to be hat accurate? btw I dont think this is true, look at your sig. digs. , still should only go to two decimal places I think, 100(whic is your limiter) has 3 sig digs, so it should be 9.43

although my memory on the subject may be hazy

5. Math - Distance between points
This function gives you the distance between 2 points in pixels, warcraft's default distance measurement unit(move speed is also measured in pixels per second a unit can travel).

may want to explain how this is done

also may want to give some examples of how to use each of the functions, may be helpful

+rep
 

Effane

Save/Load Code Tutorial
Reaction score
51
Well random number generators are actually random 0.0000-1.0000(Out many, many places) generators. They always produce a result between that range from either a "seed" or a previously generated result. Then what the result is used for is to multiply against your range.

So if you have random 10-40 (Integer), and the random result is 0.8653, then what happens in the function subtracts your min from your max. Multiplies the result into that (Shaves decimals (Real) or converts to integer), and then readds the min.

( ( Max - Min ) * RandomResult ) + Min = Random number.

( ( 40 - 10 ) * 0.8653 ) + 10 = 35 (Notice the result from the multiplication was 25.959, but it becomes 25, thats because integer conversion doesnt round up, it just shaves off the decimals and discards them. Very important when you are concerned about accurcacy, and also explains something else I will talk about.)


Accuracy of random numbers. As I mentioned above, there isnt any rounding with integers. This causes a weird side effect. Its nearly impossible to get 100% on something, the odds for numbers from Min to (Max - 1), are much higher than the odds for Max. Here is why.

Lets say you are doing 0-100 integer. now since you are multiplying by the result of random, then shaving the decimal, you will notice something. for results that will cause 0-99, you just have to have 0.XX###, XX being the number, IE, 5 would need 0.05###, and ### just being more decimals. But to get 100 you would need a perfect result of 1.00000, being a 1 in 100000 chance considering this decimal system I am using alone.

(most random number generators are actually much larger in results, like 14 places of decimals. They make them so big, because you need a decimal place for every possible integer or real place. So if integers are 14 places, you need that many for the decimals, not including placeholder 0. at the beginning)

This may or may not affect Reals, depending on how WC3s functions convert the decimals. If it rounds up from 0.5000+ then your fine. If it doesnt then the same issues apply there.
 

Luth

Lex Luthor!
Reaction score
41
It might behoove you to mention how Reals are converted to Ints and such. Real-to-Int of 2.9 is, as expected, 2. "Oh, so round down", one might say. Not quite. :) Real-to-Int of -2.9 is -2. Simply drop the decimal point. (An aside: some values are unattainable in Real format. For example, "2" in real may actually be something akin to 1.9999...99374, which is as close to 2 as the computer can get using floating point math, and will even be displayed as "2.00". This has cropped up in one WE bug where the value as a real was being displayed properly, but when converted to an Integer, was actually 1 less than expected.)

Also, a simple trick to do your own "rounding" of reals, if needed:
Real R = 2.7
Integer I = R2I(R) // wrong, this gives you I == 2
Integer I = R2I(R + 0.5) // right, I == 3, this will "round" to the nearest whole integer

And lots of people dont know modulo, yet it is extremely useful. :) An example would probably help the definition a tad. ie: 32 modulo 10 = 2 (as there is 2 left over after diviing 32 by 10)

I see this as helping many a beginner. :) Thanks for the work.
 

wonderpriest

New Member
Reaction score
18
Nice, but tedious to read since there are no breakups, it just goes on and on. Try bolding a few words here and there, adding a line break, you know...

Also, I didn't look at the whole thing, but I didn't see a section on how to avoid using slow math functions in triggers too much, or how to optimize your math so you can avoid them entirely or avoid repeating math that could be stored in a variable. Some people take the computer for granted and they don't realize alot of math can bog a computer down alot.

You may want to add also the limits of integers and reals (if you haven't).

-2147483648 to 2147483648 for integers and
3.4e +/- 38 for reals (7 digits) (I believe that wc3 just uses normal floats..)


ADD: You posted this tutorial today and have more activity than my Trackables tutorial. I would think they are alot more fun than math :(
 
W

Wazat

Guest
wonderpriest: Math may not interest you quite as much, but it sure confuses the hell out of a lot of people. This will probably be a really popular tutorial simply because it explains those crucial bits to people who would otherwise be lost. So that's probably why.
Btw, link to your trackables tutorial (or maybe pm me if that's more appropriate)? I don't check the forum often enough to catch a lot of stuff.

martix: Cheers, dude! This will be a handy tutorial to point people to.
 

zylack

New Member
Reaction score
15
yes i agree, i'm good at math and i've never had problems with it myself, but its a very well written tutorial that im sure could help the "math challenged."
I read through it and learned a few things i forgot in math class :D +rep
 

wonderpriest

New Member
Reaction score
18
Wazat, I love math, it can get you alot of places, you really can't write a script without it. I'm just saying I didn't expect for people to be more enthusiastic about math than some neat mouse detection :D
And no I don't think I will link here, that's kind of rude, but if you want the link I can send it to you, I'd appreciate it if you looked at it, it's probably on the fifth page by now =P
 
W

Wazat

Guest
Heh, yea stuff gets buried fast here. I'd love to take a look at that! Please pm it to me.
 

Andrewgosu

The Silent Pandaren Helper
Reaction score
716
wonderpriest said:
it's probably on the fifth page by now =P

Bump:)

This is one if the best tutorials around here. I love it, you are as good as Daelin, when it comes to writing tutorials.:)
 

martix

There is no spoon
Reaction score
49
Wow, I didn't anticipate so much positive feedback. And yeah, I know the tut is not finished, thats why I'm so surprised. I know it needs more formatting as wonderpriest said. I'll also add the variable ranges(thanks). There was no maths tutorial around here, so I though that would be a great subject to start with.
Thanks for all the criticism too :) I'll try to improve it as best as I can.

@N2o)
Well sorry for the typo, will be fixed and sorry if I look unworthy to you. I actually don't care that much for rep. Actually I did some research on it just a day before starting this tutorial, until then I didn't know what this is all about.
@emjlr3
> actually its the 1/1000, GUI has its limitations
Note: Here's something odd I am not sure of - in the editor, the accuracy goes to the 2nd digit after the decimal point, in the game it goes 3rd, and I'm not sure if this 3rd digit can actually be used.
(I wrote this in the 2nd section at the end)
So thanks for clarifying this :) So it can be used, and any number returned is 1/1000th accurate, and the limitation is just within GUI not the game. But this tutorial is not about the form of the functions(GUI/JASS) nor the speed. I know that x*x is faster than x^2, but I don't think this is needed in the tutorial, its about explaining what everything does, not how it does it.

The accuracy - I at least needed it a couple of times. But it depends on how the computer works in this case. Because it may be rounding each number to WC3's limitation, or WC3 may be giving the inctructions, the computer calculating the number very accurately and then convert it to WC3's limitations. I don't know whether this is true, or whether I made any sense at all. :) I admit I posted this based on assumptions, but I will try to test it sometime.
About the distance - what do you mean exactly?

And examples - as I said in the end of the tut, I'm on vacation right now. And I will be giving plenty of examples once I return. I will make a map demostrating each and every function described here.

@Effane
Read the quotes I included about computer randomness. They say it as clear as it can be said. And they are not my words(thats why its in quotes). And I believe this guy. I just can't imagine someting based on logic producing randomness. These 2 words just don't fit together. Also, I've seen some comments about how WC3's RNG is actually pretty crappy, but I cannot confirm this.

@Luth
Thanks, thats worth mentioning :) I'll add it :)

Again thanks for the feedback.

Edit:
Um, wonderpriest, what is the reals range exactly?
And also - any in negative to positive range the positive number is 1 less than the negative(its the 0's fault).
 

emjlr3

Change can be a good thing
Reaction score
395
not everyone will find it very useful, and among those not many ofthem will even post about it, the initial craze is over

if ppl have further comments or questions, they will post them, trust me
 

Sim

Forum Administrator
Staff member
Reaction score
534
I'm a bit late but insist on telling you this is a nice tutorial.

I wish I had read a tutorial like this before...

But... I have some things to point out, as I let my brother read your tutorial, who is not too good in maths, and there are a couple of things he didn't understand because of the lack of examples.

- The min function. You don't really explain it very well, and without any examples, if you ever happen to lose somebody while explaining, you won't get him back.

- Same for the max function.

- The Modulo. I suggest giving an example.

And my other comment: You should add an example of, not only the function itself in maths but how every function can be used in the World editor. Example: Power, for exponential Damage values. (Triggered, that is)

10
100
1000
10000
100000

instead of say 50*level

50
100
150
200
250

Even Modulo can have a use in the WE :rolleyes:

Do whatever you wish though, as this tutorial is indeed very nice. I recommend it to anybody :)

+rep
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top