Float Precision Issue?

Started by
12 comments, last by DJTN 11 years, 4 months ago
Previously I posted about a rotation issue with my camera. I thought I had solved the problem by adding frame time to the camera’s movement calculation but unfortunately this only masked the real problem, which I think is a precision issue with floats. I noticed this when I rotate not only the camera but other objects as well.

I striped my render code down to the basics in an effort to identify the issue:

[source lang="cpp"]Camera Calculations


CameraTarget.X = CameraPosition.X + Sin(vRadians)
CameraTarget.Y = CameraPosition.Y + (Cos(vRadians) * Cos(hRadians))
CameraTarget.Z = CameraPosition.Z + (Cos(vRadians) * Sin(hRadians))

CameraUp.X = 0;
CameraUp.Y = 1;
CameraUp.Z = 0;




Update View Matrix:


D3DXMatrixLookAtLH(View_Matrix, CameraPosition, CameraTarget, CameraUp);
[/source]

To make things as simple as possible I’ll add 0.002 to the hRadians float each frame. This makes the camera rotate to the left but when I slow the frame rate down I can clearly see the camera is not turning the same amount each frame. This also happens when I calculate rotations for the world matrix before rendering 3D objects too. When rendering at 60 FPS and adding an easing calculation to movements it’s not as noticeable for moving forward, backwards, left and right but when rotating, it is very noticeable. Even with the easing code added you can tell the rotations are accelerating and decelerating, sometimes jerky.


I’ve tried to truncate the calculations for the CameraTarget and CameraPosition to regain some precision but to no avail, the issue still persists.


Is this a precision issue? And if so, how can I get more precision for rotations? If it’s a not a precision problem then what is it?
Advertisement
What are those trig functions you're using - do they map to sinf/cosf?

What is the absolute value of hRadians? Do you reset it back to the zero to 2Pi range or to the -PI to PI range after it leaves it? (if you let a float get too far from zero the precision drops significantly).
How exactly do you increase hRadians? If you add a constant amount every frame, the rotation will of course accelerate/decelerate depending on the frame rate.

What are those trig functions you're using - do they map to sinf/cosf?

What is the absolute value of hRadians? Do you reset it back to the zero to 2Pi range or to the -PI to PI range after it leaves it? (if you let a float get too far from zero the precision drops significantly).


Yes they are sinf and cosf, the code is pseudo code from memory, I'm not at my computer. hRadians starts out at 0 and I'm adding 0.002 to it every frame in an attempt to find the problem. It accumulative I do not set it back to zero. When I get home I'll check to see how I can implement locking it down to as close to zero as I can get and see if that helps.

How exactly do you increase hRadians? If you add a constant amount every frame, the rotation will of course accelerate/decelerate depending on the frame rate.


I've removed the easing (dampening) code as well as the addition of the timestamp each frame so that I can get to the bottom of the issue. Basically my render loop is just adding 0.002 to hRadians and creating the view matrix. This slowly turns the camera. When I slow the frame rate down in the debugger I can clearly see the ratation is different each frame. Some frames the rotation amount is more obvious than others.

What is the absolute value of hRadians?


hRadians is set to zero and 0.002 is added each frame. 6.0 in hRadians is a complete circle. It doesn't appear to be related with the inaccuracies coming from the distance from zero.
How about D3DXToRadian() and use 0-360 values?

How about D3DXToRadian() and use 0-360 values?


A lot of overhead making that change. I'll need to figure out a way I cant test it before going all the way.
6.0 in hRadians is a complete circle
Shouldn't it be 6.283....?
Another thing that occurs to me is that if your CameraPosition vector was a long way from the origin, then there'd be a lot of error when you create your CameraTarget vector by adding the unit vector, which could explain some oddness. Could you check that's not the case?

This topic is closed to new replies.

Advertisement