JavaScript: find pairs from an object (not array) - collision detection -


i building game using javascript , need collision detection.

everything drawn in game has own id , stored in object. chose use object despite array container because easy delete children it. when example, bullet hits ship, removed object. array, of children have shifted when 1 object deleted. , nice have ids keep track of everything.

in order find pairs in array, can this:

var = []; //object container  for(var = 0; < a.lenght; i++){     for(var j = i+1; j < a.lenght; j++){         var object1 = a[i];         var object2 = a[j];         //collision detection     } } 

but object

var o = {}; //object container  var progress = [];  for(var key1 in o){     progress.push(key1);     for(var key2 in o){         if(progress.indexof(key2) === -1){             var object1 = a[i];             var object2 = a[j];             //collision detection         }     } } progress = []; 

this lot uglier array. there better way?

you use object.keys property names in object. it's array version:

var keys = object.keys(o);        // *** keys (var = 0; < keys.length; i++) {     (var j = + 1; j < keys.length; j++) {         var object1 = o[keys[i]]; // *** main change on         var object2 = o[keys[j]]; // *** these 2 lines         //collision detection     } } 

note object.keys lists enumerable, own properties. for-in loop visit enumerable properties, inherited ones. i'm guessing o doesn't have enumerable inherited properties.

you need object.keys when add or remove ship, not every time need collision detection.

you'll need profile determine whether using object , rebuilding keys array when change more or less efficient using array , splice.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -