I'm in the process of developing a Javascript game and I'm facing the challenge of implementing collision detection.
Each element rendered in the game has its own unique ID and is stored in an object. I opted for using an object as a container instead of an array because it allows easy removal of children. In the case of a bullet hitting a ship, the object ensures that the specific element can be removed without having to shift all other elements like in an array. Additionally, having IDs for tracking purposes makes management more efficient.
When attempting to identify pairs within an array, I can follow this approach:
var a = [];
//Object container
for(var i = 0; i < a.lenght; i++){
for(var j = i+1; j < a.lenght; j++){
var object1 = a[i];
var object2 = a[j];
//collision detection
}
}
However, when dealing with an 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 method seems less elegant compared to working with arrays. Is there a more effective solution available?