The Mugen Fighters Guild

Help => M.U.G.E.N Development Help => Code Library => Topic started by: Bea on November 07, 2003, 12:20:13 pm

Title: Square roots and General Roots in mugen...
Post by: Bea on November 07, 2003, 12:20:13 pm
Well, well, well. I know that many here might not be too found on math, but there is a thing some might find interesting. It was originally posted at MugenDev an year ago.
There are two ways to get a square root of a number in mugen.
The first one is by using the exp and ln functions in this way:
;Square Root
[State WhatEver, I don't Care]
Type = VarSet
trigger1 = Something
fvar(39) = exp(0.5*ln(fvar(38)))

This soluction is nice, cleaner and will give more accurate results than the second method below. Thanks to ANMC for pointing me out that the exp function in mugen ain't broken as some other math functions are and could be used this way.
You can always try to generalize the function so you won't be limited to square roots:
;Root Function
[State WhatEver, Root]
Type = VarSet
trigger1 = Something
fvar(39) = exp((1.0/fvar(37)) * ln(fvar(38)))

Here fvar(37) represents the root base (2, 3, 4 and so on). Keep in mind that square roots and roots functions are very slow.

Also, you can use exp to calculate power in this way: exp(fvar(37)*ln(fvar(38))). This gives a better result than the ** operator in mugen.

Now, on the second method, that I'll be posting just for my own sake, as I spent some good nights awake and lost many precious dating hours trying to figure out the code. It has rounding error problems, is slower than the code above and so on, but it works quite nicelly.
This code is based in taylor series:

;Square Root of a number using Taylor series.
[State 666, SquareRoot1]
Type = VarSet
trigger1 = 1
fv = 0
value = Value ;We'll be storing the value we want to get the sqrt in the parent's fvar(0)

[State 666, SquareRoot10]
Type = VarSet
trigger1 = 1
fv = 1
value = 0.5*ln(fvar(0)) ;This is the main key for the success.

[State 666, SquareRoot11]
Type = VarSet
trigger1 = 1
fv = 2
value = fvar(1)*fvar(1)/2.0 ;Remember guys, the ** operator does not work very well in mugen

[State 666, SquareRoot12]
Type = VarSet
trigger1 = 1
fv = 3
value = fvar(2)*fvar(1)/6.0 ;fvar(1)**3/3!

[State 666, SquareRoot13]
Type = VarSet
trigger1 = 1
fv = 4
value = fvar(3)*fvar(1)/24.0 ;fvar(1)**4/4!

[State 666, SquareRoot14]
Type = VarSet
trigger1 = 1
fv = 5
value = fvar(4)*fvar(1)/120.0 ;fvar(1)**5/5!

[State 666, SquareRoot15]
Type = VarSet
trigger1 = 1
fv = 6
value = fvar(5)*fvar(1)/720.0 ;fvar(1)**6/6!

[State 666, SquareRoot16]
Type = VarSet
trigger1 = 1
fv = 7
value = fvar(6)*fvar(1)/5040.0 ;fvar(1)**7/7!

[State 666, SquareRoot17]
Type = VarSet
trigger1 = 1
fv = 8
value = fvar(7)*fvar(1)/40320.0 ;fvar(1)**8/8!

[State 666, SquareRoot18]
Type = VarSet
trigger1 = 1
fv = 9
value = fvar(8) *fvar(1)/362880.0 ;fvar(1)**9/9!

[State 666, SquareRoot19]
Type = VarSet
trigger1 = 1
fv = 10
value = fvar(9)*fvar(1)/3628800.0 ;fvar(1)**10/10!

[State 666, SquareRoot19]
Type = VarSet
trigger1 = 1
fv = 11
value = fvar(10)*fvar(1)/39916800.0 ;fvar(1)**11/11!

[State 666, SquareRoot20]
Type = VarSet
trigger1 = 1
fv = 12
value = fvar(11)*fvar(1)/479001600.0 ;fvar(1)**12/12!

[state 666, SquareRoot22]
Type = VarSet
trigger1 = 1
fvar(39) = 1+fvar(1)+fvar(2)+fvar(3)+fvar(4)+fvar(5)+fvar(6)+fvar(7)+fvar(8) +fvar(9)+fvar(10)+fvar(11)+fvar(12)
;The result is stored at fvar(39)

If you need to calculate the root of a number with mugen, stick with the first method. It has several advantages over the one I developed using Taylor series.

Have fun. :P
Title: Re:Square roots and General Roots in mugen...
Post by: Winane on November 07, 2003, 12:57:31 pm
Heh, hard to let go of code you've spent a lot of time on, isn't it? :)

ain't broken as some other math functions are

This makes me wonder:  Which other math functions have you found to be broken in Mugen, and in what ways?
Title: Re:Square roots and General Roots in mugen...
Post by: Bea on November 07, 2003, 01:30:03 pm
Winane: The most significant one is the ** operator, which has a bad precision, doesn't like to be in expressions and doesn't accept float numbers as its base. The other thing is expression processing. Mugen hates complex expression with passion and will give you broken results whereas it can...
Also, it's float number precision is really bad...
So, math in mugen is like a nightmare.
Title: Re:Square roots and General Roots in mugen...
Post by: Booba92i on November 07, 2003, 04:43:48 pm
Mmm I don't think many people will use math formulas for their characters (unless they have specific needs) but that's still a good tutorial.  
Title: Re:Square roots and General Roots in mugen...
Post by: Sheng Long on November 07, 2003, 07:37:39 pm
LOL I'm just as confused as ever looking at that code. I'd rather stick with to my common knowledge. Besides, what OrochiKOF97 says, we do not really need that kind of math equation for our characters, unless there is a specific reason why we needed it.
Title: Re:Square roots and General Roots in mugen...
Post by: Winane on November 07, 2003, 11:28:46 pm
Well, the point of it is, if you do need to use it, the tutorial is here for you, and you don't really need to understand it to use it.

Anyway, I just noticed you left out some important information that's almost obvious, but possibly necessary for people who don't know much about math:

In "[State WhatEver, I don't Care]", the VarSet controller assigns the value of the square root of fvar(38) to fvar(39).  In other words, fvar(39) = sqrt(fvar(38)).

In "[State WhatEver, Root]", the VarSet controller assigns the value of the var(37)th root of fvar(38) to fvar(39).  In other words, fvar(39) = fvar(38)^(1/fvar(37)).

In "exp(fvar(37)*ln(fvar(38)))", this expression returns the value of fvar(38) to the fvar(37)th power.  In other words, fvar(38)^fvar(37), or in Mugen terms, fvar(38)**fvar(37).

Just thought I'd add that in case newbies get confused trying to figure out what each variable represents.
Title: Re:Square roots and General Roots in mugen...
Post by: The Necromancer on November 08, 2003, 04:45:50 am
Quote
Mmm I don't think many people will use math formulas for their characters (unless they have specific needs) but that's still a good tutorial.

I've been in need of a square root fuction for a while now, and it was only recent that I asked SMEE on how to do this. It was a coincidence that he had already made such a formula. So now I can create some of these once "impossible to make" moves in my characters, thanks to him. =)

An example for what I'm using this for, includes being able to shoot a projectile directly at the opponent, regardless of where ever he/she may be. (Not homing, just aiming.)
 
Title: Re:Square roots and General Roots in mugen...
Post by: S.D. on November 08, 2003, 08:09:35 am
Mmm I don't think many people will use math formulas for their characters (unless they have specific needs) but that's still a good tutorial.  
Indeed, not MANY characters use, them...but is needed for some
example: the movement of the small cannons in MvC Morrigan's Soul eraser, those need a sin function, that' is much better and faster than handling the movement in air file, or the usual x and y movement in the constants
Title: Re:Square roots and General Roots in mugen...
Post by: Starkid69 on November 08, 2003, 10:46:48 am
i think the point is if you can use it great if not well then great too
Title: Re:Square roots and General Roots in mugen...
Post by: Booba92i on November 08, 2003, 01:12:39 pm
Mmm I don't think many people will use math formulas for their characters (unless they have specific needs) but that's still a good tutorial.  
Indeed, not MANY characters use, them...but is needed for some
example: the movement of the small cannons in MvC Morrigan's Soul eraser, those need a sin function, that' is much better and faster than handling the movement in air file, or the usual x and y movement in the constants

Sure, now to see who needs what, and who codes better a identical move, it's up to the creators.  ;)