Colision detection

Septym

Member
Licensed User
Longtime User
i am trying to create a game and the last thing that i need to implement is the collision detection i am having some problems with it , some of the enemy's die when others are hit. i will post the code that i have so far, i hope some one can help me with this problem

Thanks

B4X:
re1 = e1.left + 40
re2 = e2.left + 40
re3 = e3.left + 40
re4 = e4.left + 40
re5 = e5.left + 40
re6 = e6.left + 40
re7 = e7.left + 40
re8 = e8.left + 40
re9 = e9.left + 40
re10 = e10.left + 40
re11 = e11.left + 40
re12 = e12.left + 40
re13 = e13.left + 40
re14 = e14.left + 40
re15 = e15.left + 40
re16 = e16.left + 40
re17 = e17.left + 40
re18 = e18.left + 40

be1 = e1.top - 40
be2 = e2.top - 40
be3 = e3.top - 40
be4 = e4.top - 40
be5 = e5.top - 40
be6 = e6.top - 40
be7 = e7.top - 40
be8 = e8.top - 40
be9 = e9.top - 40
be10 = e10.top - 40
be11 = e11.top - 40
be12 = e12.top - 40
be13 = e13.top - 40
be14 = e14.top - 40
be15 = e15.top - 40
be16 = e16.top - 40
be17 = e17.top - 40
be18 = e18.top - 40

rmiss = miss.left + 25
sleft = Ship.left + 20
stop = Ship.Top 
Eco(0,0) = e1.left
Eco(0,1) = e1.Top
Eco(1,0) = e2.left
Eco(1,1) = e2.Top
Eco(2,0) = e3.left
Eco(2,1) = e3.Top
Eco(3,0) = e4.left
Eco(3,1) = e4.Top
Eco(4,0) = e5.left
Eco(4,1) = e5.Top
Eco(5,0) = e6.left
Eco(5,1) = e6.Top
Eco(6,0) = e7.left
Eco(6,1) = e7.Top
Eco(7,0) = e6.left
Eco(7,1) = e6.Top
Eco(8,0) = e7.left
Eco(8,1) = e7.Top
Eco(9,0) = e8.left
Eco(9,1) = e8.Top
Eco(10,0) = e9.left
Eco(10,1) = e9.Top
Eco(11,0) = e10.left
Eco(11,1) = e10.Top
Eco(12,0) = e11.left
Eco(12,1) = e11.Top
Eco(13,0) = e12.left
Eco(13,1) = e12.Top
Eco(14,0) = e13.left
Eco(14,1) = e13.Top
Eco(15,0) = e14.left
Eco(15,1) = e14.Top
Eco(16,0) = e15.left
Eco(16,1) = e15.Top
Eco(17,0) = e16.left
Eco(17,1) = e16.Top
Eco(18,0) = e17.left
Eco(18,1) = e17.Top
Eco(19,0) = e18.left
Eco(19,1) = e18.Top

If miss.Top < Eco(0,1) AND miss.Top > be1 Then
   If miss.left > Eco(0,0) AND miss.left < re1 Then
      misstimer.Enabled = False
      miss.RemoveView
      e1.Top = e1.Top + 1000
      EP.Play
   End If 
End If 
If miss.Top < Eco(1,1)  AND miss.Top > be2 Then
   If miss.left > Eco(1,0) AND miss.left < re2 Then
      misstimer.Enabled = False
      miss.RemoveView
      e2.Top = e2.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(2,1)  AND miss.Top > be3 Then
   If miss.left > Eco(2,0) AND miss.left < re3 Then
      misstimer.Enabled = False
      miss.RemoveView
      e3.Top = e3.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(3,1)  AND miss.Top > be4 Then
   If miss.left > Eco(3,0) AND miss.left < re4 Then
      misstimer.Enabled = False
      miss.RemoveView
      e4.Top = e4.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(4,1)  AND miss.Top > be5 Then
   If miss.left > Eco(4,0) AND miss.left < re5 Then
      misstimer.Enabled = False
      miss.RemoveView
      e5.Top = e5.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(5,1)  AND miss.Top > be6 Then
   If miss.left > Eco(5,0) AND miss.left < re6 Then
      misstimer.Enabled = False
      miss.RemoveView
      e6.Top = e6.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(6,1)  AND miss.Top > be7 Then
   If miss.left > Eco(6,0) AND miss.left < re7 Then
      misstimer.Enabled = False
      miss.RemoveView
      e7.Top = e7.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(7,1)  AND miss.Top > be8 Then
   If miss.left > Eco(7,0) AND miss.left < re8 Then
      misstimer.Enabled = False
      miss.RemoveView
      e8.Top = e8.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(8,1)  AND miss.Top > be9 Then
   If miss.left > Eco(8,0) AND miss.left < re9 Then
      misstimer.Enabled = False
      miss.RemoveView
      e9.Top = e9.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(9,1)  AND miss.Top > be10 Then
   If miss.left > Eco(9,0) AND miss.left < re10 Then
      misstimer.Enabled = False
      miss.RemoveView
      e10.Top = e10.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(10,1)  AND miss.Top > be11 Then
   If miss.left > Eco(10,0) AND miss.left < re11 Then
      misstimer.Enabled = False
      miss.RemoveView
      e11.Top = e11.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(11,1)  AND miss.Top > be12 Then
   If miss.left > Eco(11,0) AND miss.left < re12 Then
      misstimer.Enabled = False
      miss.RemoveView
      e12.Top = e12.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(12,1)  AND miss.Top > be13 Then
   If miss.left > Eco(12,0) AND miss.left < re13 Then
      misstimer.Enabled = False
      miss.RemoveView
      e13.Top = e13.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(13,1)  AND miss.Top > be14 Then
   If miss.left > Eco(13,0) AND miss.left < re14 Then
      misstimer.Enabled = False
      miss.RemoveView
      e14.Top = e14.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(14,1)  AND miss.Top > be15 Then
   If miss.left > Eco(14,0) AND miss.left < re15 Then
      misstimer.Enabled = False
      miss.RemoveView
      e15.Top = e15.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(15,1)  AND miss.Top > be16 Then
   If miss.left > Eco(15,0) AND miss.left < re16 Then
      misstimer.Enabled = False
      miss.RemoveView
      e16.Top = e16.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(16,1)  AND miss.Top > be17 Then
   If miss.left > Eco(16,0) AND miss.left < re17 Then
      misstimer.Enabled = False
      miss.RemoveView
      e17.Top = e17.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(17,1)  AND miss.Top > be18 Then
   If miss.left > Eco(17,0) AND miss.left < re18 Then
      misstimer.Enabled = False
      miss.RemoveView
      e18.Top = e18.Top + 1000
      EP.Play
   End If 
End If
 

Geezer

Active Member
Licensed User
Longtime User
B4X:
re1 = e1.left + 40

be1 = e1.top - 40

I'm assuming re1 is right side of enemy 1, or e1 left + 40

And that be1 is bottom of enemy 1 ... but shouldn't the bottom be the top PLUS 40 ?
 
Upvote 0

Septym

Member
Licensed User
Longtime User
Thank you i realise my stupidity, for some reason i was thinking that the bottom of the screen was "0", i have re run the code and tested it but now the missiles just go through the enemy's, before at least the collision worked sometimes.

B4X:
re1 = e1.left + 40
re2 = e2.left + 40
re3 = e3.left + 40
re4 = e4.left + 40
re5 = e5.left + 40
re6 = e6.left + 40
re7 = e7.left + 40
re8 = e8.left + 40
re9 = e9.left + 40
re10 = e10.left + 40
re11 = e11.left + 40
re12 = e12.left + 40
re13 = e13.left + 40
re14 = e14.left + 40
re15 = e15.left + 40
re16 = e16.left + 40
re17 = e17.left + 40
re18 = e18.left + 40

be1 = e1.top + 40
be2 = e2.top + 40
be3 = e3.top + 40
be4 = e4.top + 40
be5 = e5.top + 40
be6 = e6.top + 40
be7 = e7.top + 40
be8 = e8.top + 40
be9 = e9.top + 40
be10 = e10.top + 40
be11 = e11.top + 40
be12 = e12.top + 40
be13 = e13.top + 40
be14 = e14.top + 40
be15 = e15.top + 40
be16 = e16.top + 40
be17 = e17.top + 40
be18 = e18.top + 40

rmiss = miss.left + 25
sleft = Ship.left + 20
stop = Ship.Top 
Eco(0,0) = e1.left
Eco(0,1) = e1.Top
Eco(1,0) = e2.left
Eco(1,1) = e2.Top
Eco(2,0) = e3.left
Eco(2,1) = e3.Top
Eco(3,0) = e4.left
Eco(3,1) = e4.Top
Eco(4,0) = e5.left
Eco(4,1) = e5.Top
Eco(5,0) = e6.left
Eco(5,1) = e6.Top
Eco(6,0) = e7.left
Eco(6,1) = e7.Top
Eco(7,0) = e6.left
Eco(7,1) = e6.Top
Eco(8,0) = e7.left
Eco(8,1) = e7.Top
Eco(9,0) = e8.left
Eco(9,1) = e8.Top
Eco(10,0) = e9.left
Eco(10,1) = e9.Top
Eco(11,0) = e10.left
Eco(11,1) = e10.Top
Eco(12,0) = e11.left
Eco(12,1) = e11.Top
Eco(13,0) = e12.left
Eco(13,1) = e12.Top
Eco(14,0) = e13.left
Eco(14,1) = e13.Top
Eco(15,0) = e14.left
Eco(15,1) = e14.Top
Eco(16,0) = e15.left
Eco(16,1) = e15.Top
Eco(17,0) = e16.left
Eco(17,1) = e16.Top
Eco(18,0) = e17.left
Eco(18,1) = e17.Top
Eco(19,0) = e18.left
Eco(19,1) = e18.Top

If miss.Top < Eco(0,1) AND miss.Top > be1 Then
   If miss.left > Eco(0,0) AND miss.left < re1 Then
      misstimer.Enabled = False
      miss.RemoveView
      e1.Top = e1.Top + 1000
      EP.Play
   End If 
End If 
If miss.Top < Eco(1,1)  AND miss.Top > be2 Then
   If miss.left > Eco(1,0) AND miss.left < re2 Then
      misstimer.Enabled = False
      miss.RemoveView
      e2.Top = e2.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(2,1)  AND miss.Top > be3 Then
   If miss.left > Eco(2,0) AND miss.left < re3 Then
      misstimer.Enabled = False
      miss.RemoveView
      e3.Top = e3.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(3,1)  AND miss.Top > be4 Then
   If miss.left > Eco(3,0) AND miss.left < re4 Then
      misstimer.Enabled = False
      miss.RemoveView
      e4.Top = e4.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(4,1)  AND miss.Top > be5 Then
   If miss.left > Eco(4,0) AND miss.left < re5 Then
      misstimer.Enabled = False
      miss.RemoveView
      e5.Top = e5.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(5,1)  AND miss.Top > be6 Then
   If miss.left > Eco(5,0) AND miss.left < re6 Then
      misstimer.Enabled = False
      miss.RemoveView
      e6.Top = e6.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(6,1)  AND miss.Top > be7 Then
   If miss.left > Eco(6,0) AND miss.left < re7 Then
      misstimer.Enabled = False
      miss.RemoveView
      e7.Top = e7.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(7,1)  AND miss.Top > be8 Then
   If miss.left > Eco(7,0) AND miss.left < re8 Then
      misstimer.Enabled = False
      miss.RemoveView
      e8.Top = e8.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(8,1)  AND miss.Top > be9 Then
   If miss.left > Eco(8,0) AND miss.left < re9 Then
      misstimer.Enabled = False
      miss.RemoveView
      e9.Top = e9.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(9,1)  AND miss.Top > be10 Then
   If miss.left > Eco(9,0) AND miss.left < re10 Then
      misstimer.Enabled = False
      miss.RemoveView
      e10.Top = e10.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(10,1)  AND miss.Top > be11 Then
   If miss.left > Eco(10,0) AND miss.left < re11 Then
      misstimer.Enabled = False
      miss.RemoveView
      e11.Top = e11.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(11,1)  AND miss.Top > be12 Then
   If miss.left > Eco(11,0) AND miss.left < re12 Then
      misstimer.Enabled = False
      miss.RemoveView
      e12.Top = e12.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(12,1)  AND miss.Top > be13 Then
   If miss.left > Eco(12,0) AND miss.left < re13 Then
      misstimer.Enabled = False
      miss.RemoveView
      e13.Top = e13.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(13,1)  AND miss.Top > be14 Then
   If miss.left > Eco(13,0) AND miss.left < re14 Then
      misstimer.Enabled = False
      miss.RemoveView
      e14.Top = e14.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(14,1)  AND miss.Top > be15 Then
   If miss.left > Eco(14,0) AND miss.left < re15 Then
      misstimer.Enabled = False
      miss.RemoveView
      e15.Top = e15.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(15,1)  AND miss.Top > be16 Then
   If miss.left > Eco(15,0) AND miss.left < re16 Then
      misstimer.Enabled = False
      miss.RemoveView
      e16.Top = e16.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(16,1)  AND miss.Top > be17 Then
   If miss.left > Eco(16,0) AND miss.left < re17 Then
      misstimer.Enabled = False
      miss.RemoveView
      e17.Top = e17.Top + 1000
      EP.Play
   End If 
End If 
If miss.top < Eco(17,1)  AND miss.Top > be18 Then
   If miss.left > Eco(17,0) AND miss.left < re18 Then
      misstimer.Enabled = False
      miss.RemoveView
      e18.Top = e18.Top + 1000
      EP.Play
   End If 
End If
 
Last edited:
Upvote 0

Geezer

Active Member
Licensed User
Longtime User
You are checking if the missile is above AND below the enemy

B4X:
If miss.Top < Eco(0,1) AND miss.Top > be1 Then

You need to check if the missile is in the area that the enemy is in

B4X:
if miss.top > e1.top AND miss.top < be1 then

This will check if the missile top is within the range of enemy top and bottom. Then you check if the missile is between left and right sides.

If both those match, it's a hit.
 
Upvote 0
Top