Best method to manage rotation overflow > 360 degrees

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Best method to manage rotation overflow > 360 degrees

Post by robmar »

Does anyone know a better way to control rotation values?

I´m trying to find the most CPU efficient way to do this, but maybe there´s a better method.

Code: Select all

 
if ( rotate.X >= 360.f || rotate.X <= -360.f )
     rotate.X = fmodf( rotate.X,  360.f );
if ( rotate.Y >= 360.f || rotate.Y <= -360.f )
     rotate.Y = fmodf( rotate.Y,  360.f );
if ( rotate.Z >= 360.f || rotate.Z <= -360.f )
     rotate.Z = fmodf( rotate.Z,  360.f );
 
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Best method to manage rotation overflow > 360 degrees

Post by hendu »

Doing fabsf(number) >= 360 is somewhat faster here than the two comparisons. YMMV.
axeves
Posts: 12
Joined: Tue Jun 21, 2011 5:13 pm
Location: Sweden

Re: Best method to manage rotation overflow > 360 degrees

Post by axeves »

I don't know it's faster, but instead of making it go minus, change it to 1 when the rotation is over 360.0f degrees, and then continue rotating.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Best method to manage rotation overflow > 360 degrees

Post by serengeor »

axeves wrote:I don't know it's faster, but instead of making it go minus, change it to 1 when the rotation is over 360.0f degrees, and then continue rotating.
You would loose 1 degree then.
Working on game: Marrbles (Currently stopped).
shadowslair
Posts: 758
Joined: Mon Mar 31, 2008 3:32 pm
Location: Bulgaria

Re: Best method to manage rotation overflow > 360 degrees

Post by shadowslair »

axeves wrote:I don't know it's faster, but instead of making it go minus, change it to 1 when the rotation is over 360.0f degrees, and then continue rotating.
This will result in loss of precision. The initial method is ok.

EDIT: serengeor was faster
"Although we walk on the ground and step in the mud... our dreams and endeavors reach the immense skies..."
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Best method to manage rotation overflow > 360 degrees

Post by hendu »

BTW, is this for a phone, or something that doesn't have a FPU?
pera
Posts: 460
Joined: Wed May 14, 2008 1:05 pm
Location: Novi Sad, Serbia
Contact:

Re: Best method to manage rotation overflow > 360 degrees

Post by pera »

it doesn't matter, check will skip the fmodf 99% of the time. why optimize something that never happens?
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Best method to manage rotation overflow > 360 degrees

Post by robmar »

Thanks for the replies. Out of interest I thought I´d list the actual optimized asm code produced by VS 2008, which is about as compact as it could be with an FPU, and looks about as good as any handwritten ASM could be.

As fabs only used 5 lines of code compared to 7 for the <> tests, depending on FPU speed, it is probably the fastest. Not much of a difference unless there are lots of calls per second to functions like this.

Whichever method is used in C++, there is :-

Code: Select all

 
; 279  : // test one
; 280  :                                                if ( fabs( m_af3Spin.X)  >= 360.f )
 
  0072f f3 0f 10 07      movss   xmm0, DWORD PTR [edi]
  00733 f3 0f 11 45 e4   movss   DWORD PTR tv1356[ebp], xmm0
  00738 e8 00 00 00 00   call    _fabsf
  0073d 0f 2f 05 00 00
        00 00            comiss  xmm0, DWORD PTR __real@43b40000
  00744 72 1c            jb      SHORT $LN8@animateNod@3
 
; 281  :                                                        m_af3Spin.X = fmodf( m_af3Spin.X,  360.f );
 
  00746 d9 45 e4         fld     DWORD PTR tv1356[ebp]
  00749 d9 5d e4         fstp    DWORD PTR $T952717[ebp]
  0074c d9 45 e4         fld     DWORD PTR $T952717[ebp]
  0074f dd 05 00 00 00
        00               fld     QWORD PTR __real@4076800000000000
  00755 e8 00 00 00 00   call    __CIfmod
  0075a d9 5d e4         fstp    DWORD PTR tv1416[ebp]
  0075d d9 45 e4         fld     DWORD PTR tv1416[ebp]
  00760 d9 1f            fstp    DWORD PTR [edi]
$LN8@animateNod@3:
 
; 282  : // test two
; 283  :                                                if ( fabs( m_af3Spin.Y ) >= 360.f )
; 284  :                                                {
; 285  : //                                                     m_af3Spin.Y = fmodf( m_af3Spin.Y,  360.f );
; 286  :                                                        float fY = m_af3Spin.Y / 360.f;
; 287  :                                                        fY = fY -(float)(int)fY;
; 288  :                                                        fY = fY * 360.f;
; 289  :                                                }
; 290  : // test three
; 291  :                                                if ( m_af3Spin.Z >= 360.f || m_af3Spin.Z <= -360.f )
 
  00762 f3 0f 10 86 d8
        00 00 00         movss   xmm0, DWORD PTR [esi+216]
  0076a f3 0f 11 45 e4   movss   DWORD PTR tv1366[ebp], xmm0
  0076f 0f 2f 05 00 00
        00 00            comiss  xmm0, DWORD PTR __real@43b40000
  00776 73 0d            jae     SHORT $LN5@animateNod@3
  00778 f3 0f 10 0d 00
        00 00 00         movss   xmm1, DWORD PTR __real@c3b40000
  00780 0f 2f c8         comiss  xmm1, xmm0
  00783 72 20            jb      SHORT $LN6@animateNod@3
$LN5@animateNod@3:
 
 
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Best method to manage rotation overflow > 360 degrees

Post by Mel »

fmodf returns values always between 0.0 and 360.0, so you can use straight these values, and never worry about getting outside these bounds, fmodf does the check for you.
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
Post Reply