Optimizing Code

Blitz3D Forums/Blitz3D Beginners Area/Optimizing Code

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

The drawing will be the most time consumeing part...followed by the collision detection.

As you are doing a shooter, the design of the game itself can lead to some ways to optimize the collision routine.

as the player shots are most likely traveling from the left to the right, you can construct a grouping routine to define a rectangle of screen space that the player bullets occupy...

for example, something like this:

;start of main loop

Bull_max_X = 0
Bull_max_Y = 0
Bull_min_X = screenwidth
Bull_min_Y = screenhieght

For b.bullet = each bullet

  b\x = b\x + 3

   if b\x > Bull_max_X then Bull_max_X = b\x
   if b\y > bull_max_y then Bull_max_y = b\y
   if b\x > Bull_min_X then Bull_min_X = b\x
   if b\y > bull_min_y then Bull_min_y = b\y

Next


For e.enemy = each enemy

  if e\x > Bull_min_X and e\x < Bull_max_X
    if e\y > bull_min_Y and e\Y < Bull_max_Y

       ;do collisions
    end if
  end if

next



One more thing you may want to consider is to roll your own collision routines and not use the Blitz supplied ones...function calls inside nested loops can be much slower then pure code inside the same loop