Trouble getting x and y position, on a canvas

SlavaVB

Member
Licensed User
Longtime User
Hi, i wrote an app recently that tells you the x and y of the place where you touch the screen, however it doesn't work if there's a canvas on top of it. Any ideas or suggestions?

Thanks!
 

thedesolatesoul

Expert
Licensed User
Longtime User
Compiling code. 0.03
Generating R file. 0.00
Compiling generated Java code. 0.91
Convert byte code - optimized dex. 0.52
Packaging files. 0.47
Signing package file (debug key). Error


Are you able to manipulate the large ball by touching the screen?
It slows down quite a bit. But it looked like the balls were moving away from the mouse.

Maybe Erel can tell why it is not compiling. I have no clue!
 
Upvote 0

SlavaVB

Member
Licensed User
Longtime User
Could you post the full code that worked on your machine.. i want to try it on my compiler. if it doesn't work, ill have to buy the full version. Are you able to move the large circle with your finger?
 
Upvote 0

SlavaVB

Member
Licensed User
Longtime User
The small balls shouldn't be affected by the cursor, only the large ball, which also repels the smaller balls.
 
Upvote 0

SlavaVB

Member
Licensed User
Longtime User
Try this code... the balls don't slow down in it...


B4X:
Sub Process_Globals
   Dim Timer1 As Timer
   
   Dim xx(100) As Double
   Dim yy(100) As Double
   Dim vx(100) As Double
   Dim vy(100) As Double
   Dim ax(100) As Double
   Dim ay(100) As Double
   Dim r(100) As Double
   Dim dt As Double
   
   Dim w As Double
   Dim h As Double
   Dim diffx As Double
   Dim diffy As Double
   Dim dist1 As Double
   Dim dist2 As Double
   
   Dim xval As Double
   Dim yval As Double

   
End Sub

Sub Globals
   Dim Canvas1 As Canvas
   

   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   canvas1.Initialize(Activity)
   
   
   
   
   
   Globe
End Sub


Sub Globe
   



   
   w = Canvas1.Bitmap.Width 
   h = Canvas1.Bitmap.Height
   
   xx(0) = w/2
   yy(0) = h/2
   vx(0) = Rnd(-5,5)
   vy(0) = Rnd(-5,5)
   ax(0) = 0
   ay(0) = 0
   r(0) = 100
   
   For i = 1 To 99
      xx(i) = Rnd(0,w)
      yy(i) = Rnd(0,h)
      vx(i) = Rnd(-20,20)   
      vy(i) = Rnd(-20,20)
      ax(0) = 0
      ay(0) = 0
      r(i) = Rnd(10,15)
   Next 
   
   dt = .1
   
   
   'For a = 1 To 1000000
         
   'Next

  
     Timer1.Initialize("MainEvent", 10)
   Timer1.Enabled = True

  
  
End Sub

Sub MainEvent_Tick
      
      For i = 0 To 99
      
         For j = 0 To 99
            ax(j) = 0.0;
            ay(j) = 0.0;
         Next
            
         For j = 0 To 99
         
            If i <> j Then
               diffx = xx(i) - xx(j)
               diffy = yy(i) - yy(j)
               dist1 = Sqrt(Power(diffx,2)+Power(diffy,2))
               dist2 = r(i) + r(j)
               
               If dist1 < dist2 Then
                  deltaDistance = (dist2 - dist1)
               
                  xval = (((deltaDistance * 10.0) / dist2) * diffx)
                  yval = (((deltaDistance * 10.0) / dist2) * diffy)
                  
                  ax(i) = ax(i) + xval
                       ay(i) = ay(i) + yval
 
               End If
            
            End If
         Next
      
         vx(i) = (vx(i) + (ax(i) * dt))
         vy(i) = (vy(i) + (ay(i) * dt))
           xx(i) = xx(i) + (vx(i) * dt)
         yy(i) = yy(i) + (vy(i) * dt)
       
         If xx(i)-r(i) < 0 Then
            vx(i) = -vx(i)
            xx(i) = 0 + r(i)
         End If
         If yy(i)-r(i) < 0 Then
            vy(i) = -vy(i)
            yy(i) = 0 + r(i)
         End If
         If xx(i)+r(i) > w Then
            vx(i) = -vx(i)
            xx(i) = w - r(i)
         End If
         If yy(i)+r(i) > h Then
            vy(i) = -vy(i)
            yy(i) = h - r(i)
         End If
         
         
      Next 
      
        Canvas1.DrawColor(Colors.Black)
      
      Canvas1.DrawCircle(xx(0),yy(0),r(0),Colors.Red,True,5)
      For i = 1 To 99
         Canvas1.DrawCircle(xx(i),yy(i),r(i),Colors.Green,True,5)
      Next 
        Activity.Invalidate
      DoEvents 

End Sub
Sub Activity_Touch (Action As Int, X As Float, Y As Float)
   xx(0) = x
   yy(0) = y
End Sub
 
Upvote 0

SlavaVB

Member
Licensed User
Longtime User
I just reinstalled it, and it works great! thanks for your help... btw i also got a full version of the app, but its not recognizing my email address for some reason.
 
Upvote 0

SlavaVB

Member
Licensed User
Longtime User
This code works fine but it is still too slow.
It is quite smooth with 9 balls, but not will 99 balls. Maybe you are doing too much collision detection.
But then again, I am testing on the emulator so it could be faster on a real device.


It runs in realtime on an actual device... try the following code... this is my final version of it....


B4X:
Sub Process_Globals
   Dim Timer1 As Timer
   
   Dim xx(100) As Double
   Dim yy(100) As Double
   Dim vx(100) As Double
   Dim vy(100) As Double
   Dim ax(100) As Double
   Dim ay(100) As Double
   Dim r(100) As Double
   Dim dt As Double
   
   Dim w As Double
   Dim h As Double
   Dim diffx As Double
   Dim diffy As Double
   Dim dist1 As Double
   Dim dist2 As Double
   
   Dim xval As Double
   Dim yval As Double

   
End Sub

Sub Globals
   Dim Canvas1 As Canvas
   

   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   canvas1.Initialize(Activity)
   
   
   
   
   
   Globe
End Sub


Sub Globe
   



   
   w = Canvas1.Bitmap.Width 
   h = Canvas1.Bitmap.Height
   
   xx(0) = w/2
   yy(0) = h/2
   vx(0) = Rnd(-5,5)
   vy(0) = Rnd(-5,5)
   ax(0) = 0
   ay(0) = 0
   r(0) = 100
   
   For i = 1 To 99
      xx(i) = Rnd(0,w)
      yy(i) = Rnd(0,h)
      vx(i) = Rnd(-50,50)   
      vy(i) = Rnd(-50,50)
      ax(0) = 0
      ay(0) = 0
      r(i) = Rnd(10,15)
   Next 
   
   dt = .1
   
     
     Timer1.Initialize("MainEvent", 10)
   Timer1.Enabled = True

  
  
End Sub

Sub MainEvent_Tick
      
      For i = 0 To 99
      
         For j = 0 To 99
            ax(j) = 0.0;
            ay(j) = 0.0;
         Next
            
         For j = 0 To 99
         
            If i <> j Then
               diffx = xx(i) - xx(j)
               diffy = yy(i) - yy(j)
               dist1 = Sqrt(Power(diffx,2)+Power(diffy,2))
               dist2 = r(i) + r(j)
               
               If dist1 < dist2 Then
                  deltaDistance = (dist2 - dist1)
               
                  xval = (((deltaDistance * 10.0) / dist2) * diffx)
                  yval = (((deltaDistance * 10.0) / dist2) * diffy)
                  
                  ax(i) = ax(i) + xval
                       ay(i) = ay(i) + yval
 
               End If
            
            End If
         Next
      
         vx(i) = (vx(i) + (ax(i) * dt)) * .99
         vy(i) = (vy(i) + (ay(i) * dt)) * .99
           xx(i) = xx(i) + (vx(i) * dt)
         yy(i) = yy(i) + (vy(i) * dt)
       
         If xx(i)-r(i) < 0 Then
            vx(i) = -vx(i)
            xx(i) = 0 + r(i)
         End If
         If yy(i)-r(i) < 0 Then
            vy(i) = -vy(i)
            yy(i) = 0 + r(i)
         End If
         If xx(i)+r(i) > w Then
            vx(i) = -vx(i)
            xx(i) = w - r(i)
         End If
         If yy(i)+r(i) > h Then
            vy(i) = -vy(i)
            yy(i) = h - r(i)
         End If
         
         
      Next 
      
        Canvas1.DrawColor(Colors.Black)
      
      Canvas1.DrawCircle(xx(0),yy(0),r(0),Colors.Red,True,5)
      For i = 1 To 99
         Canvas1.DrawCircle(xx(i),yy(i),r(i),Colors.Green,True,5)
      Next 
        Activity.Invalidate
      DoEvents 

End Sub
Sub Activity_Touch (Action As Int, X As Float, Y As Float)
   For i = 1 To 99
      vx(i) = Rnd(-50,50)   
      vy(i) = Rnd(-50,50)
   Next 
End Sub



You can make it run faster if you change the dt variable to a higher value ie. 1.000 ... you can also decrease the timer interval.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
It runs in realtime on an actual device... try the following code... this is my final version of it....
Is slightly laggy on my Galaxy S but I'm sure it can be tweaked.
Not sure what happens when I touch the screen, things go faster but Im not sure what happens.
 
Upvote 0

SlavaVB

Member
Licensed User
Longtime User
I changed the code, try the following code out... now the big circle follows your finger without going crazy...

B4X:
Sub Process_Globals
   Dim Timer1 As Timer
   
   Dim xx(100) As Double
   Dim yy(100) As Double
   Dim vx(100) As Double
   Dim vy(100) As Double
   Dim ax(100) As Double
   Dim ay(100) As Double
   Dim r(100) As Double
   Dim dt As Double
   
   Dim w As Double
   Dim h As Double
   Dim diffx As Double
   Dim diffy As Double
   Dim dist1 As Double
   Dim dist2 As Double
   
   Dim xval As Double
   Dim yval As Double

   
End Sub

Sub Globals
   Dim Canvas1 As Canvas
   

   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   canvas1.Initialize(Activity)
   
   
   
   
   
   Globe
End Sub


Sub Globe
   



   
   w = Canvas1.Bitmap.Width 
   h = Canvas1.Bitmap.Height
   
   xx(0) = w/2
   yy(0) = h/2
   vx(0) = Rnd(-5,5)
   vy(0) = Rnd(-5,5)
   ax(0) = 0
   ay(0) = 0
   r(0) = 100
   
   For i = 1 To 99
      xx(i) = Rnd(0,w)
      yy(i) = Rnd(0,h)
      vx(i) = Rnd(-50,50)   
      vy(i) = Rnd(-50,50)
      ax(0) = 0
      ay(0) = 0
      r(i) = Rnd(10,15)
   Next 
   
   dt = .1
   
     
     Timer1.Initialize("MainEvent", 10)
   Timer1.Enabled = True

  
  
End Sub

Sub MainEvent_Tick
      
      For i = 0 To 99
      
         For j = 0 To 99
            ax(j) = 0.0;
            ay(j) = 0.0;
         Next
            
         For j = 0 To 99
         
            If i <> j Then
               diffx = xx(i) - xx(j)
               diffy = yy(i) - yy(j)
               dist1 = Sqrt(Power(diffx,2)+Power(diffy,2))
               dist2 = r(i) + r(j)
               
               If dist1 < dist2 Then
                  deltaDistance = (dist2 - dist1)
               
                  xval = (((deltaDistance * 10.0) / dist2) * diffx)
                  yval = (((deltaDistance * 10.0) / dist2) * diffy)
                  
                  ax(i) = ax(i) + xval
                       ay(i) = ay(i) + yval
 
               End If
            
            End If
         Next
      
         vx(i) = (vx(i) + (ax(i) * dt)) * .99
         vy(i) = (vy(i) + (ay(i) * dt)) * .99
           xx(i) = xx(i) + (vx(i) * dt)
         yy(i) = yy(i) + (vy(i) * dt)
       
         If xx(i)-r(i) < 0 Then
            vx(i) = -vx(i)
            xx(i) = 0 + r(i)
         End If
         If yy(i)-r(i) < 0 Then
            vy(i) = -vy(i)
            yy(i) = 0 + r(i)
         End If
         If xx(i)+r(i) > w Then
            vx(i) = -vx(i)
            xx(i) = w - r(i)
         End If
         If yy(i)+r(i) > h Then
            vy(i) = -vy(i)
            yy(i) = h - r(i)
         End If
         
         
      Next 
      
        Canvas1.DrawColor(Colors.Black)
      
      Canvas1.DrawCircle(xx(0),yy(0),r(0),Colors.Red,True,5)
      For i = 1 To 99
         Canvas1.DrawCircle(xx(i),yy(i),r(i),Colors.Green,True,5)
      Next 
        Activity.Invalidate
      DoEvents 

End Sub
Sub Activity_Touch (Action As Int, X As Float, Y As Float)
   xx(0) = x
   yy(0) = y
   vx(0) = 0
   vy(0) = 0
   
   'For i = 1 To 99
   '   vx(i) = Rnd(-50,50)   
   '   vy(i) = Rnd(-50,50)
   'Next 
End Sub
 
Last edited:
Upvote 0

SlavaVB

Member
Licensed User
Longtime User
One more thing, do you by any chance know how to get readings from the accelerometer, i want to add gravitry to my program ... so when the user tilts it, the balls fall down in another direction. Thanks.
 
Upvote 0

SlavaVB

Member
Licensed User
Longtime User
Compiling code. Error
Error parsing program.
Error description: Unknown type: phoneaccelerometer
Are you missing a library reference?
Occurred on line: 3



Dim Accelerometer As PhoneAccelerometer




I added the path to the libraries also...
C:\Program Files (x86)\Anywhere Software\Basic4android\Libraries


still no luck.


p.s. phone.jar and phone.xml are inside of the directory
 
Last edited:
Upvote 0

SlavaVB

Member
Licensed User
Longtime User
For some reason it only scans the sensor in the beginning but it doesn't update the values after the first reading...


B4X:
Sub Process_Globals

   
   Dim Accelerometer As PhoneAccelerometer
   Dim AccX As Long
   Dim AccY As Long


   Dim Timer1 As Timer
   
   Dim xx(100) As Double
   Dim yy(100) As Double
   Dim vx(100) As Double
   Dim vy(100) As Double
   Dim ax(100) As Double
   Dim ay(100) As Double
   Dim r(100) As Double
   Dim dt As Double
   
   Dim w As Double
   Dim h As Double
   Dim diffx As Double
   Dim diffy As Double
   Dim dist1 As Double
   Dim dist2 As Double
   
   Dim xval As Double
   Dim yval As Double

   
End Sub

Sub Globals
   Dim Canvas1 As Canvas
   

   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
   
   
   canvas1.Initialize(Activity)
   
   Globe
End Sub


Sub Globe
   



   
   w = Canvas1.Bitmap.Width 
   h = Canvas1.Bitmap.Height
   
   xx(0) = w/2
   yy(0) = h/2
   vx(0) = Rnd(-5,5)
   vy(0) = Rnd(-5,5)
   ax(0) = 0
   ay(0) = 0
   r(0) = 100
   
   For i = 1 To 99
      xx(i) = Rnd(0,w)
      yy(i) = Rnd(0,h)
      vx(i) = Rnd(-50,50)   
      vy(i) = Rnd(-50,50)
      ax(0) = 0
      ay(0) = 0
      r(i) = Rnd(10,15)
   Next 
   
   dt = .1
   
   Accelerometer.StartListening("Accelerometer") 
     
     Timer1.Initialize("MainEvent", 10)
   Timer1.Enabled = True

    
  
End Sub

Sub MainEvent_Tick
      
      For i = 0 To 99
      
         For j = 0 To 99
            ax(j) = 0.0;
            ay(j) = 0.0;
         Next
            
         For j = 0 To 99
         
            If i <> j Then
               diffx = xx(i) - xx(j)
               diffy = yy(i) - yy(j)
               dist1 = Sqrt(Power(diffx,2)+Power(diffy,2))
               dist2 = r(i) + r(j)
               
               If dist1 < dist2 Then
                  deltaDistance = (dist2 - dist1)
               
                  xval = (((deltaDistance * 10.0) / dist2) * diffx)
                  yval = (((deltaDistance * 10.0) / dist2) * diffy)
                  
                  ax(i) = ax(i) + xval
                       ay(i) = ay(i) + yval
 
               End If
            
            End If
         Next
      
         vx(i) = (vx(i) + (ax(i) * dt)) * .99
         vy(i) = (vy(i) + (ay(i) * dt)) * .99 
           xx(i) = xx(i) + (vx(i) * dt)
         yy(i) = yy(i) + (vy(i) * dt)
       
         If xx(i)-r(i) < 0 Then
            vx(i) = -vx(i)
            xx(i) = 0 + r(i)
         End If
         If yy(i)-r(i) < 0 Then
            vy(i) = -vy(i)
            yy(i) = 0 + r(i)
         End If
         If xx(i)+r(i) > w Then
            vx(i) = -vx(i)
            xx(i) = w - r(i)
         End If
         If yy(i)+r(i) > h Then
            vy(i) = -vy(i)
            yy(i) = h - r(i)
         End If
         
         
      Next 
      
        Canvas1.DrawColor(Colors.Black)
      
      Canvas1.DrawCircle(xx(0),yy(0),r(0),Colors.Red,True,5)
      For i = 1 To 99
         Canvas1.DrawCircle(xx(i),yy(i),r(i),Colors.Green,True,5)
      Next 
        Activity.Invalidate
      DoEvents 

End Sub
Sub Activity_Touch (Action As Int, X As Float, Y As Float)
   xx(0) = x
   yy(0) = y
   vx(0) = 0
   vy(0) = 0
   
End Sub






Sub Accelerometer_AccelerometerChanged (X As Float, Y As Float, Z As Float)
    For i = 0 To 99
      
      Msgbox(x,"y")
      Msgbox(y,"y")
      'Msgbox(z,"z")
      
      'ax(i) = x
      'ay(i) = y
   
   Next 
   
   
End Sub
 
Upvote 0
Top