Forum

Mathlib v2

Discuss programming in the QuakeC language.

Moderator: InsideQC Admins

Mathlib v2

Postby FrikaC » Tue May 16, 2006 11:36 am

Some of you may remember my old mathlib file I posted several years ago. Well, I got around to updating it. So without further ado:

http://www.inside3d.com/frikbot/qc/mathlib.qc (Right click and choose Save As)

For those unfamiliar with this file, it provides some sorely missing arithmetic and trigonomic functions to QuakeC: sqrt (square root) cos (cosine) sin (sine) pow (raise to power). In this version I've added min, max, bound, randomvec and bitshift, these emulate the Darkplaces engine features of the same name.

As a bonus, I've also added a large chunk of code to make mathlib use the engine math functions if they are available. So, if you're already using these engine functions, you can plop this file into your project and provide QuakeC alternatives so that your mod is less tied to a specific engine. Or, on the other hand, if you're really unsure about using these engine features for fear of breaking compatability, you can optionally support them.

A warning however about QMB. Although it reports DP_QC_SINCOSSQRTPOW, DP_QC_MINMAXBOUND, DP_QC_RANDOMVEC it appears it's support for the functions pow, min, max, bound and randomvec are all broken in it. If your mod absolutely needs to run in QMB, you can call the mathlib_ equivalents of these functions directly instead. (Pow and randomvec are the only major losses).

Also for testing and as an example, I uploaded mathlib_test.qc which was used to debug the engine features.

For instructions on installing and using mathlib.qc, refer to the comment at the top of the file.


Feedback welcomed.
FrikaC
Site Admin
 
Posts: 1026
Joined: Fri Oct 08, 2004 11:19 pm

Postby Error » Tue May 16, 2006 7:52 pm

great work, thanks Frik
User avatar
Error
InsideQC Staff
 
Posts: 865
Joined: Fri Nov 05, 2004 5:15 am
Location: VA, USA

Postby Dr. Shadowborg » Tue May 16, 2006 11:03 pm

I might help to also include some examples of what these would be good for, ala Gyro...
User avatar
Dr. Shadowborg
InsideQC Staff
 
Posts: 1110
Joined: Sat Oct 16, 2004 3:34 pm

Postby Baker » Tue May 16, 2006 11:23 pm

Nice!

n00b question ... is there a string lib hanging around?
User avatar
Baker
 
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Postby Sajt » Wed May 17, 2006 1:29 am

There's not much a string lib could do on its own :p
F. A. Špork, an enlightened nobleman and a great patron of art, had a stately Baroque spa complex built on the banks of the River Labe.
Sajt
 
Posts: 1215
Joined: Sat Oct 16, 2004 3:39 am

Postby FrikaC » Wed May 17, 2006 3:42 am

Dr. Shadowborg wrote:I might help to also include some examples of what these would be good for, ala Gyro...


Yes, it would be good for Gyro.
FrikaC
Site Admin
 
Posts: 1026
Joined: Fri Oct 08, 2004 11:19 pm

Postby Dr. Shadowborg » Wed May 17, 2006 3:32 pm

FrikaC wrote:Yes, it would be good for Gyro.


No no, I meant actual practical applications for this library, in a fashon similar to the examples that Gyro shipped with. (Stuff like blimp grenade and whatnot.) Basically, a "what can this library do for my mod?" type Q&A. :wink:
User avatar
Dr. Shadowborg
InsideQC Staff
 
Posts: 1110
Joined: Sat Oct 16, 2004 3:34 pm

Postby Quake Matt » Wed May 17, 2006 6:09 pm

Yes, it would be good for Gyro.

Indeed - I'm already thinking about the sin/cos functions!

I don't suppose there's any chance of getting a good fractional pow function, is there? I've already got one for Gyro, but it's weak, even unusable, in most situations. For x^y, it's only accurate for 0.8 < x < 1.0 and 0.0 < y < 0.1, but fortunately that's all I need for now!
User avatar
Quake Matt
 
Posts: 129
Joined: Sun Jun 05, 2005 9:59 pm

Postby FrikaC » Wed May 17, 2006 8:38 pm

Dr. Shadowborg wrote:No no, I meant actual practical applications for this library, in a fashon similar to the examples that Gyro shipped with. (Stuff like blimp grenade and whatnot.) Basically, a "what can this library do for my mod?" type Q&A. :wink:


I knew what you meant, I was being facetious. I can't think of any exmaples off the top of my head. I used some of these functions in a funky railgun mod, some of them in Prydon...

Quake Matt wrote:I don't suppose there's any chance of getting a good fractional pow function, is there? I've already got one for Gyro, but it's weak, even unusable, in most situations. For x^y, it's only accurate for 0.8 < x < 1.0 and 0.0 < y < 0.1, but fortunately that's all I need for now!


Sure, but speed could be an issue. the equation is simple once you have sqrt()....I need to think of a way to optimize this stuff.
FrikaC
Site Admin
 
Posts: 1026
Joined: Fri Oct 08, 2004 11:19 pm

Postby Preach » Wed May 17, 2006 8:57 pm

Here's a pretty general power function for a positive float to a float power(can be positive, negative, fractional or whole). It does some loops so you can control how accurately it works. By way of comparison, it gets low integers to integer powers correct to only 2 or 3 dps under the default settings. On the other hand, being very general also means it's not the most computationally friendly beast, so you wouldn't want to call it often. Anyway, you asked if it was possible, and so here it is:
Code: Select all
float paccuracy; //define the global

float(float a, float b) Pow2 =
{
float e1,e2,f,i;

if(paccuracy <= 0)
   paccuracy = 0.001;   //this sets a level of accuracy, it's a global float
            //so you can set it from the calling function to specify
            //a level of accuracy per call
            //if you've set it to something stupid, or it's not been set yet
            //this is a fairly good start

f = (a - 1) / (a + 1);
            //this is the first trick
            //we're essentially doing exp(b*log(a))
            //but the power series for log (1+x) is only defined for small x
            //however log x = 2 * arctanh((x-1)/(x+1)) which will converge for any x we choose
            
e2 = 2 * f;
i = 1;
f = f * f;


while(fabs(e2) > paccuracy)
   {
   e1 = e1 + e2;               //this calculates successive terms of arctanh
   e2 = e2 * f * ((2 * i) - 1) / ((2 * i) + 1);   //when the absolute value of a term drops
   i = i + 1;               //below accuracy we call it a day
   }                  //note that this doesn't actually mean
                     //the output is accurate to 0.001, there's no
                     //direct bound on accuracy



f = e2 = e1 * b;

e1 = 1;
i = 1;

while(fabs(e2) > paccuracy)            //same idea, this is the exponential function
   {                  //which has a nice power series
   e1 = e1 + e2;               //same comments about accuracy apply, except
   i = i + 1;               //the rapid decay of terms mean it's probably
   e2 = e2 * f / i;            //close to the true value of exp f, if not pow(a,b)
   }


return e1;

}
Preach
 
Posts: 122
Joined: Thu Nov 25, 2004 7:20 pm

Postby Dr. Shadowborg » Thu May 18, 2006 12:06 am

FrikaC wrote:I knew what you meant, I was being facetious. I can't think of any exmaples off the top of my head. I used some of these functions in a funky railgun mod, some of them in Prydon...


I'll try and cook up a little something I'm now calling "Mathfrag", to help demonstrate some of this stuff. Probably won't be much more than a weapon or two, but if anybody wants to contribute code to it that uses mathlib.qc, feel free to bug me via PM and I'll add it.
User avatar
Dr. Shadowborg
InsideQC Staff
 
Posts: 1110
Joined: Sat Oct 16, 2004 3:34 pm

Postby FrikaC » Thu May 18, 2006 2:26 am

Thanks Preach, saved me a lot of trouble. I'll include it in the next version of mathlib if you don't mind.
FrikaC
Site Admin
 
Posts: 1026
Joined: Fri Oct 08, 2004 11:19 pm

Postby Preach » Thu May 18, 2006 9:04 am

Yeah, sure thing.
Preach
 
Posts: 122
Joined: Thu Nov 25, 2004 7:20 pm

Postby Quake Matt » Thu May 18, 2006 11:58 am

Anyway, you asked if it was possible, and so here it is:

I see - possible, but probably not a very good idea! Gyro simply takes a 1/(x + 1) curve and transforms it to fit a ^x curve. Not particularly accurate, but quite fast. Speed is pretty important, since it could be run many times per frame, to scale multiplications (only motion resistance right now) by the ticktime.

One thing I would like to see is matrix maths, for rotations and other transformations. I'd suggest quaternions, too, but I can't get my head round them!
User avatar
Quake Matt
 
Posts: 129
Joined: Sun Jun 05, 2005 9:59 pm

Postby FrikaC » Thu May 18, 2006 2:19 pm

Quake Matt wrote:One thing I would like to see is matrix maths, for rotations and other transformations. I'd suggest quaternions, too, but I can't get my head round them!


Now lets not get crazy here.
FrikaC
Site Admin
 
Posts: 1026
Joined: Fri Oct 08, 2004 11:19 pm

Next

Return to QuakeC Programming

Who is online

Users browsing this forum: No registered users and 1 guest