#Collision detection not working properly

28 messages · Page 1 of 1 (latest)

granite atlas
#

Making a 2d shooting game with SD2 and C++, working on the unfunctional collision between the pellets from the gun and the balls.

The problem is the balls are not getting deleted upon collision

code is in the main one my github which is on my bio

odd pilotBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

dense vault
#

Please send the relevent code

#

We're all here helping for free :) please make it as easy as possible for us to help

granite atlas
#

basically this here :


int minX = 0;
int minY = 0;
int maxX = 0;
int maxY = 0;

// check for collisions and render balls
for (size_t i = 0; i < balls.size(); ++i) {
    Ball* ball = balls[i];

    minX = ball->ballX - 50;
    minY = ball->ballY - 50;
    maxX = ball->ballX + 50;
    maxY = ball->ballY + 50;

            //check if there is a collision
    if (gun->pelletX >= minX && gun->pelletX <= maxX && gun->pelletY >= minY && gun->pelletY <= maxY) {
        ball->isHit = true;
        std::cout << "hit!" << std::endl;
    }
    std::cout << "hit!" << std::endl;
    //render balls if there is no collision
    if (ball->isHit == false) {
        ball->render(game->getRenderer(), ballPositionX[i], ballPositionY[i]);
    }

}
obtuse fulcrum
#

Your ishit bool already marks them as deletable

granite atlas
#

"i shit"

#

lmao

granite atlas
dense vault
#

yeah i'd probably do an erase if loop here

obtuse fulcrum
#

So unless you care about their order you can just copy the back element into the deleted element and then call pop-back()

granite atlas
#

ok

obtuse fulcrum
#

In the update loop

granite atlas
#

i don't care about order

obtuse fulcrum
#

Then yeah

dense vault
#

wait

#

what is balls

#

is it a std::vector<std::unique_ptr<Ball>>

granite atlas
#

it's a std::vector<Ball*> balls;

obtuse fulcrum
#
for (int i = 0; i < objects.size(); ++i) {
  if (objects[i].is_dead()) {
    objects[i] = objects.back();
    objects.pop_back();
  }
}```
#

Smth like this

granite atlas
#

yeah this makes sense

#

i'll try

obtuse fulcrum
#

Are you using ball pointers for inheritance

granite atlas
#

yes

obtuse fulcrum
#

Oki