Floating Points Rounding at 4 Decimals
by Gregory Ray · in Torque Game Builder · 09/30/2008 (10:35 pm) · 10 replies
I have some floating points with 6 decimals and its rounding it to 4 and throwing off my formulas, any ideas are appreciated.
#2
Thank you,
Greg
10/01/2008 (9:44 am)
What is the typical process for doing this? Do you edit the engine source code and recompile or can you use c++ in the cs file and have it compiled at run time? If you do have to do it in the engine source, where would you do it so that when the next update comes out the code can be easily moved over? Are there any examples where the game logic is done in c++? I really appreciate the help.Thank you,
Greg
#3
10/01/2008 (10:20 am)
You would need to edit the engine source code and recompile (any C++ changes require a recompilation of the engine just like any other application). There is a C++ forum for TGB as well. I think TDN might have some handy information for working with the engine source, but I can't remember.
#4
10/01/2008 (10:58 am)
Can you post an example of your code?
#5
$walkingSimulation = new SimSet(Walking);
$step1 = new SimObject(Step1); $step1.latitude = 40.630311; $step1.longitude = -111.891340;
$step2 = new SimObject(Step2); $step2.latitude = 40.630300; $step2.longitude = -111.891340;
$step3 = new SimObject(Step3); $step3.latitude = 40.630289; $step3.longitude = -111.891340;
$step4 = new SimObject(Step4); $step4.latitude = 40.630278; $step4.longitude = -111.891340;
$step5 = new SimObject(Step5); $step5.latitude = 40.630267; $step5.longitude = -111.891340;
$step6 = new SimObject(Step6); $step6.latitude = 40.630256; $step6.longitude = -111.891340;
$step7 = new SimObject(Step7); $step7.latitude = 40.630245; $step7.longitude = -111.891340;
$step8 = new SimObject(Step8); $step8.latitude = 40.630234; $step8.longitude = -111.891340;
$step9 = new SimObject(Step9); $step9.latitude = 40.630223; $step9.longitude = -111.891340;
$step10 = new SimObject(Step10); $step10.latitude = 40.630212; $step10.longitude = -111.891340;
$walkingSimulation.add($step1);
$walkingSimulation.add($step2);
$walkingSimulation.add($step3);
$walkingSimulation.add($step4);
$walkingSimulation.add($step5);
$walkingSimulation.add($step6);
$walkingSimulation.add($step7);
$walkingSimulation.add($step8);
$walkingSimulation.add($step9);
$walkingSimulation.add($step10);
function findAzimuth(%current, %next){
%pi = 3.14159265358979323846;
%lon1 = %current.longitude;
%lat1 = %current.latitude;
%lon2 = %next.longitude;
%lat2 = %next.latitude;
//find distance %d
%dlon = %lon2 - %lon1;
%dlat = %lat2 - %lat1;
%a = mPow(mSin(%dlat/2),2) + (mCos(%lat1) * mCos(%lat2) * mPow(mSin(%dlon/2),2));
%c = 2 * mAsin(min(1,mSqrt(%a)));
%d = 3956 * %c;
%x = mCos((mSin(%lat2) - mSin(%lat1)*mCos(%d))/(mSin(%d)*mCos(%lat1)));
if(mSin(%lon2-%lon1) < 0){
%phi = %x;
} else {
%phi = 2*%pi - %x;
}
return %phi;
}
10/01/2008 (11:03 am)
Sure, I am just doing some GPS simulation, problem is the level of accuracy needed in the coords is at the 6th decimal place, so when it drops it to the 4th then it can't simulate properly.$walkingSimulation = new SimSet(Walking);
$step1 = new SimObject(Step1); $step1.latitude = 40.630311; $step1.longitude = -111.891340;
$step2 = new SimObject(Step2); $step2.latitude = 40.630300; $step2.longitude = -111.891340;
$step3 = new SimObject(Step3); $step3.latitude = 40.630289; $step3.longitude = -111.891340;
$step4 = new SimObject(Step4); $step4.latitude = 40.630278; $step4.longitude = -111.891340;
$step5 = new SimObject(Step5); $step5.latitude = 40.630267; $step5.longitude = -111.891340;
$step6 = new SimObject(Step6); $step6.latitude = 40.630256; $step6.longitude = -111.891340;
$step7 = new SimObject(Step7); $step7.latitude = 40.630245; $step7.longitude = -111.891340;
$step8 = new SimObject(Step8); $step8.latitude = 40.630234; $step8.longitude = -111.891340;
$step9 = new SimObject(Step9); $step9.latitude = 40.630223; $step9.longitude = -111.891340;
$step10 = new SimObject(Step10); $step10.latitude = 40.630212; $step10.longitude = -111.891340;
$walkingSimulation.add($step1);
$walkingSimulation.add($step2);
$walkingSimulation.add($step3);
$walkingSimulation.add($step4);
$walkingSimulation.add($step5);
$walkingSimulation.add($step6);
$walkingSimulation.add($step7);
$walkingSimulation.add($step8);
$walkingSimulation.add($step9);
$walkingSimulation.add($step10);
function findAzimuth(%current, %next){
%pi = 3.14159265358979323846;
%lon1 = %current.longitude;
%lat1 = %current.latitude;
%lon2 = %next.longitude;
%lat2 = %next.latitude;
//find distance %d
%dlon = %lon2 - %lon1;
%dlat = %lat2 - %lat1;
%a = mPow(mSin(%dlat/2),2) + (mCos(%lat1) * mCos(%lat2) * mPow(mSin(%dlon/2),2));
%c = 2 * mAsin(min(1,mSqrt(%a)));
%d = 3956 * %c;
%x = mCos((mSin(%lat2) - mSin(%lat1)*mCos(%d))/(mSin(%d)*mCos(%lat1)));
if(mSin(%lon2-%lon1) < 0){
%phi = %x;
} else {
%phi = 2*%pi - %x;
}
return %phi;
}
#6
I just saw that someone already has done this for you :)
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=13620
-Michael
10/01/2008 (11:48 am)
I think the problem are the math functions. All of them convert their argument to float and they return a float. So you lose some precession. If that really is the problem, you could just add some "double"-versions of the functions you need. That would also easily port to any new engine releases.I just saw that someone already has done this for you :)
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=13620
-Michael
#7
If you convert findAzimuth to a ConsoleFunction, you could put it anywhere, but it might make sense to make your own .cpp and .h for myGameFunctions.
As a rough template, this might get you started...
10/01/2008 (12:22 pm)
Is it truncating the numbers when you assign them to The latitude/longitude fields? Or just later during "findAzimuth"? And if it happens during findAziumth, during which part?If you convert findAzimuth to a ConsoleFunction, you could put it anywhere, but it might make sense to make your own .cpp and .h for myGameFunctions.
As a rough template, this might get you started...
#include "console/consoleTypes.h"
#include "math/mMathFc.h" // something like that.
#include "the 64 bit precision math library resource"
ConsoleFunction( findAzimuth, const char*, 5, 5, "(F64 lon1, F64 lat1, F64 lon1, F64 lat1)" )
{
F64 lon1 = dAtof64(argv[1]);
F64 lat1 = dAtof64(argv[1]);
F64 lon2 = dAtof64(argv[1]);
F64 lat2 = dAtof64(argv[1]);
//find distance
F64 dlon = lon2 - lon1;
F64 dlat = lat2 - lat1;
// these all have to be converted to F64 versions of the math functions...
/*
F64 a = mPow(mSin(%dlat/2),2) + (mCos(%lat1) * mCos(%lat2) * mPow(mSin(%dlon/2),2));
F64 c = 2.0 * mAsin(min(1,mSqrt(%a)));
F64 d = 3956.0 * c;
%x = mCos((mSin(%lat2) - mSin(%lat1)*mCos(%d))/(mSin(%d)*mCos(%lat1)));
if(mSin(%lon2-%lon1) < 0)
{
%phi = %x;
}
else
{
%phi = 2*%pi - %x;
}
*/
char * buff = Con::getReturnBuffer(256);
dSprintf( buff, 256, "%d", phi );
return buff;
}
#8
10/01/2008 (2:05 pm)
Guys this is exactly what I needed, my last question. Once I compile the engine with these new console functions, how can I get the new engine over to TGB so when I hit play scene it has my new 64 bit functions.
#9
10/01/2008 (3:15 pm)
If you add the c++ source file from the resource to the TGBGame project and recompile the project then the functions should be available in torquescript automatically. If they are not, be sure you are actually using the right executable. If you make a debug build for example, it will not be used if you press the play button TGB. What will always work is setting the working directory for debugging to the directory your project resides in and then start TGBGame from Visual Studio.
#10
10/06/2008 (11:31 pm)
Also, if I'm not mistaken, if you copied the executable to the Project directory at Project creation, you'll need to copy the newly compiled TGBGame.exe to your project directory.
Associate James Ford
Sickhead Games