Game Development Community

Getting an inconsistancy with mRadToDeg

by Wesley Hopson · in Torque Game Engine · 09/30/2008 (11:25 pm) · 8 replies

I am getting some wierd numbers out of mDegToRad function which while not horribly off is causing me to end up with values 1 or 2 degrees off what they should be.

really was not clear exactly where to post this it mentions the RTS kit some but only because it was what i was working on the real problem is the mDegToRad function

EDIT:on advice i am moving my thread here/reposting it

code i am running it in is two key bindings i setup for the RTS camera that i wanted to control the yaw and pitch with the mouse.

function updateViewPitch(%val)
{
echo("Pitch = " @ %Val);
if ($cursorschroll == 1)
{
Echo("------------------------");
%Angle = $RTSCamera.GetPitchAngle() + -1 * %val * ($cameraFov / 90) * 0.02 * $cameraYawSpeed;
$RTSCamera.SetPitchAngle(%Angle);
Echo("we want set Pitch to " @ %Angle);
Echo("Pitch Angle = " @ $RTSCamera.GetPitchAngle());
Echo("Pitch Rad = " @ mDegToRad(%Angle));
Echo("Converted Pitch = " @ mDegToRad(mRadToDeg(%Angle)));
Echo("------------------------");
}
}

function updateViewYaw(%val)
{
if ($cursorschroll == 1)
{
Echo("------------------------");
%Angle = $RTSCamera.GetYawAngle() + -1 * %val * ($cameraFov / 90) * 0.02 * $cameraYawSpeed;
$RTSCamera.setYawAngle(%Angle);
Echo("we want to set Yaw to " @ %Angle);
Echo("Yaw Angle = " @ $RTSCamera.GetYawAngle());
Echo("Yaw Rad = " @ mDegToRad(%Angle));
Echo("Converted Yaw = " @ mDegToRad(mRadToDeg(%Angle)));
Echo("------------------------");
}
}

I noticed it works fairly well but seems a bit jerky at times in certain places so i went to investigate. I noticed that the SetYaw and pitch functions converted my deg to Radians to calculate with then converted it back to degrees with the get functions. My first instinct was to see if thier was a round off error causing my problems after all you can only approx pi so long so then i woulda just had to increase the size of the variable the radian values were being put in. What i found was a bit more than a simple round off issue. So of the values i was getting from mRadToDeg in some places the result is just wrong.

Small section of my console report for the above code:

Pitch = -8
------------------------
we want set Pitch to 42
Pitch Angle = 50
Pitch Rad = 0.872665
Converted Pitch = 50
------------------------
------------------------
we want to set Yaw to 24
Yaw Angle = 24
Yaw Rad = 0.418879
Converted Yaw = 24
------------------------
Pitch = -9
------------------------
we want set Pitch to 32
Pitch Angle = 41
Pitch Rad = 0.715585
Converted Pitch = 41.0001
------------------------
Pitch = -1
------------------------
we want set Pitch to 39
Pitch Angle = 40
Pitch Rad = 0.698132
Converted Pitch = 40
------------------------

Now taking my calculator for say this one
we want set Pitch to 42
Pitch Angle = 50
Pitch Rad = 0.872665
Converted Pitch = 50

I can tell you that 42 * (pi / 180) should be 0.733038 not 0.872665

I checked the Math section of code and found

inline F32 mDegToRad(F32 d)
{
return((d * M_PI_F) / 180.0f);
}

inline F32 mRadToDeg(F32 r)
{
return((r * 180.0f) / M_PI_F);
}

inline F64 mDegToRad(F64 d)
{
return (d * M_PI) / 180.0;
}

inline F64 mRadToDeg(F64 r)
{
return (r * 180.0) / M_PI;
}

These seem to be correct to me. My best guess is thier is some problem with the M_PI_F or M_PI value being used.

Any idea why it is doing what it is doing or at the least some idea for how to correct for this or work around it would be greatly helpful.

Edited on Sep 30, 2008 23:34

#1
09/30/2008 (11:44 pm)
The earlier thread is here: http://www.garagegames.com/mg/forums/result.thread.php?qt=79601

@Tom Spilmen: as to your post on the other thread. Is thier any clearer documentation of exactly what it is that is occuring between information being passed from script to the C++ code. I think it is the console method class but i am not sure From all the looking around i have done all i have gathered is what the indvidual fields in the console method are but even thier i am a tad confused as to thier usage i see plenty of console methods that really do not recieve any arguments that have a min of 2 arguments and a max of 2 arguments defined for them.

I am also equaly perplexed where the data that is getting passed in is coming from. I think it has somthing to do with this: dAtof(argv[2] ) Beyond that i am clueless.

I admit i am really just starting out at learning the inner workings of the engine. So any help on figuring this out would make finding the bug alot easier.

I tried to get a cout working inside the actual RTSCamera::setPitchAngle function but for the life of me i could not get the thing working.

Hate to say it but this still might not be the right place for this thread even though i am looking for a bug. I am kinda asking for clarification on how a part of the engine works. Can't seem to win so this will just have ot do.
#2
10/01/2008 (12:13 am)
@Wesley - Do a search for mDegToRad in the source code and you'll find some code like...
ConsoleFunction( mDegToRad, F32, 2, 2, "(float degrees) Convert a measure in degrees to radians.")
{
   return(mDegToRad(dAtof(argv[1])));
}
... this is the C++ code that gets called for the mDegToRad script function. argv[1] is the string containing your number... dAtof() converts it to a float for the mDegToRad() inline function.

I would make a few temporaries there to test all the intermediate values and see what is getting into the code.
#3
10/01/2008 (12:28 pm)
Thanks that is helpful when i went tracing back through the include statements with the mMath all i managed to find was the inline functions I was sure it had a console method somewhere but being inline i was not exactly sure.

That clears up the argv[] and dAtof some. But i am still not sure what values are getting put in which index of that array.

I went hunting for the documentation on the console hopeing to see if they have some cout or print function specficly for this sort of thing i found

con::printf() I am guessing this is the right thing to use. Does it have any special methods for making it useable inside a class's methods?
#4
10/01/2008 (2:44 pm)
Argv[0] is the name of the console function called... in this case "mDegToRad".... argv[1] will be your number as a string.... argc is the count of the number of arguments passed to this function... in this case it will be 2.

I wouldn't printf anything... i'm just saying run it in the VisualStudio debugger and see what the values are.
#5
10/01/2008 (3:49 pm)
Maybe you're getting some number mixed up? I wrote this real quick:

function testStuff()
{
  %bob = mDegToRad(50.0);
  echo(%bob);
  %bob = mDegToRad(42.0);
  echo(%bob);
}

and got

0.872665
0.733038

at the console. Which seems right to me.
#6
10/02/2008 (8:55 am)
@Brian oh i am sure the numbers being entered and given out are right you can see my echo statements what are in them and the resulting output of me panning around with the RTS cammera. The odd thing is some of the numbers come out right and some do not. I am not sure exactly the circumstances that cause them to come out incorrectly yet. However After doing some testing with printf's i can tell this wierd anomaly whatever it is. Is not why the camera seems a bit jerky to me. So for the time being i am not gonna worry about this bug. At this point it would take more time to track it down than it would do me good to try and fix it.
#7
10/02/2008 (10:43 am)
@Wesley:

I'm just trying to figure out which numbers in your example are coming out wrong. Based on your code above, if there were a bug in mDegToRad, then PitchAngle would be unequal to converted pitch. There's the one case where 41 comes out as 41.0001, but that's explainable with floating point rounding/representation.
#8
10/02/2008 (2:17 pm)
@Brian sorry i guess i was not really being clear enough

we want set Pitch to 32
Pitch Angle = 41
Pitch Rad = 0.715585
Converted Pitch = 41.0001

the value for the line "we want to set Pitch to" is the value to which i am passing into the setpitchangle() function. It is what all the other values should acctualy pretty much come to equaling. "The Pitch Angle =" equal line is where i ran a getpitchangle() to see what was acctualy getting recorded in c++. Again it should be 32 not 41.
I was attempting to reproduce the error with the "Converted Pitch = " line and the "Pitch Rad =" Line. Surprisingly it came out just as inccorect as the GetPitchAngle()'s return value In the Pitch Rad Line i just ran my 32 through the MDegToRAd Function. The "Converted Pitch =" Line i ran my 32 through the MDegToRad then the MRadToDeg.

The reason i was trying this sorta test was i noticed the SetPitchangle() first converts the degree value you give it to radians using the mDegToRad function then the when you call the getPitchAngle() it converts it back calling the MRadToDeg(). I was just trying to narrow the bug down was not expecting to see any problems with either of the RadtoDeg or degtorad functions.

Now I will say the bug is happening sporadicly at best it is not happening with all the outputed data i collected. I also did some further testing and looking with the SetYaw function and oddly enough i cannot seem to get this error with it. So far it Just seems to be occuring when the SetPitchAngle() has been called.

I hope that was more clear.