Floating Precision Errors

Started by
3 comments, last by ms291052 19 years, 6 months ago
I just finished (re)implementing skinned meshes to work with DirectX 9.0c and added the ability to control the speed at which the animations play. Here's what I'm doing to get the time I'm at in the animation: Animation.time += (timeGetTime() - LastTime) * Animation.speed; Animation.time = Animation.time - ((int)(Animation.time / (AnimationLength + 1)) * (AnimationLength + 1)); Where LastTime is a static variable saved from the last frame's timeGetTime call. This way the time goes up but then wraps (loops) around whenever AnimationLength is reached. The problem is that the precision errors are off by a LOT. After just a couple seconds (albeit a couple hundred frames per second) the animations are already quite far off and only get worse. I just changed from storing time as a double instead of a float but it's still pretty bad. How should I remedy this? Is there some better way of making the animation loop around? Thanks for any advice, ms
Advertisement
Post the types of all the variables used in those two lines.
Animation.time is a double
Animation.speed is a double
AnimationLength is a DWORD (unsigned long)
LastTime is a static DWORD (unsigned long)
I've not completely understood yet in which cases the compiler makes mathematical operations return either of the data types involved but it usually helps transforming the data types involved into the the data type of the variable getting the result. For example you don't always want an operation involving a DWORD and a double to be calculated as a DWORD so transform the DWORD into a double:

Animation.time += (double) (timeGetTime() - LastTime) * Animation.speed;´//What type is timeGetTime? If it's a float maybe you want to transform LastTime into float.Animation.time -= ((Animation.time / (double)(AnimationLength + 1)) * (double) (AnimationLength + 1));


\Jimmy H
@Jimmy - timeGetTime() returns a DWORD

I think I figured it out. I type casted everything to float (yes, float, not double) and changed the animation function to accept a DWORD parameter called CurrTime so that the timeGetTime function is called once per frame (before it was being called once per animation resulting in each animation being slightly off from each other). This seems to be working well. Thanks for your help!

This topic is closed to new replies.

Advertisement