Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Difficulty with wall collision (FIXED)  (Read 470 times)

0 Members and 1 Guest are viewing this topic.

nsteiner25

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Difficulty with wall collision (FIXED)
« on: August 05, 2023, 05:37:30 pm »
In my game I have enemies that move down the screen, and if they hit a wall, the hp of that wall goes down and the enemy is deleted. Once the hp of the wall is 0, the wall is deleted. Everything seems to work fine until around the 5th or 6th wall is deleted. There are three rows of walls stacked in a brick-like formation, and when one of the walls in the second or third row is deleted and there still exists a wall in a row above it, that one above it is also deleted.

Here is my code:

void Wall::updateWalls(std::vector<Wall>& walls, std::vector<Enemy*>& enemies){
    for (size_t j = 0; j < enemies.size(); j++) {
   
        bool wallErased = false;

        size_t i;
        for (i = 0; i < walls.size(); i++) {
            if (walls.wall.getGlobalBounds().intersects(enemies[j]->enemySprite.getGlobalBounds())) {
                enemies.erase(enemies.begin() + j);
                walls.hp--;

                if (walls.hp <= 0) {
                    wallErased = true;
                    break;
                }
            }
        }
        if (wallErased) {
            walls.erase(walls.begin() + i);
            i--;
        }
    }
}
« Last Edit: August 08, 2023, 12:36:04 am by nsteiner25 »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Difficulty with wall collision
« Reply #1 on: August 06, 2023, 10:01:56 pm »
The logic doesn't seem to make complete sense to me.

- Why are you performing i--? This is a local variable that does nothing after this.
- You are not checking against all walls? You are checking walls until you find one that should be erased.

I presume that i-- is due to the concept of erasing during a loop so be aware that you may actually need this when you remove inside the loop but, at the moment, it does nothing since you erase outside of the wall loop.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

nsteiner25

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Difficulty with wall collision
« Reply #2 on: August 08, 2023, 12:27:34 am »
tbh I put my original function into chatGPT and got that, but it had the same issue so I just left it

here's my new function, but it still does the same thing:

for (size_t j = 0; j < enemies.size(); j++) {
        size_t i;
        for (i = 0; i < walls.size(); i++) {
            if (walls->wall.getGlobalBounds().intersects(enemies[j]->enemySprite.getGlobalBounds())) {
                delete enemies[j];
                enemies.erase(enemies.begin() + j);
                walls->hp--;

                if (walls->hp <= 0) {
                    delete walls;
                    walls.erase(walls.begin() + i);
                }
            }
        }
    }

side note, the walls are now pointers

nsteiner25

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Difficulty with wall collision (FIXED)
« Reply #3 on: August 08, 2023, 12:35:13 am »
Update: I fixed it, it had nothing to do with this function, but with how I was printing the walls

Also, this is my first time posting to this board, is there something I am supposed to do when my problem is solved

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: Difficulty with wall collision (FIXED)
« Reply #4 on: August 08, 2023, 09:50:43 pm »
Nothing, you can update the title with fixed or solved like you did but there's nothing more :)