User Tag List

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13

Thread: advmath.zh - atan, acos, asin

  1. #1
    Lynel
    ZC Developer
    pkmnfrk's Avatar
    Join Date
    Jan 2004
    Location
    Toronto
    Age
    37
    Posts
    1,248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    3,142
    Level
    18
    vBActivity - Bars
    Lv. Percent
    10.49%

    advmath.zh - atan, acos, asin

    Put this in a new file called "advmath.zh" (so that if anyone else uses it in their scripts, it will have the same name)

    Code:
    //Code adapted from the snippet located here:
    //http://discussion.forum.nokia.com/forum/showthread.php?t=72840
    //The original code was placed in the public domain
    //The translated code below is also public domain
    
    //I had to truncate these constants, since ZScript uses fixed place floats.
    const float sq2p1 = 2.4142;    // .414213562373095048802;
    const float sq2m1 = 0.4142;    // .414213562373095048802;
    const float p4    = 16.1536;   // .1536412982230228262;
    const float p3    = 268.4255;  // .42548195503973794141;
    const float p2    = 1153.0294; // .0293515404850115428136;
    const float p1    = 1780.4063; // .40631643319697105464587;
    const float p0    = 896.7860;  // .78597403663861959987488;
    const float q4    = 58.9570;   // .95697050844462222791;
    const float q3    = 536.2654;  // .265374031215315104235;
    const float q2    = 1666.7838; // .7838148816337184521798;
    const float q1    = 2079.3350; // .33497444540981287275926;
    const float q0    = 896.7860;  // .78597403663861962481162;
    const float PIO2  = 1.5708;    // .5707963267948966135;
    const float PIO1  = 3.1416;
    const float iRt2  = 0.7071;
    const float PIO3  = 57.2958;
    const float PIO4  = 0.0175;
    
    // reduce
    float mxatan(float arg) {
    	float argsq = arg*arg;
    	float value = (((p4*argsq + p3)*argsq + p2)*argsq + p1)*argsq + p0;
    	value /= ((((argsq + q4)*argsq + q3)*argsq + q2)*argsq + q1)*argsq + q0;
    	return value*arg;
    }
    
    
    // reduce
    float msatan(float arg) {
    	if(arg < sq2m1)
    		return mxatan(arg);
    	if(arg > sq2p1)
    		return PIO2 - mxatan(1/arg);
    		return PIO2/2 + mxatan((arg-1)/(arg+1));
    }
    
    // implementation of atan
    float atan(float arg) {
    	if(arg > 0)
    		return msatan(arg);
    	return -msatan(-arg);
    }
    
    // implementation of asin
    float asin(float arg) {
    	float temp;
    	int sign;
    
    	sign = 0;
    	if(arg < 0) {
    		arg = -arg;
    		sign++;
    	}
    	if(arg > 1)
    		return 0.0; //should be NaN
    
    	temp = Sqrt(1 - arg*arg);
    	if(arg > iRt2)
    			temp = PIO2 - atan(temp/arg);
    	else
    			temp = atan(arg/temp);
    	if(sign > 0)
    			temp = -temp;
    	return temp;
    }
    
    // implementation of acos
    float acos(float arg) {
    	if(arg > 1 || arg < -1)
    		return 0.0; //should be NaN
    	return PIO2 - asin(arg);
    }
    
    // convert radian angle to degrees
    float rad2deg(float arg) {
    	return arg * PIO3;
    }
    
    // convert degree angle to radians
    float deg2rad(float arg) {
    	return arg * PIO4;
    }
    
    float findAngle(float x1, float y1, float x2, float y2) {
    	
    	float ret;
    	
    	float dx = x2 - x1;
    	float dy = y2 - y1;
    	
    	ret = ArcTan(dy, dx);
    
    	return PI / 2 - ret;
    }
    * Implements the ATan, ACos and ASin functions (until we get built in versions of the same)
    * Does not rely on std.zh
    * Easily importable into any script
    * As a bonus, includes two functions to convert between degrees and radians!
    * As a super special bonus, includes a function to find the angle from one point to another!

    Function prototypes:

    See this page for what they do: http://www.xgc.com/manuals/xgclib/x940.html

    Code:
    float asin(float x);
    float acos(float x);
    float atan(float x);
    //atan2 is provided by the as yet undocumented function "ArcTan(y,x)"
    float deg2rad(float deg);
    float rad2deg(float rad);
    float findAngle(float x1, float y1, float x2, float y2);
    EDIT: Hurrr. I meant to post this in Script Showcase...
    Tale of the Cave - a web comic - Updates Monday, Wednesday, Friday
    ZC Tutorials - Tutorials and Script Library - Updated July 30, 2008
    ZeldaGuard - Corruption in my save files? It's more likely than you think!
    I do script requests!

  2. #2
    Octorok
    Join Date
    Jan 2008
    Age
    32
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    939
    Level
    10
    vBActivity - Bars
    Lv. Percent
    63.57%

    Re: advmath.zh - atan, acos, asin

    Holy... That's awesome! Don't you know that you are a scripting god? :p

    Great. :)

  3. #3
    Gibdo beefster09's Avatar
    Join Date
    Mar 2006
    Age
    31
    Posts
    699
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    2,710
    Level
    16
    vBActivity - Bars
    Lv. Percent
    97.3%

    Re: advmath.zh - atan, acos, asin

    We already have atan2, or findangle- it's ArcTan(x,y) _L_ just added documentation for it.

    The other functions are helpful, I suppose. I don't really see myself using them much, though.
    Avatar: Just who the SPAAAACE do you think I am?

  4. #4
    Lynel
    ZC Developer
    pkmnfrk's Avatar
    Join Date
    Jan 2004
    Location
    Toronto
    Age
    37
    Posts
    1,248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    3,142
    Level
    18
    vBActivity - Bars
    Lv. Percent
    10.49%

    Re: advmath.zh - atan, acos, asin

    Well, findangle isn't directly atan2, but either way. I had no idea ArcTan existed.

    Also, I've updated my post with this revalation, and with a version of the script that actually compiles.
    Tale of the Cave - a web comic - Updates Monday, Wednesday, Friday
    ZC Tutorials - Tutorials and Script Library - Updated July 30, 2008
    ZeldaGuard - Corruption in my save files? It's more likely than you think!
    I do script requests!

  5. #5
    Octorok Elmensajero's Avatar
    Join Date
    May 2008
    Age
    35
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    851
    Level
    10
    vBActivity - Bars
    Lv. Percent
    18.72%

    Re: advmath.zh - atan, acos, asin

    Very nice job getting these scripted! I tested them, and for about half of the values they were 100% accurate (within 4 decimal places of course.) The other half was off by only 0.0001, but this is prob just due to the fifth decimal place not being considered when truncating the result to 4 decimal places.
    Looking for the newest 2.5 content? Check out CZC for quests, demos, scripts, and more!

  6. #6
    Lynel
    ZC Developer
    pkmnfrk's Avatar
    Join Date
    Jan 2004
    Location
    Toronto
    Age
    37
    Posts
    1,248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    3,142
    Level
    18
    vBActivity - Bars
    Lv. Percent
    10.49%

    Re: advmath.zh - atan, acos, asin

    I don't know if the engine rounds or truncates the fifth decimal place, but I imagine that it doesn't matter a whole lot.

    Now, if you chain these together, the imprecision will add up to larger errors, but then again, nothing that will break your game.
    Tale of the Cave - a web comic - Updates Monday, Wednesday, Friday
    ZC Tutorials - Tutorials and Script Library - Updated July 30, 2008
    ZeldaGuard - Corruption in my save files? It's more likely than you think!
    I do script requests!

  7. #7
    The Time-Loop Continues ZC Developer
    Gleeok's Avatar
    Join Date
    Apr 2007
    Posts
    4,826
    Mentioned
    259 Post(s)
    Tagged
    10 Thread(s)
    vBActivity - Stats
    Points
    12,958
    Level
    33
    vBActivity - Bars
    Lv. Percent
    26.17%

    Re: advmath.zh - atan, acos, asin

    Cool. :)

    Y'know, my only qualm is that the variable names are too short (and thus confusing or misleading), and there needs to be longer explanations of what/how to do/use them, because something like this could be built on in the future by anyone wanting to contribute. ...Uugh, what I'm basically saying is a standard "math.zh" file is a pretty good idea. I've got some functions that come in handy, and I'm sure others have usefull ones as well. We'd just have to stick them all into one file.
    This post contains the official Gleeok seal of approval. Look for these and other posts in an area near you.

  8. #8
    Lynel
    ZC Developer
    pkmnfrk's Avatar
    Join Date
    Jan 2004
    Location
    Toronto
    Age
    37
    Posts
    1,248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    3,142
    Level
    18
    vBActivity - Bars
    Lv. Percent
    10.49%

    Re: advmath.zh - atan, acos, asin

    The variable names are 100% internal. So, bugger off

    Actually, the real reason is that I do not understand that code at all. I merely ported it, and that's what the original source called them. And, though I added a few new ones (also internal), I kept the names short so the constants would line up.

    In short, you're welcome to use the functions (the documented ones, anyway), but leave the constants alone, I have no idea what they represent!
    Tale of the Cave - a web comic - Updates Monday, Wednesday, Friday
    ZC Tutorials - Tutorials and Script Library - Updated July 30, 2008
    ZeldaGuard - Corruption in my save files? It's more likely than you think!
    I do script requests!

  9. #9
    Octorok
    Join Date
    Nov 2006
    Age
    29
    Posts
    463
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    1,770
    Level
    14
    vBActivity - Bars
    Lv. Percent
    8.05%

    Re: advmath.zh - atan, acos, asin

    Wow, you made a SECOND scripting engine for it to draw from? Wow, nice work. I'll like using those new commands. So we just have to

    import "std.zh"
    import "advmath.zh"?

    You're doing really nice work pkmnfrk! Stop it, you're making the rest of us look bad!

    Click here to level up my card, or be sacrificed... ^_^

    Want to see Shattered Earth, AGN's RPG, back? Got a computer that is capable of running a server? PM me!

  10. #10
    Lynel
    ZC Developer
    pkmnfrk's Avatar
    Join Date
    Jan 2004
    Location
    Toronto
    Age
    37
    Posts
    1,248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    3,142
    Level
    18
    vBActivity - Bars
    Lv. Percent
    10.49%

    Re: advmath.zh - atan, acos, asin

    Quote Originally Posted by Din View Post
    Wow, you made a SECOND scripting engine for it to draw from?
    Er, no. That's not-

    ...

    Actually, yes. Yes I did.

    Quote Originally Posted by Din View Post
    Wow, nice work. I'll like using those new commands. So we just have to

    import "std.zh"
    import "advmath.zh"?
    Yup.

    Quote Originally Posted by Din View Post
    You're doing really nice work pkmnfrk! Stop it, you're making the rest of us look bad!
    I'm just trying to make your lives easier :)
    Tale of the Cave - a web comic - Updates Monday, Wednesday, Friday
    ZC Tutorials - Tutorials and Script Library - Updated July 30, 2008
    ZeldaGuard - Corruption in my save files? It's more likely than you think!
    I do script requests!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Armageddon Games is a game development group founded in 1997. We are extremely passionate about our work and our inspirations are mostly drawn from games of the 8-bit and 16-bit era.
Social