Actually I use p5.js, but I suppose the js built in functions should also work in the same way as in javascript.
Given is a list of dynamically generated objects stored in an array. The objects have coordinates (x,y), size, and some other attributes. These objects are moving over the screen according to their attributes (direction, velocity)
I want to write a function, where I can check the coordinates of objects, if they collide, they should change directions e.g. for this I need to compare every element of my array with every other.
As I have a number of elements I don't know, I iterate with
for (let i in array)
then I want to pop the first element, and in the included for each loop compare it with the rest of array.
After this loop, I want to unshift this element to the array for doing the same with the whole array.
I suppose, that the built in function of pop() doesn't return me this element, because I get an error, which let me think, that the objects of this array are changed with something else.
How can I fix that?
Here is my code of this function:
function checkCollisions() {
for (let i in array_of_objects) {
let element = array_of_objects.pop()
for (let j in array_of_objects) {
if (comparison_of_some_attributes_between(element, array_of_objects[j])) {
some_changes_on_attributes_of(element, array_of_objects[j])
}
array_of_objects.unshift(element)
}
}