Author Topic: Collision isn't working[BB2D]  (Read 4544 times)

0 Members and 1 Guest are viewing this topic.

Offline no prob

  • ZX 81
  • *
  • Posts: 11
  • Karma: 0
    • View Profile
Collision isn't working[BB2D]
« on: September 01, 2006 »
 :D my collisions are not working for some reason
 
Code: [Select]
Graphics 640,480
SetBuffer BackBuffer()
AutoMidHandle True


;TYPES
Type ship
    Field x,y
End Type

Global s.ship=New ship
       s\x=320
       s\y=440     


Type laser
   Field x,y
End Type


Type alien
   Field x,y,frame
End Type

Global a.alien=New alien
          a\x=30
          a\y=30
          a\frame=0

Global ship=LoadImage("graphics/ship.bmp")
Global laser=LoadImage("graphics/laser.bmp")
Global alien=LoadAnimImage("graphics/alien.bmp",68,38,0,10)
Global background=LoadImage("graphics/stars.bmp")

While Not KeyHit(1)
Cls

t=t


If a\x>=620 Then t=1

Select t
     
         Case 1
              a\x=a\x-1
           
     
         Case 2
              a\x=a\x+1
             
End Select


If a\x<=30 Then t=2


UpdateAlien()
UpdateShip()
LaserUpdate()
Collision_Update()


TileImage background,x,y
Flip

Wend



;-----------------------
;     Update Ship      -
;-----------------------
Function UpdateShip()

If KeyDown(203) Then s\x=s\x-2 ;move ship left
If s\x<=10 Then s\x=10 ;stop ship from going off screen

If KeyDown(205) Then s\x=s\x+2 ;move ship right
If s\x>=610 Then s\x=610;stop ship from going off screen

If KeyHit(57) Then    ;if space is pushed then shoot laser
   l.laser=New laser     
        l\x=s\x
        l\y=s\y+2
EndIf

DrawImage ship,s\x,s\y

End Function

   

;---------------
;  Laser Update-
;---------------
Function LaserUpdate()
  For l.laser=Each laser   
        l\y=l\y-5 
   
      If l\y<0 Then  ;if laser goes off screen delete it
         
           Delete l
     
      Else

           DrawImage laser,l\x,l\y
     
     EndIf
             
  Next
End Function

;-----------------
;  Alien Update  -
;-----------------
Function UpdateAlien()

a\frame=a\frame+1


Delay 4
If a\frame>9 Then a\frame=0
If a\frame<0 Then a\frame=9

DrawImage alien,a\x,a\y,a\frame

End Function

;----------------------
;  Collision Update   -
;----------------------
Function Collision_Update()
 For a.alien=Each alien 
   If ImagesOverlap(laser,l\x,l\y,alien,a\x,a\y)
       Delete a
   EndIf
 Next
End Function
:whack:
« Last Edit: July 21, 2007 by Shockwave »

Offline Blitz Amateur

  • Atari ST
  • ***
  • Posts: 243
  • Karma: 13
    • View Profile
Re: Collision isn't working
« Reply #1 on: September 01, 2006 »
Code: [Select]
Function Collision_Update()
For a.alien=Each alien
   If ImagesOverlap(laser,l\x,l\y,alien,a\x,a\y)
       Delete a
   EndIf
Next
End Function

The problem with that code, is that you need to loop through your lasers too, that way you can tell what you're colliding with :)

Try putting a For..Each loop for the lasers around the For..Each loop for the aliens

Example:
Code: [Select]
For..Each laser
    For..Each alien
        Collision Check
    Next
Next

See if you can get that to work :)

Offline no prob

  • ZX 81
  • *
  • Posts: 11
  • Karma: 0
    • View Profile
Re: Collision isn't working
« Reply #2 on: September 01, 2006 »
 ;D thanks that  worked almost perfectly only my alien don't get deleted  :||  O0

Offline Blitz Amateur

  • Atari ST
  • ***
  • Posts: 243
  • Karma: 13
    • View Profile
Re: Collision isn't working
« Reply #3 on: September 01, 2006 »
If you need some help still, feel free to post your code, and someone can get you an answer =)

Offline no prob

  • ZX 81
  • *
  • Posts: 11
  • Karma: 0
    • View Profile
Re: Collision isn't working
« Reply #4 on: September 13, 2006 »
Alright I still can't get my collision to work so what's the deal  :-\
email me at JoshuaMoten@gcasda.org and if that don't work just use all lower case letters

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Collision isn't working
« Reply #5 on: September 13, 2006 »
No offence but if you want help on your project, please post your code. If the problem is sorted out over email then nobody else on this forum in the future with a similar problem would be able to get any benefit from it.

Here's what to do.
Post *exactly* what you want the code to do and explain what the problem is, then please post the full code, you can get help off everyone here who has an interest in games (even though this is a demo forum, some people still like games) and who uses Blitz and there are quite a few.

Posting a line like "collision aint working" isn't helpful to those who want to help you. Be more explicit.

Assuming a member of the forum will just contact you via email because you want them to, when it's you who has the problem is arrogant so think about your manners a bit.

Thanks.
« Last Edit: September 13, 2006 by Shockwave »
Shockwave ^ Codigos
Challenge Trophies Won:

Offline no prob

  • ZX 81
  • *
  • Posts: 11
  • Karma: 0
    • View Profile
Re: Collision isn't working
« Reply #6 on: September 17, 2006 »
Ok I see what you are saying in a way but my code is already posted. I just want the alien to be deleted as soon as the laser hit the alien but it just goes through it. How can I accomplish this task
Code: [Select]
Graphics 640,480
SetBuffer BackBuffer()
AutoMidHandle True


;TYPES
Type ship
    Field x,y
End Type

Global s.ship=New ship
       s\x=320
       s\y=440     


Type laser
   Field x,y
End Type


Type alien
   Field x,y,frame,shoot
End Type

Global a.alien=New alien
          a\x=30
          a\y=30
          a\frame=0
          a\shoot=shoot

Global ship=LoadImage("graphics/ship.bmp")
Global laser=LoadImage("graphics/laser.bmp")
MaskImage laser,0,0,0
Global alien=LoadAnimImage("graphics/alien.bmp",68,38,0,10)
MaskImage alien,0,0,0
Global background=LoadImage("graphics/stars.bmp")
Global a_bullet=LoadAnimImage("graphics/s_bullet.bmp",34,19,0,10)

While Not KeyHit(1)
Cls

t=t


If a\x>=620 Then t=1

Select t
     
         Case 1
              a\x=a\x-1
           
     
         Case 2
              a\x=a\x+1
             
End Select


If a\x<=30 Then t=2


UpdateAlien()
UpdateShip()
LaserUpdate()



TileImage background,x,y
Flip

Wend
End


;-----------------------
;     Update Ship      -
;-----------------------
Function UpdateShip()

If KeyDown(203) Then s\x=s\x-2 ;move ship left
If s\x<=10 Then s\x=10 ;stop ship from going off screen

If KeyDown(205) Then s\x=s\x+2 ;move ship right
If s\x>=610 Then s\x=610;stop ship from going off screen

If KeyHit(57) Then    ;if space is pushed then shoot laser
   l.laser=New laser   
        l\x=s\x
        l\y=s\y+2
EndIf

DrawImage ship,s\x,s\y

End Function

   

;---------------
;  Laser Update-
;---------------
Function LaserUpdate()
  For l.laser=Each laser   
        l\y=l\y-5 
   
      If l\y<0 Then  ;if laser goes off screen delete it
         
           Delete l
     
      Else

           DrawImage laser,l\x,l\y
     
     EndIf
             
  Next
End Function

;-----------------
;  Alien Update  -
;-----------------
Function UpdateAlien()

a\frame=a\frame+1


Delay 4
If a\frame>9 Then a\frame=0
If a\frame<0 Then a\frame=9

DrawImage alien,a\x,a\y,a\frame

End Function

;--------------------
;  Collision Update -
;--------------------
Function Update_Collision()
 For l.laser=Each laser
     
    For a.alien=Each alien
   
        If ImagesCollide(laser,l\x,l\y,0,alien,a\x,a\y,0)
          Delete a
        EndIf
       
 Next     
     Next
 
   

End Function

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Collision isn't working
« Reply #7 on: September 17, 2006 »
I'll have a look for you and fix it up :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Collision isn't working
« Reply #8 on: September 17, 2006 »
Righto, well there are a lot of bugs in the program but I've got it working, though to be honest with you, you'll be better off re-writing it from scratch. This is a demo code forum but we could make an exception if you need some help I guess.

Here's the program now doing what you wanted it to.

NOTE. You need to change the image loading back to how it was as I had to change this program to make it work, you hadn't included your graphics.

Code: [Select]
Graphics 640,480
SetBuffer BackBuffer()
AutoMidHandle True


;TYPES
Type ship
    Field x,y
End Type

Global s.ship=New ship
       s\x=320
       s\y=440     


Type laser
   Field x,y
End Type


Type alien
   Field x,y,frame,shoot,dir
End Type

Global a.alien=New alien
          a\x=30
          a\y=30
          a\frame=0
          a\shoot=shoot
  a\dir=1

Global ship=LoadImage("ship.bmp")
Global laser=LoadImage("laser.bmp")
MaskImage laser,0,0,0
Global alien=LoadImage("alien.bmp")
MaskImage alien,0,0,0
Global background=LoadImage("stars.bmp")
Global a_bullet=LoadImage("s_bullet.bmp")




While Not KeyHit(1)
Cls

;t=t

For zz.alien = Each alien
If zz\x>=620 Then zz\dir=-1
If zz\x<=30 Then zz\dir=1
zz\x=zz\x+zz\dir
Next




UpdateAlien()
UpdateShip()
LaserUpdate()
update_collision()


TileImage background,x,y
Flip

Wend
End


;-----------------------
;     Update Ship      -
;-----------------------
Function UpdateShip()

If KeyDown(203) Then s\x=s\x-2 ;move ship left
If s\x<=10 Then s\x=10 ;stop ship from going off screen

If KeyDown(205) Then s\x=s\x+2 ;move ship right
If s\x>=610 Then s\x=610;stop ship from going off screen

If KeyHit(57) Then    ;if space is pushed then shoot laser
   l.laser=New laser   
        l\x=s\x
        l\y=s\y+2
EndIf

DrawImage ship,s\x,s\y

End Function

   

;---------------
;  Laser Update-
;---------------
Function LaserUpdate()
  For l.laser=Each laser   
        l\y=l\y-5
   
      If l\y<0 Then  ;if laser goes off screen delete it
         
           Delete l
     
      Else

           DrawImage laser,l\x,l\y
     
     EndIf
             
  Next
End Function

;-----------------
;  Alien Update  -
;-----------------
Function UpdateAlien()
For zz.alien = Each alien
zz\frame=0


Delay 4
If zz\frame>9 Then zz\frame=0
If zz\frame<0 Then zz\frame=9

DrawImage alien,zz\x,zz\y,zz\frame
Next

End Function

;--------------------
;  Collision Update -
;--------------------
Function Update_Collision()

For ll.laser=Each laser   
    For aa.alien=Each alien
   
        If ImagesCollide(laser,ll\x,ll\y,0, alien,aa\x,aa\y,0)
          Delete ll
  Delete aa
        EndIf
       
Next     
     Next
 
   

End Function

In future, if you want to post a question that you need help for and that program needs external files, you can attach them to your post.
Make sure that you include them all in a zip file and click on additional options in the new post window and you can browse for attachments.
Shockwave ^ Codigos
Challenge Trophies Won: