About Monkey 2 › Forums › Monkey 2 Programming Help › Float and Double image rotation problem
This topic contains 2 replies, has 2 voices, and was last updated by 
 Pakz 2 years, 4 months ago.
- 
		AuthorPosts
 - 
		
			
				
December 11, 2016 at 11:53 pm #5732
When I use doubles for the rotation variable and the rotation modification variable the behaviour is like I want it to be, but when I change them to float the behaviour seems to be different. In one direction it does not rotate at all.
What can be the problem?
Monkey1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class itemField can:CanvasField image:ImageField rot:Float 'change to double and it worksField rotspd:Float ',, ,,Method New()rot=Rnd(TwoPi)rotspd=Rnd(-0.2,0.2)image = New Image(64,64)can = New Canvas(image)image.Handle=New Vec2f( .5,.5 )makeimage()End MethodMethod makeimage()can.Color = Color.Nonecan.BlendMode=BlendMode.Opaquecan.DrawRect(0,0,64,64)Local w:Int=Rnd(4,30)Local h:Int=Rnd(4,30)For Local i:=0 Until 10Local a:Float=Rnd()can.Color = New Color(a,a,a)If Rnd() <0.1 ThenLocal a:Int=Rnd()*4Select aCase 0can.Color = New Color(Rnd(.2,1),0,0)Case 1can.Color = New Color(0,Rnd(.2,1),0)Case 2can.Color = New Color(0,0,Rnd(.2,1))Case 3can.Color = New Color(Rnd(.2,1),Rnd(.2,1),0)End SelectEnd Ifcan.DrawTriangle( New Vec2f(32+Rnd(-w,w),32+Rnd(-h,h)),New Vec2f(32+Rnd(-w,w),32+Rnd(-h,h)),New Vec2f(32+Rnd(-w,w),32+Rnd(-h,h)))Nextcan.Flush()End MethodEnd ClassGlobal myitem:List<item> = New List<item>Class MyWindow Extends WindowMethod New()For Local y:=0 To 3For Local x:=0 To 3myitem.AddLast(New item())NextNextClearColor = Color.BlackEnd MethodMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender() ' Activate this methodLocal x:Int=0Local y:Int=0canvas.Color = Color.WhiteFor Local i:=Eachin myitemcanvas.DrawImage(i.image,x*120+96,y*96+32+32,i.rot)i.rot+=i.rotspdIf i.rot<0 Then i.rot=TwoPiIf i.rot>TwoPi Then i.rot=0x+=1If x>3 Then y+=1;x=0Next'if press escape then endIf Keyboard.KeyReleased(Key.Escape) Then App.Terminate()End MethodEnd ClassFunction Main()New AppInstanceNew MyWindowApp.Run()End FunctionDecember 12, 2016 at 12:09 am #5735This is because TwoPi is a Double.
On line 71, if rot < 0, rot is set to TwoPi (implicit cast from Double to Float) but on line 72 you then compare a Float with a Double. It looks like they are being implicitly cast to Doubles before being compared and so rot is rounded to be slightly larger than TwoPi… and gets set back to zero.
Try changing line 72 to :
If i.rot > Cast<Float>(TwoPi) Then i.rot=0
Or define your own variable TwoPiF (or whatever) as a Float if you don’t like using an explicit cast.
December 12, 2016 at 12:17 am #5736Thank you ! That solved it.
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.