Hi all,
it is possible to detect colliding ImageViews?
I want to make a game, but detecting colliding ImageViews are essential for this.
Are any (not too long) code available for this?
I have seen around and have only found examples with tons of code...
Hi all,
it is possible to detect colliding ImageViews?
I want to make a game, but detecting colliding ImageViews are essential for this.
Are any (not too long) code available for this?
I have seen around and have only found examples with tons of code...
Sub HitTest(View1 As View, View2 As View) As Boolean
If View1.Left > View2.Left AND View1.Left < (View2.Left + View2.Width) AND _
View1.Top > View2.Top AND View1.Top < (View2.Top + View2.Height) Then Return True
If View1.Left > View2.Left AND View1.Left < (View2.Left + View2.Width) AND _
(View1.Top + View1.Height) > View2.Top AND (View1.Top + View1.Height) < (Top + Height) Then Return True
If (View1.Left + View1.Width) > View2.Left AND (View1.Left + View1.Width) < (View2.Left + View2.Width) AND _
View1.Top > View2.Top AND View1.Top < (Top + Height) Then Return True
If (View1.Left + View1.Width) > View2.Left AND (View1.Left + View1.Width) < (View2.Left + View2.Width) AND (View1.Top + View1.Height) > View2.Top AND (View1.Top + View1.Height) < (View2.Top + View2.Height) Then Return True
Return False
End Sub
Most likely a bug. Probably should be (View2.Top + View2.Height). Don't know why he is doing so many checks, though. I came up with this solution
B4X:
Sub Collided(Image1 As ImageView, Image2 As ImageView) As Boolean
If Image1.Left <= Image2.Left+Image2.Width AND _
Image2.Left <= Image1.Left+Image1.Width AND _
Image1.Top <= Image2.Top+Image2.Height AND _
Image2.Top <= Image1.Top+Image1.Height Then Return True
Return False
End Sub