German Ball in ein Kreis

D

Deleted member 103

Guest
Hallo,

ich weis es sollte eigendlich einfach sein, aber ich bekomme es einfach nicht in!
Ich möchte das der schwarze Punkt nur innerhalb vom rote Kreis bleibt, bzw. den rote Kreis nicht übersteigt.
Weißt jemanden wie das funktioniert?

B4X:
'AccelBall
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 pnlAuge As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
   pnlAuge.Initialize("")
   pnlAuge.Color=Colors.White
   Activity.AddView(pnlAuge, 100dip,100dip,100dip,100dip)

   Sensor.Initialize(Sensor.TYPE_ORIENTATION)
   can.Initialize(pnlAuge)

    ballx=pnlAuge.Width/2 : bally=pnlAuge.Height/2
    ballsize=20dip : targety=bally : targetx=ballx

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)
    targety=targety-Values(1)
    If targety<ballsize Then targety=ballsize
    If targety>(pnlAuge.Height-ballsize) Then targety=(pnlAuge.Height-ballsize)

   targetx=targetx-Values(2)
    If targetx<ballsize Then targetx=ballsize
    If targetx>(pnlAuge.Width-ballsize) Then targetx=(pnlAuge.Width-ballsize)

   Log("Sensor: " & Values(0) & " ;" & Values(1) & " ;" & Values(2))

    can.DrawColor(Colors.White)
    ballx=ballx+(targetx-ballx)/2.0
    bally=bally+(targety-bally)/2.0
    can.DrawCircle(pnlAuge.Width/2,pnlAuge.Height/2,pnlAuge.Width/2,Colors.Red,False,2.0)
    can.DrawCircle(ballx,bally,ballsize,Colors.Black,True,0.0)
    pnlAuge.Invalidate
   Log(ballx & ";" & bally)
End Sub

Ciao,
Filippo
 

Attachments

  • sensor-test.jpg
    10.1 KB · Views: 216

klaus

Expert
Licensed User
Longtime User
Hallo Filippo.
Versuche diesen Code:
B4X:
    can.DrawColor(Colors.White)
    ballx=ballx+(targetx-ballx)/2.0
    bally=bally+(targety-bally)/2.0
    r = Sqrt((ballx - pnlAuge.Width/2) * (ballx - pnlAuge.Width/2) + (bally - pnlAuge.Width/2) * (bally - pnlAuge.Width/2))
    If r <= pnlAuge.Width/2 - ballsize Then
        can.DrawCircle(pnlAuge.Width/2,pnlAuge.Height/2,pnlAuge.Width/2,Colors.Red,False,2.0)
        can.DrawCircle(ballx,bally,ballsize,Colors.Black,True,0.0)
        pnlAuge.Invalidate
    End If
    Log(ballx & ";" & bally)
Beste Grüsse.
 
D

Deleted member 103

Guest
Hallo Klaus,

vielen Dank für deine Start-Hilfe, aber es funktioniert immer noch nicht wie es sein soll.
Ich habe jetzt auch noch ein paar Zeilen geändert, aber auch ohne Erfolg.
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, 100dip,100dip,100dip,100dip)

   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/2) - (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 As Float
   
    targety=targety-Values(1)
   r1= Sqrt(Power(Abs(targetx)-mitteX,2) + Power(Abs(targety)-mitteY,2))
   If r1 <= maxRadius Then
       ballx=ballx+(targetx-ballx)/2.0
       bally=bally+(targety-bally)/2.0   
   Else
      targety=targety+Values(1)
   End If
   
   targetx=targetx-Values(2)
   r2= Sqrt(Power(Abs(targetx)-mitteX,2) + Power(Abs(targety)-mitteY,2))
   If r2 <= maxRadius Then
       ballx=ballx+(targetx-ballx)/2.0
       bally=bally+(targety-bally)/2.0
   Else
      targetx=targetx+Values(2)
   End If
   
   can.DrawColor(Colors.White)
   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 & " ;targety=" & targety)
   Log("maxRadius=" & maxRadius & "; r1=" & r1 & "; r2=" & r2)
   
End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…