Ball should remain in a circle

D

Deleted member 103

Guest
Hi,

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

  • sensor-test.jpg
    sensor-test.jpg
    10.1 KB · Views: 271
Last edited by a moderator:

eps

Expert
Licensed User
Longtime User
I tried running the App you posted but it didn't seem to do too much...

What is it supposed to do? Is it supposed to be a ball game where you lay the device flat to balance it and then tilt the device to move the ball in that direction?

At a rough guess your maths is out.. Sorry to be obvious! If it were me I would sketch it out on a piece of paper (old school I know!) and then calculate what you think the nos. are... and then "move" the ball... and recalculate.. I'm sure that the penny will drop as you work with the centre and then the limits (the circle)..
 
Upvote 0
D

Deleted member 103

Guest
@eps
I think my math skills are a bit rusty.:eek:

I'm trying to run the ball inside the circle, but apparently can not do so well.
The red circle should be representing an eye and the black circle to the pupil.
So when I move the phone the pupil must not leave the red circle.

@timo
stò provando a far girare la palla dentro il cerchio, ma quanto pare non ci riesco tanto bene.
Il cerchio rosso dovrebbe rapresentare un occhio e il cerchio nero la pupilla.
Quindi quando muovo il cellulare la pupilla non deve uscire dal cerchio rosso.
 
Upvote 0
D

Deleted member 103

Guest
Problem solved, now works.:)

B4X:
Sub Process_Globals
   Dim Sensor As PhoneSensors
End Sub

Sub Globals
    Dim can As Canvas
    Dim ballsize As Float
    Dim ballx,bally 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)

    ballsize=20dip
    ballx=pnlAuge.Width/2
   bally=pnlAuge.Height/2
   maxRadius=(pnlAuge.Width - ballsize) / 2 -10dip
    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,grad,x,y As Float
   
   bally=bally - Values(1)
   ballx=ballx - Values(2)
   r1= Sqrt(Power(ballx - mitteX,2) + Power(bally - mitteY,2))
   
    If r1 > maxRadius Then
        x = ballx - mitteX
      y = bally - mitteY
        grad = ATan(x / y)

        If bally < mitteY Then
            bally = mitteY - maxRadius * Cos(grad)
            ballx = mitteX - maxRadius * Sin(grad)
        Else
            bally = mitteY + maxRadius * Cos(grad)
            ballx = mitteX + maxRadius * Sin(grad)
        End If
    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("")
   Log("ballx: " & (ballx - mitteX) & " ;bally=" & (bally - mitteY))
   Log("maxRadius=" & maxRadius & "; r1=" & r1 & "; grad=" & grad)
   
End Sub

Ciao,
Filippo
 
Upvote 0
Top