Skip to content Skip to sidebar Skip to footer

Collision Detection/remove Object From Arraylist

I am currently trying to test collision between a falling object and a box. I understand basic collision detection, but my problem here is that I have to test it for an indefinite

Solution 1:

One way you could do this is to have a field on the Blossom that indicates whether it is active or not, then only draw it if it is active. If it is inactive, another Blossom could replace it in the list.

Solution 2:

Setting a visibility flag is one way to go, however I'd recommend against it since you are adding an indeterminate amount of Bitmaps to an ArrayList...you'll find yourself running out of memory pretty quickly. Change your collision detection iterator from a foreach to a handwritten loop, this will avoid concurrency issues you may run to in the code you have listed above.

for (int i = 0; i < blossomArrayList.size(); i++)
            {
                if(blossom.hit(box_x,box_y, box_x + boxWidth, box_y + boxHeight)) {
                    blossomArrayList.remove(i);
                }
            }

Additionally, I'd recommend changing all your ArrayList foreach iterators to hand written for loops, as iterating ArrayLists (but not any other object) is relatively slow on Android and can lead to unexpected concurrency issues.

Thirdly, it seems as though you only need to run your Collision() method once after your UpdatePositions loop has completed since you're already checking every Blossom in your Collision() method.

Post a Comment for "Collision Detection/remove Object From Arraylist"