swipe direction and speed

notedop

Member
Licensed User
Longtime User
Hi,

First let me introduce myself, my name is Raoul and Im from the Netherlands. Programming is a hobby of mine which I once in a while pick-up again. The most 'programs' I build are actually access databases. So I consider my skills still to be novice.
Anyhow, I came across with the program and bought it so I can play around a little bit with it.

So I did.... unfortunatly I now have a problem on which I hope someone can help me with. In attached project you find a little program which fairly simple. The blok moves and bounces of the wall. You can reposition it by clicking (tapping) in the screen. My next step would be changing it direction by swiping and eventually also adjusting speed by the swipe.

Unfortuatly it's been over 10 years since Ive done mathematics and I can't recall how to calculate the increase or decrease of x and y. Can someone help me out?

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
Public DirX As Float
Public DirY As Float
Dim MOVEMENT_THRESHOLD As Int    
MOVEMENT_THRESHOLD = 50dip

Dim StartX As Float
Dim StartY As Float


Public touch As Boolean
Public touchtime As Int

DirX=1
DirY=1
touchtime=0



End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
Dim image1 As ImageView
Dim timer1 As Timer

End Sub

Sub Activity_Create(FirstTime As Boolean)

   Activity.LoadLayout("main")
   
   If FirstTime Then
      timer1.initialize("timer1", 50)
      
      
      image1.Initialize("")
         If image1.IsInitialized() Then
            image1.Bitmap = LoadBitmap(File.DirAssets,"blok.png")
         Activity.AddView(image1, 1,1,25,25)
         End If
   End If
   timer1.Enabled = True
   
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub Activity_Touch (Action As Int, x As Float, Y As Float)

   Activity.Title = x & " " & Y

   image1.Top = Y - image1.Height/2
   image1.Left = x -image1.Width/2
'   Log(Action)



  Select Action
   Case Activity.ACTION_DOWN        
   StartX = x
      touch = True 

      StartY = Y
      
   Case Activity.ACTION_UP
   
       If (Abs(x - StartX) > MOVEMENT_THRESHOLD) OR (Abs(Y - StartY) > MOVEMENT_THRESHOLD)    Then                
          Select True
            Case (x < StartX)
               If (x-StartX)/(Y-StartY) < 0 Then 
               DirX = (Y-StartY)/(x-StartX)
               Else
               DirX = (Y-StartY)/(x-StartX) *-1
               End If
               
            Case (x > StartX)
               If (x-StartX)/(Y-StartY) < 0 Then
                DirX = (Y-StartY)/(x-StartX) *-1
               Else
               DirX = (Y-StartY)/(x-StartX)
               End If                  
         End Select
         
         
'         Select True
'            Case (Y < StartY)
'               If ((Y-StartY)/(x-StartX)) < 0 Then
'                DirY = (Y-StartY)/(x-StartX)
'               Else 
'               DirY = (Y-StartY)/(x-StartX)*-1
'               End If
'            Case (Y > StartY)
'               If ((Y/StartY)/(x-StartX)) < 0 Then
'                DirY = (Y-StartY)/(x-StartX)
'               Else 
'               DirY = ((Y-StartY)/(x-StartX))*-1
'               End If
'         End Select      
         
      End If
   
'disable the touchflag   
      touch = False
      touchtime=0
End Select

Log(DirX & " dirx" )
Log(DirY & " diry" )   
   
End Sub
Sub Swipe(Left As Boolean)    
If Left Then Msgbox("Left", "") Else Msgbox("Right", "")
End Sub


Sub timer1_Tick

   Dim x1 As Int
   Dim y1 As Int
   Dim x2 As Int
   Dim y2 As Int

If touch = True Then
touchtime= touchtime+1
End If


'this Is the movement of the image. 
'In Case it exceeds the Activity dimension, the direction Is reversed

   Select True
      Case (image1.Top <0):
         DirY = DirY * -1
      Case ((image1.Top+image1.Height) > Activity.Height):
         DirY = DirY * -1
   End Select

   Select True
      Case (image1.Left< 0):
         DirX = DirX * -1
      Case ((image1.Left+image1.Width) > Activity.Width):
         DirX = DirX * -1
   End Select
   
   
'   Log("dirx" & DirX)
'   Log("diry" & DirY)

'   move the actual image
   image1.Top = image1.Top + DirY
   image1.Left = image1.left + DirX
   
   
End Sub

Sub but_slow_Click
   
   Select True
      Case (DirX<0):
      DirX= DirX+1
      Case (DirX>0):
      DirX=DirX-1
   End Select
   
   Select True
      Case (DirY<0):
      DirY=DirY+1
      Case (DirY>0):
      DirY=DirY-1
   End Select
   
   

End Sub
Sub but_fast_Click

   Select True
      Case (DirX<0):
      DirX=DirX-1
      Case (DirX>0):
      DirX=DirX+1
   End Select
   
   Select True
      Case (DirY<0):
      DirY=DirY-1
      Case (DirY>0):
      DirY=DirY+1
   End Select
   
End Sub
 

Attachments

  • test2.zip
    7.9 KB · Views: 187

notedop

Member
Licensed User
Longtime User
I just realized that I did not take the screen resolution in account. So incrementing x by 1 and incrementing y by 1 does not mean it moves in 45 degrees. Ill try to include it later on, still any help and tips are appreciated!
 

notedop

Member
Licensed User
Longtime User
Here you are, I hope that it's what you are looking for.
The pixel size is the same in both directions so no problem.

Attached the modified program.

Best regards.

:sign0098:

Awesome, seems to be easier then I was thinking :-D. I'll have another look later on today, need to go get a aquarium now with my gf.
 

notedop

Member
Licensed User
Longtime User
Cool stuff. I was wondering about 1 piece of the code. The point you use the power() function. For which value does cE stand for?

B4X:
Damping = Power(cE, -DampingTime * DampingFactor)
 
Last edited:
Top