If I declare the variables x,y,z, and w at the top of the function, the results are correct. If I leave them where they are, the results are incorrect rotations. If I comment out the KeyHit() command or add a Delay(1) command, the results are correct. Any printing or debugging I do makes the results correct, and masks the problem.
I wish I could give a better example, but any changes I make hide the problem. Any idea why this could be?
I wish I could give a better example, but any changes I make hide the problem. Any idea why this could be?
Method Rotation:TQuat() Rem Local sc:TVec3=Scale() Local iv:TVec3=Vec3(ix,iy,iz).Scale(1/sc.x) Local jv:TVec3=Vec3(jx,jy,jz).Scale(1/sc.y) Local kv:TVec3=Vec3(kx,ky,kz).Scale(1/sc.z) Local t#=iv.x+jv.y+kv.z+1 If t>0 t=Sqr(t)*2 x=(kv.y-jv.z)/t y=(iv.z-kv.x)/t z=(jv.x-iv.y)/t w=t/4 Else If iv.x>jv.y And iv.x>kv.z t=Sqr(iv.x-jv.y-kv.z+1)*2.0 x=t/4 y=(jv.x+iv.y)/t z=(iv.z+kv.x)/t w=(kv.y-jv.z)/t Else If jv.y>kv.z t=Sqr(jv.y-kv.z-iv.x+1)*2.0 x=(jv.x+iv.y)/t y=t/4 z=(kv.y+jv.z)/t w=(iv.z-kv.x)/t Else t=Sqr(kv.z-jv.y-iv.x+1)*2.0 x=(iv.z+kv.x)/t y=(kv.y+jv.z)/t z=t/4 w=(jv.x-iv.y)/t EndIf Return Quat( x,y,z,w ) EndRem Local fourWSquaredMinus1:Float = +ix+jy+kz Local fourXSquaredMinus1:Float = +ix-jy-kz Local fourYSquaredMinus1:Float = +jy-ix-kz Local fourZSquaredMinus1:Float = +kz-ix-jy Local biggestIndex=0 Local fourBiggestSquaredMinus1:Float=fourWSquaredMinus1 If fourXSquaredMinus1 > fourBiggestSquaredMinus1 fourBiggestSquaredMinus1 = fourXSquaredMinus1 biggestIndex=1 EndIf If fourYSquaredMinus1 > fourBiggestSquaredMinus1 fourBiggestSquaredMinus1 = fourYSquaredMinus1 biggestIndex=2 EndIf If fourZSquaredMinus1 > fourBiggestSquaredMinus1 fourBiggestSquaredMinus1 = fourZSquaredMinus1 biggestIndex=3 EndIf Local biggestValue:Float=Sqr(fourBiggestSquaredMinus1+1.0)*0.5 Local mult:Float=0.25/biggestValue Local x#,y#,z#,w# Select BiggestIndex Case 0 w#=BiggestValue x#=(jz-ky)*mult y#=-(kx-iz)*mult z#=-(iy-jx)*mult Case 1 x#=-BiggestValue w#=-( jz - ky )*mult y#=( iy + jx )*mult z#=( kx + iz )*mult Case 2 y#=BiggestValue w#=-(kx-iz)*mult x#=-(iy+jx)*mult z#=(jz+ky)*mult Case 3 z#=-BiggestValue w#=(iy-jx)*mult x#=(kx+iz)*mult y#=-(jz+ky)*mult EndSelect 'If KeyHit(KEY_L) Notify BiggestIndex Return quat(-x,y,z,w) Rem 'testing code: Global mode=0 If KeyHit(KEY_N) mode:+1 'Notify mode EndIf If KeyHit(KEY_L) Notify mode Select mode Case 0 Return quat(x,y,z,w) Case 1 Return quat(x,y,z,-w) Case 2 Return quat(x,-y,z,w) Case 3 Return quat(x,y,-z,w) Case 4 Return quat(-x,-y,z,w) Case 5 Return quat(x,-y,-z,w) Case 6 Return quat(-x,y,z,w) EndSelect EndRem EndMethod