Collision Detection works only with two objects

57 views Asked by At

I've been trying to write a collision detection program and it's not always detecting a collision. I've found that when I have two objects the collisions work fine when I only have one object detecting the other. For example if I have objects blue and red the program will work fine with blue->collisionManager(red); . Since I want to add more objects I'm running a 2d array for now that looks like

for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
    if (arr[i] != arr[j]) {
        arr[i]->collisionManager(arr[j]);
    }
}
arr[i]->checkWall();

}

So what I think is going on is that one object is detecting a collision from the other but when the other detects the collision it sort of negates the whole process. My idea is to put a boolean to tell the other object that the first is already in a collision. But I don't know how to implement this boolean in my code. This is my collision manager and detection code

    void platform::collisionManager(platform* other) {  
    if (this->checkCollision(other)) {
        this->anglelessCollision(other);
    }
}

bool platform::checkCollision(platform* square)
{
    bool AisToTheRightOfB = this->getLeft() > square->getRight();
    bool AisToTheLeftOfB = this->getRight() < square->getLeft();
    bool AisAboveB = this->getBottom() < square->getTop();
    bool AisBelowB = this->getTop() > square->getBottom();
    return !(AisToTheRightOfB
        || AisToTheLeftOfB
        || AisAboveB
        || AisBelowB);
    
}
0

There are 0 answers