In regards to some of the code above, something to bear in mind with Blitz3D is that it executes the code supplied as a parameter for the 'For' command at the start of each loop iteration. By using:-
For c = 1 To CountCollisions(p\piv)
you're actually performing the collision count during each loop iteration. It's better to cache the result to a variable and use the variable in the loop instead.
eg.
num_collisions = CountCollisions(p\piv)
For c = 1 To num_collisions
For c = 1 To CountCollisions(p\piv)
you're actually performing the collision count during each loop iteration. It's better to cache the result to a variable and use the variable in the loop instead.
eg.
num_collisions = CountCollisions(p\piv)
For c = 1 To num_collisions