View previous topic :: View next topic |
Author |
Message |
FrikaC Site Admin

Joined: 08 Oct 2004 Posts: 947
|
Posted: Tue May 16, 2006 11:36 am Post subject: Mathlib v2 |
|
|
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. _________________
 |
|
Back to top |
|
 |
Error Inside3D Staff

Joined: 05 Nov 2004 Posts: 558 Location: VA, USA
|
|
Back to top |
|
 |
Dr. Shadowborg Inside3D Staff

Joined: 16 Oct 2004 Posts: 726
|
Posted: Tue May 16, 2006 11:03 pm Post subject: |
|
|
I might help to also include some examples of what these would be good for, ala Gyro... _________________ "Roboto suggests Plasma Bazooka." |
|
Back to top |
|
 |
Baker

Joined: 14 Mar 2006 Posts: 1538
|
Posted: Tue May 16, 2006 11:23 pm Post subject: |
|
|
Nice!
n00b question ... is there a string lib hanging around? |
|
Back to top |
|
 |
Sajt
Joined: 16 Oct 2004 Posts: 1026
|
Posted: Wed May 17, 2006 1:29 am Post subject: |
|
|
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. |
|
Back to top |
|
 |
FrikaC Site Admin

Joined: 08 Oct 2004 Posts: 947
|
Posted: Wed May 17, 2006 3:42 am Post subject: |
|
|
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. _________________
 |
|
Back to top |
|
 |
Dr. Shadowborg Inside3D Staff

Joined: 16 Oct 2004 Posts: 726
|
Posted: Wed May 17, 2006 3:32 pm Post subject: |
|
|
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.  _________________ "Roboto suggests Plasma Bazooka." |
|
Back to top |
|
 |
Quake Matt

Joined: 05 Jun 2005 Posts: 129
|
Posted: Wed May 17, 2006 6:09 pm Post subject: |
|
|
Quote: | 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! |
|
Back to top |
|
 |
FrikaC Site Admin

Joined: 08 Oct 2004 Posts: 947
|
Posted: Wed May 17, 2006 8:38 pm Post subject: |
|
|
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.  |
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. _________________
 |
|
Back to top |
|
 |
Preach
Joined: 25 Nov 2004 Posts: 122
|
Posted: Wed May 17, 2006 8:57 pm Post subject: |
|
|
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: | 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;
}
|
|
|
Back to top |
|
 |
Dr. Shadowborg Inside3D Staff

Joined: 16 Oct 2004 Posts: 726
|
Posted: Thu May 18, 2006 12:06 am Post subject: |
|
|
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. _________________ "Roboto suggests Plasma Bazooka." |
|
Back to top |
|
 |
FrikaC Site Admin

Joined: 08 Oct 2004 Posts: 947
|
Posted: Thu May 18, 2006 2:26 am Post subject: |
|
|
Thanks Preach, saved me a lot of trouble. I'll include it in the next version of mathlib if you don't mind. _________________
 |
|
Back to top |
|
 |
Preach
Joined: 25 Nov 2004 Posts: 122
|
Posted: Thu May 18, 2006 9:04 am Post subject: |
|
|
Yeah, sure thing. |
|
Back to top |
|
 |
Quake Matt

Joined: 05 Jun 2005 Posts: 129
|
Posted: Thu May 18, 2006 11:58 am Post subject: |
|
|
Quote: | 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! |
|
Back to top |
|
 |
FrikaC Site Admin

Joined: 08 Oct 2004 Posts: 947
|
Posted: Thu May 18, 2006 2:19 pm Post subject: |
|
|
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. |
|
Back to top |
|
 |
|
|
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 © 2004 phpBB Group
|