D
Deleted member 103
Guest
Hi,
the ball should stay in a circle but I can not.
What is wrong with my code?
Thank you in advance
Filippo
the ball should stay in a circle but I can not.
What is wrong with my code?
B4X:
Sub Process_Globals
Dim Sensor As PhoneSensors
End Sub
Sub Globals
Dim can As Canvas
Dim ballx,bally,ballsize As Float
Dim targetx,targety As Float
Dim maxRadius As Float
Dim mitteX,mitteY As Int
Dim pnlAuge As Panel
End Sub
Sub Activity_Create(FirstTime As Boolean)
pnlAuge.Initialize("")
pnlAuge.Color=Colors.White
Activity.AddView(pnlAuge, 50dip,10dip,200dip,200dip)
Sensor.Initialize(Sensor.TYPE_ORIENTATION)
can.Initialize(pnlAuge)
ballx=pnlAuge.Width/2 : bally=pnlAuge.Height/2
ballsize=20dip : targety=bally : targetx=ballx
maxRadius=(pnlAuge.Width - ballsize) / 2
mitteX=pnlAuge.Width/2 : mitteY=pnlAuge.Height/2
End Sub
Sub Activity_Resume
Sensor.StartListening("Sensor")
End Sub
Sub Activity_Pause (UserClosed As Boolean)
Sensor.StopListening
End Sub
Sub Sensor_SensorChanged(Values() As Float)
Dim r1,r2,grad,x,y As Float
targety=targety - Values(1)
targetx=targetx - Values(2)
r1= Sqrt(Power(targetx - mitteX,2) + Power(targety - mitteY,2))
If r1 > maxRadius Then
x = targetx - mitteX : y = targety - mitteY
If x > y Then
grad = ATan(y / x)
Else
grad = ATan(x / y)
End If
If targety < mitteY Then
targety = mitteY - maxRadius * Cos(grad)
Else
targety = mitteY + maxRadius * Cos(grad)
End If
If targetx < mitteX Then
targetx = mitteX - maxRadius * Cos(grad)
Else
targetx = mitteX + maxRadius * Cos(grad)
End If
End If
can.DrawColor(Colors.White)
ballx=ballx+(targetx-ballx)/2.0
bally=bally+(targety-bally)/2.0
can.DrawCircle(mitteX,mitteY,mitteX,Colors.Red,False,2.0)
can.DrawCircle(ballx,bally,ballsize,Colors.Black,True,0.0)
pnlAuge.Invalidate
Log("targetx: " & (targetx - mitteX) & " ;targety=" & (targety - mitteY))
Log("maxRadius=" & maxRadius & "; r1=" & r1 & "; grad=" & grad)
End Sub
Thank you in advance
Filippo
Attachments
Last edited by a moderator: