Android Question Detect colliding ImageViews

Fusseldieb

Active Member
Licensed User
Longtime User
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...

Thanks in advance ;)
 

abhishek007p

Active Member
Licensed User
Longtime User
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...

Thanks in advance ;)

how about studying Libgdx for creating games?..
 
Upvote 0

WAZUMBi

Well-Known Member
Licensed User
Longtime User
Search for gameview
Erel has an excelent example and tutorial which has basic collision detection
 
Upvote 0

TheMightySwe

Active Member
Licensed User
Longtime User
This might work

B4X:
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
 
Upvote 0

James Chamblin

Active Member
Licensed User
Longtime User
@TheMightySwe Thanks so much for this solution.
I have only one more question...
Why is (Top + Height) undeclared?
It's red...

Thanks ;)
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

Example of Sub in action
 

Attachments

  • ImvCollide.zip
    9.5 KB · Views: 80
Upvote 0
Top