image collision/deletion problem

Blitz3D Forums/Blitz3D Beginners Area/image collision/deletion problem

767pilot(Posted 2005) [#1]
please see above file

the problem i have is that the bullet goes upwards yet when it hits the alien the 'imagecollision' command somehow doesnt recognise the hit until the bullet is past the alien.

any ideas why this is please?

thanks
graham


Ross C(Posted 2005) [#2]
Hmmm, i've had a look see. This bit seems to be a bit odd. You using variables to offset the positioning of the bullets and aliens. now:

	For enemy.enemy=Each enemy
		If enemy\alive <>0 Then DrawImage (enemy\image,enemy\x+boardxstart,enemy\y+boardystart,enemy\imagehandle)
	Next
	
	;draw bullets
	For bullet.bullet=Each bullet
		If bullet\alive <>0 Then DrawImage (bullet\image,bullet\x+boardxstart,bullet\y+tilewidth,bullet\imagehandle)
		Rect bullet\x+boardxstart,bullet\y+tilewidth,1,1
	Next


you use "tilewidth" to position the Y of the bullets, BUT, you use boardYstart to position the aliens. Now, your basically drawing them in one position and checking the collisions is different positions.

Basically, it's the draw bullets line that mucking it up. It should be:

	;draw bullets
	For bullet.bullet=Each bullet
		If bullet\alive <>0 Then DrawImage (bullet\image,bullet\x+boardxstart,bullet\y+boardystart,bullet\imagehandle)
	Next


Note, the change is using the variable "boardystart" to position the bullet on the Y axis. :o)


Ross C(Posted 2005) [#3]
Oh, also, i notice you aren't actually deleting the bullets once they've hit something. Add in:

			If bullet\alive = 0 Then
				Delete bullet.bullet
			End If


In the "PlayerCollisions()" function, just below where you check the Y position of the bullet. :o) To make the function read:

Function PlayerCollisions()

	;Reset the collisions variables
	playerup = False
	playerleft = False
	playerright=False
	playerdown = False
	;Start Collision Detection
	;solid
	For x = 0 To mapsizex
		For y = 0 To mapsizey
			If map(0,x,y)>0 Then;if there is something occupying the current x,y position in the Map array
				If ImagesCollide(player\image ,player\x+collisioncheck ,player\y ,player\imagehandle ,gfx_tiles,x*32,y*32,map(0,x,y)) Then playerright = True ;IF the player collided with the current tile on his right
				If ImagesCollide(player\image ,player\x-collisioncheck ,player\y ,player\imagehandle ,gfx_tiles,x*32,y*32,map(0,x,y)) Then playerleft = True ;IF the player collided with the current tile on his left
				If ImagesCollide(player\image ,player\x ,player\y+collisioncheck ,player\imagehandle ,gfx_tiles,x*32,y*32,map(0,x,y)) Then playerdown = True;IF the player collided with the current tile below him
				If ImagesCollide(player\image ,player\x ,player\y-collisioncheck ,player\imagehandle ,gfx_tiles,x*32,y*32,map(0,x,y)) Then playerup = True;IF the player collided with the current tile Above Him
			EndIf
		Next
	Next
	
	;check collision for the pickups
	For pickup.pickup=Each pickup
	
		If pickup\alive =1 Then
			If ImagesCollide(player\image,player\x,player\y,player\imagehandle ,pickup\image ,pickup\x,pickup\y ,pickup\imagehandle ) Then
				pickup\alive =0 ;remove the pickup
				pickupid=pickupid-1
				player\score=player\score+10	
			EndIf
		End If

	Next
	
	Local bullet_dead = 0
	
		;check collision for the bullets
	For bullet.bullet=Each bullet
	
		If bullet\alive =1 Then
		
			bullet\y =bullet\y -5 ;move the bullet upwards if not hit anything
			
			For enemy.enemy=Each enemy
			

				If enemy\alive =1 Then
				
					If ImagesCollide(enemy\image,enemy\x,enemy\y,enemy\imagehandle ,bullet\image ,bullet\x,bullet\y ,bullet\imagehandle ) Then

						enemy\alive =0 ;remove the enemy
						bullet\alive =0 ; remove the bullet
						player\score=player\score+10	
					
					End If
					
				EndIf
			
			Next

			If bullet\y=<0 Then
				;if bullet off top of screen then delete and dont show
				bullet\alive =0
			End If

			If bullet\alive = 0 Then
				Delete bullet.bullet
			End If
		
		End If
		
	Next

End Function


I also moved the part where you move the bullets, to before the collision check is done, to make sure it doesn't miss.


767pilot(Posted 2005) [#4]
that works great, thanks for your time and help

graham