Trigonometry Functions

By Wolfenhex
created 12/15/03

Yes, these are real trigonometry functions (not giant if/else statements) that you can use in your RMS. I made them with the help of my friends Bryan and Gabe in July 2003, but they seem to have been forgotten about. I’m writing this in hopes that they will hopefully help future RMS creators (and maybe help ES finally add them to the game).

You can change the for(i = 1; < 100) statement to a different number, but it will be less accurate. Also, it has never gone all 100 cycles in testing (that is what the break; syntax is for).

The functions have an underscore (_) before them because I was not sure if ES would add them in their expansion pack and did not want them to override ES’s functions in case they did.

demo map:

This map is basically a spider web (the more players the more complex the map). It uses a lot of trig and is a good demo map to see how to use these functions. Download it Here

code:

/*

   I would like to thank Bryan and Gabe for their math help, and the Greeks
   for creating something left out of a game that they are one of the stars of.

   I would also like to thank Ensemble Studios for creating a rmDegreesToRadians
   function without including any other trigonometry functions to use it with.

   - Wolfenhex

*/

float PI = 3.141592;

float _pow(float n = 0,int x = 0) {
    float r = n;
    for(i = 1; < x) {
        r = r * n;
    }
    return (r);
}

float _atan(float n = 0) {
    float m = n;
    if(n > 1) m = 1.0 / n;
    if(n < -1) m = -1.0 / n;
    float r = m;
    for(i = 1; < 100) {
        int j = i * 2 + 1;
        float k = _pow(m,j) / j;
        if(k == 0) break;
        if(i % 2 == 0) r = r + k;
        if(i % 2 == 1) r = r - k;
    }
    if(n > 1 || n < -1) r = PI / 2.0 - r;
    if(n < -1) r = 0.0 - r;
    return (r);
}

float _atan2(float z = 0,float x = 0) {
    if(x > 0) return (_atan(z / x));
    if(x < 0) {
        if(z < 0) return (_atan(z / x) - PI);
        if(z > 0) return (_atan(z / x) + PI);
        return (PI);
    }
    if(z > 0) return (PI / 2.0);
    if(z < 0) return (0.0 - (PI / 2.0));
    return (0);
}

float _fact(float n = 0) {
    float r = 1;
    for(i = 1; <= n) {
        r = r * i;
    }
    return (r);
}

float _cos(float n = 0) {
    float r = 1;
    for(i = 1; < 100) {
        int j = i * 2;
        float k = _pow(n,j) / _fact(j);
        if(k == 0) break;
        if(i % 2 == 0) r = r + k;
        if(i % 2 == 1) r = r - k;
    }
    return (r);
}

float _sin(float n = 0) {
    float r = n;
    for(i = 1; < 100) {
        int j = i * 2 + 1;
        float k = _pow(n,j) / _fact(j);
        if(k == 0) break;
        if(i % 2 == 0) r = r + k;
        if(i % 2 == 1) r = r - k;
    }
    return (r);
}

Back to Section