collisions optimization

BlitzMax Forums/BlitzMax Beginners Area/collisions optimization

Looks like you are trying to use AABB ( Axis-aligned Bounding Boxes)
Check this out.
Type AABB
	Field MinX:Int
	Field MinY:Int
	Field MaxX:Int
	Field MaxY:Int
End Type

Function TestAABBAABB:Int( a:AABB , b:AABB)
	If a.MaxX < b.minX Or a.MinX > b.MaxX Return 0
	If a.MaxY < b.minY Or a.MinY > b.MaxY Return 0
	Return 1
End Function

Test the two AABB and if returns 1(true) check pixelPerfect.
The problem with that is that you will have to reposition in every frame the AABB Fields.
For example if the handle of you picture is at the middle of the image and the position of the image is at x , y.
MinX = x - ImageWidth/2 , MinY = y - ImageHeight/2 , MaxX = x + imageWidth/2 , MaxY = y + imageHeight/2.
The best is to store somewhere the ImageWidth/2 and ImageHeight/2.
The problem with this is that you will have to check for collisions for all your objects in the game. For example if you have in the level 300 enemies you will have to run 300 times the TestAABBAABB and to update all AABB.
One good solution (I haven't checked this yet) is to split your map in AABBs. Put a byte in your enemies’ types that will tell them if they are going to be Active.
Then you will have active only the enemies of the AABB that you are in and the neighbours of it. Also it's good to have a (pointer) of the AABB that your enemies are belongs to before you make them active. After that if an enemy goes beyond the neighbours AABB change the pointer to the current AABB and deactivate him.
I believe that this will speed up your game. Just make a test with a small map to see how it’s going to look like.