Help me optimize collision code.

Blitz3D Forums/Blitz3D Beginners Area/Help me optimize collision code.

Phantom1029(Posted 2004) [#1]
This code needs to be optimized, wanna help? I think I can make it more efficient but I'm having coder's block or something :(


For Enemy.Enemy=Each Enemy
		For b.bullet = Each bullet 
			delete_the_b=False
			If b\Bullet_Type <> TURRETBULLET And b\Bullet_Type <> BIGBULLET
				If (ImagesOverlap(b\img,b\x,b\y,Enemy\img,Enemy\x,Enemy\y))
					Enemy\health=Enemy\health-b\dmg
						If b\Bullet_Type=BULLET Or b\Bullet_Type=SHORT_BULLET
							DrawImage b_damage, b\x-10,b\y-10,Rand(0,2)
						End If 
						If b\Bullet_Type=MISSILE And Enemy\health > 0 
							For i=0 To 3
								explosion.explosion=New explosion
								explosion\Explosion_Type=DEATH
								explosion\x=b\x
								explosion\y=b\y
								explosion\xv=Rand(-6+b\xv,1)
								explosion\yv=Rand(2,5)
								explosion\img=exp_img
								explosion\frm=0
								explosion\tmr=Frame_Count
							Next
						End If 
						If b\Bullet_Type <> LASER1 And b\Bullet_Type <> LASER2 And b\Bullet_Type<>BIGBULLET
							Delete_the_b=True
						End If 	
				End If  
			End If 
			If b\Bullet_Type=BIGBULLET
				If (ImagesCollide(b\img,b\x,b\y,b\frm,Enemy\img,Enemy\x,Enemy\y,Enemy\frm))
					Enemy\health=Enemy\health-b\dmg
				End If 
			End If 
			If Delete_the_b=True
				Delete b
			End If 
		Next
	Next



There is some more if anyone is interested.


Perturbatio(Posted 2004) [#2]
a start would be to use SELECT instead of all the IF's

i.e.
Select b\Bullet_Type
   case BIGBULLET
      ;do big bullet code
   case TURRETBULLET
      ;do turretbullet code
   default
      ;do standard bullet code
end select



WolRon(Posted 2004) [#3]
If b\Bullet_Type <> LASER1 And b\Bullet_Type <> LASER2 And b\Bullet_Type<>BIGBULLET
	Delete_the_b=True
End If
No point in checking if the bullet_type is <> BIGBULLET because you already checked that way up on top. So should read:
If b\Bullet_Type <> LASER1 And b\Bullet_Type <> LASER2
	Delete_the_b=True
End If



WolRon(Posted 2004) [#4]
No need to check if bullet_type is <> BIGBULLET and then later check if bullet_type is = BIGBULLET. Might as well rearrange to reduce a check.
For Enemy.Enemy=Each Enemy
	For b.bullet = Each bullet 
		delete_the_b=False
		If b\Bullet_Type=BIGBULLET
			If (ImagesCollide(b\img,b\x,b\y,b\frm,Enemy\img,Enemy\x,Enemy\y,Enemy\frm))
				Enemy\health=Enemy\health-b\dmg
			End If 
		ElseIf b\Bullet_Type <> TURRETBULLET
			If (ImagesOverlap(b\img,b\x,b\y,Enemy\img,Enemy\x,Enemy\y))
				Enemy\health=Enemy\health-b\dmg
				If b\Bullet_Type=BULLET Or b\Bullet_Type=SHORT_BULLET
					DrawImage b_damage, b\x-10,b\y-10,Rand(0,2)
				End If 
				If b\Bullet_Type=MISSILE And Enemy\health > 0 
					For i=0 To 3
						explosion.explosion=New explosion
						explosion\Explosion_Type=DEATH
						explosion\x=b\x
						explosion\y=b\y
						explosion\xv=Rand(-6+b\xv,1)
						explosion\yv=Rand(2,5)
						explosion\img=exp_img
						explosion\frm=0
						explosion\tmr=Frame_Count
					Next
				End If 
				If b\Bullet_Type <> LASER1 And b\Bullet_Type <> LASER2
					Delete_the_b=True
				End If 	
			End If  
		End If 
		If Delete_the_b=True
			Delete b
		End If 
	Next
Next