3D Rad - Free 3D game maker - Forum

This forum is now archived!

This forum is locked, and is a read-only version. A new community-ran forum can be found at classdev.net

News:

The 3DRad community can be found at classdev.net.

Pages: [1] 2 3 ... 8

Author Topic: RadMath dll  (Read 11727 times)

jestermon

« on: July 15, 2010, 02:04:42 AM »
RadMath.dll, a dll with 17 maths functions for those who need it.
See the z_RadMath_Test.3dr for examples of use.

The zip file contains RadMath.dll to be place in the script directory, the RadMath.3dr project to be used as a script base, and z_RadMath_Test.3dr a test as well as an example project.

The image shows a screen shot of z_RadMath_Test.3dr in operation, and give an overview of the functions available.

z_RadMath_Test.3dr

Code: [Select]
///****************************************************************************
/// START OF RADMATH FUNCTION BLOCK
///****************************************************************************
int MathDLLHandle=0;
//-----------------------------------------------------------------------------
//R_radians2degrees
//-----------------------------------------------------------------------------
float R_radians2degrees(float value)
{
   iDLLArraySet(0,value);
   iDLLCall(MathDLLHandle,"R_radians2degrees",0);
   float f = iDLLArrayGet(0);
   return f;
}
//-----------------------------------------------------------------------------
//R_degrees2radians
//-----------------------------------------------------------------------------
float R_degrees2radians(float value)
{
   iDLLArraySet(0,value);
   iDLLCall(MathDLLHandle,"R_degrees2radians",0);
   float f = iDLLArrayGet(0);
   return f;
}
//-----------------------------------------------------------------------------
//R_cos
//-----------------------------------------------------------------------------
float R_cos(float value)
{
   iDLLArraySet(0,value);
   iDLLCall(MathDLLHandle,"R_cos",0);
   float f = iDLLArrayGet(0);
   return f;
}
//-----------------------------------------------------------------------------
//R_sin
//-----------------------------------------------------------------------------
float R_sin(float value)
{
   iDLLArraySet(0,value);
   iDLLCall(MathDLLHandle,"R_sin",0);
   float f = iDLLArrayGet(0);
   return f;
}
//-----------------------------------------------------------------------------
//R_tan
//-----------------------------------------------------------------------------
float R_tan(float value)
{
   iDLLArraySet(0,value);
   iDLLCall(MathDLLHandle,"R_tan",0);
   float f = iDLLArrayGet(0);
   return f;
}
//-----------------------------------------------------------------------------
//R_acos
//-----------------------------------------------------------------------------
float R_acos(float value)
{
   iDLLArraySet(0,value);
   iDLLCall(MathDLLHandle,"R_acos",0);
   float f = iDLLArrayGet(0);
   return f;
}
//-----------------------------------------------------------------------------
//R_atan
//-----------------------------------------------------------------------------
float R_atan(float value)
{
   iDLLArraySet(0,value);
   iDLLCall(MathDLLHandle,"R_atan",0);
   float f = iDLLArrayGet(0);
   return f;
}
//-----------------------------------------------------------------------------
//R_asin
//-----------------------------------------------------------------------------
float R_asin(float value)
{
   iDLLArraySet(0,value);
   iDLLCall(MathDLLHandle,"R_asin",0);
   float f = iDLLArrayGet(0);
   return f;
}
//-----------------------------------------------------------------------------
//R_sinh
//-----------------------------------------------------------------------------
float R_sinh(float value)
{
   iDLLArraySet(0,value);
   iDLLCall(MathDLLHandle,"R_sinh",0);
   float f = iDLLArrayGet(0);
   return f;
}
//-----------------------------------------------------------------------------
//R_cosh
//-----------------------------------------------------------------------------
float R_cosh(float value)
{
   iDLLArraySet(0,value);
   iDLLCall(MathDLLHandle,"R_cosh",0);
   float f = iDLLArrayGet(0);
   return f;
}
//-----------------------------------------------------------------------------
//R_tanh
//-----------------------------------------------------------------------------
float R_tanh(float value)
{
   iDLLArraySet(0,value);
   iDLLCall(MathDLLHandle,"R_tanh",0);
   float f = iDLLArrayGet(0);
   return f;
}
//-----------------------------------------------------------------------------
//R_atan2
//-----------------------------------------------------------------------------
float R_atan2(float value1, float value2)
{
   iDLLArraySet(0,value1);
   iDLLArraySet(1,value2);
   iDLLCall(MathDLLHandle,"R_atan2",0);
   float f = iDLLArrayGet(0);
   return f;
}
//-----------------------------------------------------------------------------
//R_exp
//-----------------------------------------------------------------------------
float R_exp(float value)
{
   iDLLArraySet(0,value);
   iDLLCall(MathDLLHandle,"R_exp",0);
   float f = iDLLArrayGet(0);
   return f;
}
//-----------------------------------------------------------------------------
//R_log
//-----------------------------------------------------------------------------
float R_log(float value)
{
   iDLLArraySet(0,value);
   iDLLCall(MathDLLHandle,"R_log",0);
   float f = iDLLArrayGet(0);
   return f;
}
//-----------------------------------------------------------------------------
//R_log10
//-----------------------------------------------------------------------------
float R_log10(float value)
{
   iDLLArraySet(0,value);
   iDLLCall(MathDLLHandle,"R_log10",0);
   float f = iDLLArrayGet(0);
   return f;
}
//-----------------------------------------------------------------------------
//R_sqrt
//-----------------------------------------------------------------------------
float R_sqrt(float value)
{
   iDLLArraySet(0,value);
   iDLLCall(MathDLLHandle,"R_sqrt",0);
   float f = iDLLArrayGet(0);
   return f;
}
//-----------------------------------------------------------------------------
//R_pow
//-----------------------------------------------------------------------------
float R_pow(float value1, float value2)
{
   iDLLArraySet(0,value1);
   iDLLArraySet(1,value2);
   iDLLCall(MathDLLHandle,"R_pow",0);
   float f = iDLLArrayGet(0);
   return f;
}
//-----------------------------------------------------------------------------
//MathInitialize
//-----------------------------------------------------------------------------
void MathInitialize()
{
   MathDLLHandle = iDLLLoad(".\\3DRad_res\\objects\\Script\\RadMath.dll");
}
//-----------------------------------------------------------------------------
//MathDeInitialize
//-----------------------------------------------------------------------------
void MathDeInitialize()
{
   if (MathDLLHandle != 0)
      iDLLUnload(MathDLLHandle);
}
///****************************************************************************
/// END OF RADMATH FUNCTION BLOCK
///****************************************************************************

//Add your globals here

string PS;
int PRINT = OBJ_0;
float [] results (17);


//-----------------------------------------------------------------------------
//Print results
//-----------------------------------------------------------------------------
void printResults()
{
   iPrint(PS = "sin(15) = "+results[0],-10,11,PRINT);
   iPrint(PS = "cos(10) = "+results[1],-10,10,PRINT);
   iPrint(PS = "tan(40) = "+results[2],-10,9,PRINT);
   iPrint(PS = "radians2degrees(1) = "+results[3],-10,8,PRINT);
   iPrint(PS = "degrees2radians(60) = "+results[4],-10,7,PRINT);
   iPrint(PS = "acos(0.123) = "+results[5],-10,6,PRINT);
   iPrint(PS = "atan(1) = "+results[6],-10,5,PRINT);
   iPrint(PS = "asin(0.123) = "+results[7],-10,4,PRINT);
   iPrint(PS = "sinh(1) = "+results[8],-10,3,PRINT);
   iPrint(PS = "cosh(1) = "+results[9],-10,2,PRINT);
   iPrint(PS = "tanh(1) = "+results[10],-10,1,PRINT);
   iPrint(PS = "atan2(1,2) = "+results[11],-10,0,PRINT);
   iPrint(PS = "exp(1) = "+results[12],-10,-1,PRINT);
   iPrint(PS = "log(20) = "+results[13],-10,-2,PRINT);
   iPrint(PS = "log10(5) = "+results[14],-10,-3,PRINT);
   iPrint(PS = "sqrt(81) = "+results[15],-10,-4,PRINT);
   iPrint(PS = "pow(9,2) = "+results[16],-10,-5,PRINT);
}

//-----------------------------------------------------------------------------
//MathTest
//-----------------------------------------------------------------------------
void MathTest()
{
   results[0] = R_sin(15);
   results[1] = R_cos(10);
   results[2] = R_tan(45);
   results[3] = R_radians2degrees(1);
   results[4] = R_degrees2radians(60);
   results[5] = R_acos(0.123);
   results[6] = R_atan(1);
   results[7] = R_asin(0.123);
   results[8] = R_sinh(1);
   results[9] = R_cosh(1);
   results[10]= R_tanh(1);
   results[11]= R_atan2(1,2);
   results[12]= R_exp(1);
   results[13]= R_log(20);
   results[14]= R_log10(5);
   results[15]= R_sqrt(81);
   results[16]= R_pow(9,2);
}




//-----------------------------------------------------------------------------
//Main
//-----------------------------------------------------------------------------
void Main()
{
   if(iInitializing())
   {
      MathInitialize();
      MathTest();
   }
   else if (iDeinitializing())
   {
      MathDeInitialize();
   }

   printResults();
}

There are certain functions already available in Rad, such as iFloatCos, iFloatSqrt etc.. But I have included all standard math functions to provide a complete suite.
« Last Edit: July 15, 2010, 02:23:56 AM by jestermon »
« Reply #1 on: July 15, 2010, 02:37:20 AM »
Excellent :)

psikotropico

« Reply #2 on: July 15, 2010, 06:02:34 AM »
supa geek...
« Reply #3 on: July 15, 2010, 11:56:13 AM »
I made a vow, a vow to see NO math in my holidays, thanks, I guess Ill blow my head off now.
I'm never here and if I am I will try and say something...
Sorry tramkp888 I kind of forgot you back in July

jestermon

« Reply #4 on: July 15, 2010, 04:27:34 PM »
I made a vow, a vow to see NO math in my holidays, thanks, I guess Ill blow my head off now.
Please do  ::)
« Reply #5 on: July 16, 2010, 06:46:48 PM »
Excellent jestermon.

Out of interest, have you ever found a use for the hyperbolic trig functions in a game?

jestermon

« Reply #6 on: July 16, 2010, 07:16:47 PM »
Excellent jestermon.

Out of interest, have you ever found a use for the hyperbolic trig functions in a game?
Not directly no; but I develop educational games, and in two of them i provide an immersed-maths 3D user experience. In other words, the user moves a character in a 3D world, and then enters functions, which are then dynamically plotted in VR. the *h hyperbolic functions were part of the plot routines. These were done with irrlicht. I have not tried to do the same in Rad yet, but with the math dll, it may be an interesting exercise.
Functions like atan2 and acos, on the other hand; are essential for things like; extracting angles between vectors etc.

jestermon

« Reply #7 on: July 17, 2010, 12:16:52 AM »
Mathemagic test 1
This is a little project that test the angle between vectors. The first in a series of new maths functions to implement advanced vector and quaternion calculations.

To Install
Simply copy the sources from the zip file into your 3DRad_res directory

File Contents
RadMath.dll goes into the scripts directory, x_vectorpointer and z_arrow together with their indexes goes into the skinmesh directory, and z_mathemagic_test_01.3dr goes into the projects directory

How to use
There are two arrows in the editor. Change the rotation on one or both arrows in the editor, then run the game. The resulting angle between the vectors of the arrows will be calculated and displayed. Repeat ad infinitum.

How it works
To obtain the direction vectors of the objects, an �invisible� vectorpointer object is given the location and orientation of the objects. The vectorpointer has a bone that is offset to 0,0,1 from the center of the object. The vector offset of the bone is used to determine the direction of the object, hence also the direction vector of the object it is aligned to. The arc cosine of the dot product of the two normalized vectors is used to determine the angle between the objects.

« Last Edit: July 17, 2010, 12:23:22 AM by jestermon »
« Reply #8 on: July 17, 2010, 01:26:20 AM »
That's so cool, but what It could be used for is beyond me. Well I know it's something used in 3dmodelers where you can measure any given angle in 3D space.

jestermon

« Reply #9 on: July 17, 2010, 02:23:27 AM »
That's so cool, but what It could be used for is beyond me. Well I know it's something used in 3dmodelers where you can measure any given angle in 3D space.
What you need is to be able to determine the angle of an object in relation to another. In other words an object in relation to the ground, the world, coordinates, targets, etc. In the case of your measuring flips, this is the first of a 3 part calculation requirement. For example angle between each of the steps in the recording need to be calculated in order to build a progressive rotation around object center. The car may flip forwards or backwards, and you need progressive rotation to accurately determine which way it turned, and for how many rotations.
Since I'm working on a solution towards your problem, maybe some can benefit from these functions. Who knows.
The object direction vector can be used for all sorts of calculations, since you always need the to know which way a thing is pointing; which helps with things like; the next point of progression in missiles, etc.
« Last Edit: July 17, 2010, 02:42:56 AM by jestermon »

jestermon

« Reply #10 on: July 19, 2010, 02:54:49 AM »
Mathemagic test 2
This project test the latest function R_quat2axis, which is the inverse of the Rad iQuaternionFromAxisAngle() function.
R_quat2axis returns the real world Yaw,Pitch,Roll
It is to be used on one axis at a time in order to give accurate results. Combined rotations are taken from the quaternion and calculated along all three axis.

How to use
Change the values in the x or y or z of the Orientation of the "test object", then run the game. The calculations will be displayed in the "real world" coordinate system.

This is useful if you wish to apply simple rotations in items like tank turrets. You can use this function, to give you a base for calculating rotations, from the angles returned by the function.

Rush, this is the second part of the 3 parts needed to do your calculations. You can experiment with rotation values.

Note: This version uses RadMath2.dll an upgrade to the previous version, since dll will contain more advanced quat and vector functions.

Edit: The returned values are always between 0 and 360.
« Last Edit: July 19, 2010, 03:42:21 AM by jestermon »
« Reply #11 on: July 19, 2010, 10:54:44 AM »
Works great! :) Looking forward to phase 3.

jestermon

« Reply #12 on: July 21, 2010, 03:58:33 AM »
Roller test 2.
This tests the fist part of the cumulative calculations to give a pretty accurate spin around the Y axis by using AxisAngles. Y is assumed to be "up" at all times, so this method is applicable.
Since X and Z are relative to the direction of travel, roll and flip can't be assumed using this direct method, without transposing an orthogonal world view to the actual path of travel. This will be applied as the final stage.
We are almost there.
Zip file contains project and and single mesh resource (to be used in quat2Angle and OthroProjection in next step)
 

jestermon

« Reply #13 on: July 21, 2010, 06:51:26 AM »
Roller Test 3 - The final stage

In this project, the OthtoProjection is tested, showing accurate X,y,z rotations in degrees, as you move the car around. There is a big red arrow at 0,0,0 for reference.
Drive the car around and notice the following
Turn Left and Right to change the Y  rotation
Go uphill and downhill to change the X rotation
Drive at a slantled left or right angle to change the Z rotation

This is a pseudo algorithm, using a boned mesh to cheat, and not a true projection, but the results are 100% accurate, allowing one to obtain the true x,y,z rotation in degrees of any object in Rad.

Rush.. This function should give you accurate values to do all your calculations with.. As  a bonus, I'll incorporate this all into a plugin for you.

For those who are just interested in playing, I've included the compiled project
« Last Edit: July 21, 2010, 07:08:22 AM by jestermon »

jestermon

« Reply #14 on: July 23, 2010, 05:00:26 PM »
Axis Recorder � A mathematical plugin tool - As requested by Rush3Fan

Axis Recorder calculates the total number of degrees rotated along the X, Y and Z axis of any object that is linked to it. The Recorder uses a pseudo projection algorithm in order to record the exact rotation delta along each of the axis, as the object is is moved and rotated in virtual space during each frame of the game. Each delta in the rotation is then summed to produce an total rotational representation.

Simply put, if for example, a car is flipped forwards, the forward angle of the car (X-axis) is measured against the angle of the ground, no matter in which direction the car is pointing. The measurement is done in degrees, and can range from 0 to 360, thus giving full coverage for a full rotation of the car. So the Recorder even knows if the car is upside down. This algorithm is applied to a flip along the X axis, a  spin along the Y axis, and a roll along the Z axis. Since this is only a recorded measurement, and not a calculated rotation, a gimbal lock is not a factor.

How to Install
Unzip the axisRoll_plugin_sources.zip file into the 3DRad_res directory.
The archive contains the plugin project z_plugin_axisRoll.3dr, which may be merged with any project. The test project z_roller_test_04.3dr, the Math library RadMath2.dll, and a specially boned mesh vp00 that facilitates in the measurement calculations.

How to use
1..Merge the plugin into you own project.
2..Link your object, (car, rock) to the plugin script. �Axis RECORDER�
3..In the Axis RECORDER script, change the line
    rOBJECT = OBJ_44;   //LINK TO OBJECT TO TRACK
    to reflect the correct OBJ_number for the linked object.
4..Link your own script to the plugin script. �Axis RECORDER�
5..The Axis RECORDER uses its VAL_1 value to get commands from your script..
    If VAL_1 is 1, this tells the RECORDER to start recording
    If VAL_1 is 3, this tell the RECORDER to stop recording
    If VAL_1 is 5, which is set by the RECOREDER there are x,y,z values available:
    VAL_2 has totalX rotation, VAL_3 has totalY rotation and VAL_4 has totalZ rotation.
6..The returned values are the total number of degrees that the object has rotated.
    Divide the values by 360 to get a decimal number of actual rotations per axis.
7..See the z_roller_test_04.3dr project for an example of usage.

A compiled version of the test project �Roller_Test2_compiled.zip� is provided for those who simply wish to see something in action.
The compiled project is actually named "roller_test_4_compiled.exe ", the zip file name is a typo...
Press ENTER to reset the car in the test project.

The image is just a pretty presentation of what this plugin records.
« Last Edit: July 23, 2010, 05:06:03 PM by jestermon »
Pages: [1] 2 3 ... 8