ZDaemon Forum Index ZDaemon
Client/Server DOOM
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Some math improvements for ACS

 
Post new topic   Reply to topic    ZDaemon Forum Index -> Wad Editing
View previous topic :: View next topic  

Do you think this information can be useful?
Yes
66%
 66%  [ 4 ]
No
33%
 33%  [ 2 ]
Total Votes : 6

Author Message
adeon
has entered the game!


Joined: 22 Feb 2009

PostPosted: Sat Mar 21, 2009 8:35 pm    Post subject: Some math improvements for ACS Reply with quote

Hello there, how are you doing?

This may sound useless for many, but if you are smart and like to invent creative stuff with ACS, it may be very useful IMO.
As ACS doesn't like math very much by default, I am developing my own formulaes. I'll be posting 'em here even for organization reasons (I am afraid of loosing/corrupting them somehow)
Thanks to bagheadspidey for helping me optimizing some codes.


    Exponential:
    Code:
    function int FExp(int base, int exp)
    {   
      int result = 1.0;
      int i;
      int abs_exp = exp;
      if (exp < 0) abs_exp *= -1;

      for(i=0; i<abs_exp; i++) result=FixedMul(base,result);

      if(exp<0) return FixedDiv(1.0,result);
      return result;
    }

    Usage: FExp(Your floating value,Your INTEGER exponent) (e.g.: FExp(3.0,5))
    Works with negative exponents.


    Square Root:
    Code:
    function int FixedSqrt(int number)
    {
      int samples=15; //Samples for accuracy

      if (number==1.0) return 1.0;
      if (number<=0) return 0;

      int val=FixedMul(samples<<16,10.0);

      for (int i=0; i<samples; i++)
        val=FixedDiv(val+FixedDiv(number,val),2.0);

      return val;
    }

    Usage: use it as a function writing FixedSqrt(your floating number).
    Negative values results 0.

    Keep in mind that it's not a perfect solution. The smaller the number is, the higher the percentage of error is.
    Feel free to change the number of samples by editing the samples string. 15 samples are great in most cases.


    Sine, cosine and tangent:
    ACS has a built-in sine and cosine system. But tangent is missing, and the values that you put on them must be in a different scale than radian values. 1.0 value means a full circle instead of 2*pi. If you want to make interactive math equations based on sine, cosine or tangent values in radian - which is vastly used - try this conversion:
    Code:
    int pi=3.14159;
    function int FCos(int fcosnum)
    {
    int fcosresult=cos(FixedMul(fcosnum,FixedDiv(1.0,FixedMul(2.0,pi))));
    return fcosresult;
    }

    Usage: FCos(your floating number in radians).

    Now, it's Sine turn:
    Code:
    int pi=3.14159;
    function int FSin(int fsinnum)
    {
    int fsinresult=sin(FixedMul(fsinnum,FixedDiv(1.0,FixedMul(2.0,pi))));
    return fsinresult;
    }

    Usage: FSin(your floating number in radians).

    I recommend not to change the pi value.

    What about tangent?
    That's quite simple. tangent(x) = sin(x)/cos(x). Then:
    Code:
    int pi=3.14159;
    function int FTan(int ftannum)
    {
    int ftanresult=FixedDiv(sin(FixedMul(ftannum,FixedDiv(1.0,FixedMul(2.0,pi)))),
                      cos(FixedMul(ftannum,FixedDiv(1.0,FixedMul(2.0,pi)))));
    return ftanresult;
    }

    Usage: FTan(your floating number in radians)


    Arc sine, arc cosine and arc tangent (NOT DONE YET):
    This one has been a pain in my butt since nobody teached inverse trigonometry to me yet at school. But I think I got some average results.

    Here's the arc sine formulae:
    Code:
    function int FASin(int number)
    {
    int result=0;
    int samples=5; //Samples for accuracy (over 5 samples usually results wrongly)
       if(number!=0 && number<1.0 && number>-1.0 && samples>0)
       {
       int div_on=1.0;
       int div_un=1.0;
       int div_val=1.0;
       int loopresult;
          for(int i=0; i<samples; i++)
          {
             div_on=FixedMul(div_on,div_val);
             div_un=FixedMul(div_un,div_val+1.0);
             div_val=div_val+2.0;
             
             loopresult=loopresult+FixedMul(FixedDiv(div_on,div_un),
             FixedDiv(FExp(number,div_val>>16),div_val));
          }
       result=number+loopresult;
       }
       else if(number==1.0||number==-1.0) result=FixedMul(1.5708,number);
    return result;
    }

    Usage: FASin(your floating number)
    Although there are 5 samples in this algorithm, the margin of error is considerably big if the input value is close to 1.0.
    This problem would be solved if the samples were increased, but unfortunely the values would become completly wrong when processed in ACS because of the depth of the numbers.

    Now the acos. As arc cosine of x represents pi/2-asin(x) then:
    Code:
    int pi=3.14159;
    int THEVAL=-1.0;

    int YOURINVERSECOSVALUE;
       if(THEVAL<=1.0 && THEVAL>=-1.0)
          {
          YOURINVERSECOSVALUE=FixedDiv(pi,2.0)-(THEVAL+FixedDiv(FixedDiv(FixedMul(THEVAL,
          FixedMul(THEVAL,THEVAL)),3.0),2.0)+
          FixedDiv(FixedMul(FixedDiv(FixedMul(THEVAL,FixedMul(THEVAL,
          FixedMul(THEVAL,FixedMul(THEVAL,THEVAL)))),5.0),3.0),8.0)+
          FixedDiv(FixedMul(FixedDiv(FixedMul(THEVAL,FixedMul(THEVAL,
          FixedMul(THEVAL,FixedMul(THEVAL,FixedMul(THEVAL,
          FixedMul(THEVAL,THEVAL)))))),7.0),15.0),48.0)+
          FixedDiv(FixedMul(FixedDiv(FixedMul(THEVAL,FixedMul(THEVAL,
          FixedMul(THEVAL,FixedMul(THEVAL,FixedMul(THEVAL,
          FixedMul(THEVAL,FixedMul(THEVAL,FixedMul(THEVAL,
          FixedMul(THEVAL,THEVAL))))))))),9.0),105.0),384.0));
          }
       else
          {
          YOURINVERSECOSVALUE=0;
          }

    Its graph is something like the red line:



Testing:
(sin(1.6)+cos(2.5))*sqrt(22)/tan(0.5):
    In doom results 1.70784
    In a calculator results 1.70366
    (0.24% of error)


I got to go for now. Later I may post the rest of the solutions.
See you all.


Last edited by adeon on Tue Mar 24, 2009 5:31 am; edited 12 times in total
Back to top
View user's profile Send private message
Ronald
Rontard


Joined: 17 Aug 2005
Location: Netherlands Clan: [QnB]

PostPosted: Sun Mar 22, 2009 9:07 am    Post subject: Reply with quote

Oh my God, you remind me of my math teacher! Looks like you'll be given an A at your next test.

About the usefulness of this, I'm not really convinced. What kind of creative stuff do you think you can invent with these formulaes?
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
BestOfTheWorst
Unstoppable!


Joined: 02 Jun 2006

PostPosted: Sun Mar 22, 2009 9:51 am    Post subject: Reply with quote

you should look at custom functions, to have these in a more easily useable format Arrow http://zdoom.org/wiki/Functions

also looking at the 'other useful functions' -page on zdoom wiki, I can find a lot better examples of square root: http://zdoom.org/wiki/Sqrt <- there's one that uses just 3 lines, compared to your long formula Razz

also I'm not conviced of the use of various constant values in your formulas, that can only result in hacky and inaccurate results Confused To be honest, now looking at some of the formulas..
Code:

YOURINVERSECOSVALUE=FixedDiv(pi,2.0)-(THEVAL+FixedDiv(FixedDiv(FixedMul(...

how the hell did you come up with this? Laughing


Last edited by BestOfTheWorst on Sun Mar 22, 2009 6:43 pm; edited 1 time in total
Back to top
View user's profile Send private message
adeon
has entered the game!


Joined: 22 Feb 2009

PostPosted: Sun Mar 22, 2009 6:16 pm    Post subject: Reply with quote

BestOfTheWorst wrote:
you should look at custom functions, to have these in a more easily useable format Arrow http://zdoom.org/wiki/Functions

also looking at the 'other useful functions' -page on zdoom wiki, I can find a lot better examples of square root: http://zdoom.org/wiki/Sqrt <- there's one that uses just 3 lines, compared to your long formula Razz

also I'm not conviced of the use of various constant values in your formulas, that can only result in hacky and inaccurate results Confused To be honest, now looking at some of the formulas..
Code:

YOURINVERSECOSVALUE

how the hell did you come up with this? Laughing

I took reference from wikipedia mainly. The big deal is that the number can be only 8 chars long in ACS (if I'm not mistaking), so the results can't be more accurate Sad

Anyway, thanks for the links from zdoom wiki. I'll take a look.


Ronald wrote:
Oh my God, you remind me of my math teacher! Looks like you'll be given an A at your next test.

About the usefulness of this, I'm not really convinced. What kind of creative stuff do you think you can invent with these formulaes?

I don't know really. All I can say is that using some math improvements makes the doom gameplay closer to the latest games.
Let me take an example: GTA-VC. It's pretty old, but it has daylight system, some physics and so on. I'm not sure how it works, but I guess they didn't use only additions and subtractions for this.
It depends on creactivity either.
Back to top
View user's profile Send private message
adeon
has entered the game!


Joined: 22 Feb 2009

PostPosted: Sun Mar 22, 2009 8:27 pm    Post subject: Reply with quote

@BestOfTheWorst
Thank you pal! I didn't know about these custom functions. Now it's way more simple to use these formulas.
Fixed the square root written in the first post. I'll be updating the rest later.

BTW, I couldn't get what's wrong with this square root solution at zdoom wiki page. When I tried the sqrt of 8, it returned to me the 2 value. Looks like it doesn't work with floating numbers.

See you!
Back to top
View user's profile Send private message
BestOfTheWorst
Unstoppable!


Joined: 02 Jun 2006

PostPosted: Tue Mar 24, 2009 9:37 am    Post subject: Reply with quote

Good job, looks much better now Smile

adeon wrote:
BTW, I couldn't get what's wrong with this square root solution at zdoom wiki page. When I tried the sqrt of 8, it returned to me the 2 value. Looks like it doesn't work with floating numbers.
Confused I see.. ..maybe it would be better off for you to post yours in the zdoom wiki instead then Razz
Back to top
View user's profile Send private message
Cybershark
Spamming!


Joined: 05 Jan 2005
Location: off the grid, but still fighting for the users!

PostPosted: Tue Mar 24, 2009 11:52 am    Post subject: Reply with quote



EYE CONTACT!
Back to top
View user's profile Send private message Visit poster's website
Geit-vd-plas
God like!


Joined: 27 Mar 2006
Location: Shoe salesman!

PostPosted: Tue Mar 24, 2009 1:14 pm    Post subject: Reply with quote

hahahahahahahaha!
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    ZDaemon Forum Index -> Wad Editing All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group