-
May 17th, 2009, 02:31 PM
#1
Help with nan
Hi all
Yet another question from me and hope some one shoots the answer immediately.. very simple though
I have been tryin to do a rigid body simulation ... things work fine until i hit a nan ..
What I am trying to do is retrieve a angle from a quaternion for which i wrote this following function...
Code:
void get_AxisAngle_from_Quat(float &angle, Xlib::vec3d &axis)
{
float squareLength = get4d_X()*get4d_X() + get4d_Y()*get4d_Y() + get4d_Z()*get4d_Z();
if (squareLength>C_EPSILON)
{
angle = 2.0f * (float) acos(get4d_Z());
//assert (!XlibMath::is__nan(angle));
float inverseLength ;
inverseLength = 1.0f / (float) pow(squareLength, 0.5f) ;
axis.x = get4d_X() * inverseLength;
axis.y = get4d_Y() * inverseLength;
axis.z = get4d_Z() * inverseLength;
}
else
{
angle = 0.0f;
axis.x = 1.0f;
axis.y = 0.0f;
axis.z = 0.0f;
}
}
This is being refered in the code by this way
Code:
Xlib::vec3d axis(1,0,0);
float angle=0.0f;
RBody[0].q.get_AxisAngle_from_Quat(angle,axis);
Xlib::DevMsg("Quat ",RBody[0].q);
Xlib::DevMsg("Angle ", angle);
but This hits a nan and everytime it hits nan my object disappears from the screen.... (DevMsg is a overload for cout i use) When i try printing out the values at the point where i hit the nan this shows this
Code:
DevMsg: Quat 1.442195 0.698514 1.700220 0.403262
Angle nan
I read online but it states nan is got by dividing by 0 .. but clearly i am not using any divisions at al...
can someone clarify me on where i went wrong ????
Cheers and regards
-
May 17th, 2009, 06:52 PM
#2
Re: Help with nan
You don't state where you are getting the NAN error, but your code indicates that it may be in the call to acos().
Note that the domain for arccosine is -1 to +1 so you need to determine why you are asking for the arccosine of a number outside of its domain. In other words, assert on the NAN but then back up from there to determine the cause.
-
May 17th, 2009, 11:02 PM
#3
Re: Help with nan
NaN isn't just division by 0---it may be used for any calculation which has an undefined result. Trying to take the acos of a vector of length 0 would do it.
-
May 18th, 2009, 03:35 AM
#4
Re: Help with nan
acos of 0 is well defined to be pi/2
-
May 18th, 2009, 04:44 AM
#5
Re: Help with nan
angle = 2.0f * (float) acos(get4d_Z());
//assert (!XlibMath::is__nan(angle));
You show the values for Xlib::vec3d axis but not for get4d_Z(). What is get4d_Z()? If it's not the Z-coordinate of a unit vector then that could be your problem.
-
May 18th, 2009, 08:17 AM
#6
Re: Help with nan
Originally Posted by NMTop40
acos of 0 is well defined to be pi/2
Oh, right. I was thinking of atan2.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|