Android Question 2d gameview and collision detection

raphipps2002

Active Member
Licensed User
Longtime User
Never having programmed a game its a bit of a challenge. Need some help please. The tutorials part 1,2,3 are written it seems for advanced programers, which I am not, and i need some of the basics. I have tried to follow code and achieved some headway.

Using gameview and a timer I have three bitmaps which re-draw on each tick and currently they are all spiining around happily. In the asteriods app I copied the class for collision detection where I am having problems.

Rect; Rich and squeak spin around screen
Nana: Stays at bottom and I can move her when touching the screen.

All works!

BUT

The code in bold below, and the collision detection class (copied from Asteriods) simply returns null every time despite numberous collisions.

I am sure the sure the GetRect sub has somthing to do with it but I cannot fathom it out. Can anyone help please! It will be a major breakthrough!





B4X:
Sub Timer1_Tick
   
    'move the rects

    nana.DestRect.Left = x1
    nana.DestRect.Top = y1
    nana.DestRect.Right = nana.DestRect.Left + size   
    nana.DestRect.Bottom = nana.DestRect.Top + size
   
    rich.DestRect.Left = rich.DestRect.Left + (x2dir * pos1)
    rich.DestRect.Top = rich.DestRect.Top + (y2Dir * pos1)
    rich.DestRect.Right = rich.DestRect.Left + size
    rich.DestRect.Bottom = rich.DestRect.Top + size

    squeak.DestRect.Left = squeak.DestRect.Left + (x3Dir * pos1)
    squeak.DestRect.Top = squeak.DestRect.Top + (y3Dir * pos1)
    squeak.DestRect.Right = squeak.DestRect.Left + size
    squeak.DestRect.Bottom = squeak.DestRect.Top + size

   
    Dim Otherrec As List
    Otherrec.Initialize()
    Otherrec.Add(rich.DestRect)
    Otherrec.Add(squeak.DestRect)
   
    Dim scale = (100%x + 100%y) / (320 + 480)
    Collision.Initialize(squeak.DestRect , Otherrec , 10 , 10*scale)
   
   
    If Collision.Tick <> Null Then
        toastmessageshow("collision",false)
    End If
    
    
    'Rich
    If rich.DestRect.Left <= 0 Then
        x2dir = 1
    End If
   
    If (rich.DestRect.Right - size) > Activity.Width Then
        x2dir = -1
    End If
   
    If rich.DestRect.top <= 0 Then
        y2Dir = 1
    End If
   
    If (rich.DestRect.bottom+50dip) >= nana.DestRect.top Then
        y2Dir = -1
    End If
   
    'Squak
    If squeak.DestRect.Left <= 0 Then
        x3Dir = 1
    End If
   
    If (squeak.DestRect.Right - size) > Activity.Width Then
        x3Dir = -1
    End If
   
    If squeak.DestRect.top <= 0 Then
        y3Dir = 1
    End If
   
    If (squeak.DestRect.bottom+50dip ) >= nana.DestRect.top Then
        y3Dir = -1
    End If
   
   
    gv.Invalidate 'Mark the whole GameView as "dirty"

End Sub

'This method is called by CollisionDetector to get the asteroid location and size
'CallSub methods can only return primitive types, so we pass an array with a single element.

Public Sub GetRect(outRect() As Rect)
    outRect(0) = squeak.DestRect
End Sub

B4X:
'Class module
Sub Class_Globals
    Private MyRect As Rect
    Private Others As List
    Private checkInterval, interval As Int
    Private outRect(1) As Rect
    Private safeZone As Int
End Sub

Public Sub Initialize (vMyRect As Rect, vOthers As List, vCheckInterval As Int, vSafeZone As Int)
    MyRect = vMyRect
    'Others is a list of objects. Each object should have a GetRect sub such as:
    'Public Sub GetRect(outRect() As Rect)
    '    outRect(0) = bd.DestRect
    'End Sub
    'Note that this class holds a reference to the list. This means that if later an object is added or removed
    'then it will automatically affect this class as well.
    Others = vOthers
    'Check for collision every x ticks. If the objects do not move too fast there is no point
    'in checking for collision every tick.
    checkInterval = vCheckInterval
    'As each sprite is treated as a rectangle you may want to decrease the rectangle size to avoid cases
    'where the collision is in two corners for example.
    safeZone = vSafeZone
End Sub

'Checks for collisions. Returns the collided object if there was a collision
Public Sub Tick As Object

   
    interval = interval + 1
    If interval = checkInterval Then
       
       
        interval = 0
       
        Dim r As Rect
        For i = 0 To Others.Size - 1
            CallSub2(Others.Get(i), "GetRect", outRect)
            If MyRect.Left < outRect(0).Right - safeZone AND MyRect.Right > outRect(0).Left + safeZone _
                AND MyRect.Top < outRect(0).Bottom - safeZone AND MyRect.Bottom > outRect(0).Top + safeZone Then
                Return Others.Get(i)
            End If
        Next
        Return Null
    End If
End Sub
 
Top