Issue With Collision Detection

So, for some reason when I try to add more solid objects into the scene I am only able to to collide with one of those objects and not the other ones.

Code for handling collision:

public void update() {
        for (int i = 0; i < objects.size(); i++)
            for (int j = i + 1; j < objects.size(); j++) {

                ArrayList<AABB> boxes = new ArrayList<AABB>();

               GameObject c0 = objects.get(i);
                GameObject c1 = objects.get(j);

                boxes.add(c1.getBoundingBox());

                objects.clear();



                for (int k = 0; k < boxes.size(); k++) {
                    AABB box = null;
                    if (box == null) box = boxes.get(k);
                    if (boxes.get(k) != null) {
                        Vector2F length1 = box.getCenter().sub(c1.getPosition().x, c1.getPosition().y, new Vector2F());
                        Vector2F length2 = boxes.get(k).getCenter().sub(c0.getPosition().x, c0.getPosition().y, new Vector2F());
                        if (length1.lengthSquared() > length2.lengthSquared()) box = boxes.get(k);
                    }

                    if (box != null) {
                        Collision data = c0.getBoundingBox().getCollision(box);
                        if (data.isIntersecting) {
                            c0.getBoundingBox().correctPosition(box, data);
                            c0.setPosition(c0.getBoundingBox().getCenter());
                        }
                    }
                }
            }
    }

You clear the objects array after retrieving two objects (why?). Now objects.size() returns 0 and your for loops terminate.

edit: retrieving isn’t the right word, get(i) just returns the object at index i without removing it from the array.

… now I feel completely stupid for missing that. It’s working now thanks!

Don’t worry, we’ve all done (well, hopefully not just me at least) have done some spectacularly stupid shit while learning…

A recent example; for weeks (maybe 5 hours per week) I had an issue in the project I’m working on where for whatever reason looping through an array of vectors I could not figure out how I could go from the end of the array back to the beginning flawlessly, but then from 0 to the end of the array would get to 2 points from the end before reversing direction… then finally, I spotted an unrelated post about how when you use = with arrays / vectors rather than .get or .set or .add, that you are simply copying the reference to the data, and not attributing the data to the variable as you would with .set… changed an = to a .addall() and everything worked, I think part of it was having to be explicit in C++ with the & and *… regardless, felt pretty dumb.

Well, now I’m having another issue. In my game object manager class I have a comparator that sorts the objects depending on their y axis allowing objects to appear behind or infront of an object, and for some reason this completely breaks the collision. I am able to slide through the bottom parts of the solid object

There’s not quite enough code there to say with any certainty, however, I went through the similar problem when building collision detection routines…

It might be something to do with how you are trying to calculate depth, alternatively, it might be an issue where your collision detection is determining the collisions where the object was, not where the object will be at the end of the frame.

If it’s the latter, the way I had resolved that was :

  • determine the speed vector for where the object will move over the next frame.
  • draw a line from each point of the moving object
  • determine the point of collision point with that
  • use the shortest line with a collision as the point that is collided
  • reduce speed appropriately so it stops at that point.

If its the former, I would need more information to provide anything useful.