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

Author Topic: Collsion with multiple bullets and multiple enemies  (Read 2350 times)

0 Members and 2 Guests are viewing this topic.

Tim0n

  • Newbie
  • *
  • Posts: 5
    • View Profile
Collsion with multiple bullets and multiple enemies
« on: September 13, 2014, 01:22:40 pm »
Hello! I'm trying to do a simple shoot 'em up game, but I've run into a problem.

What I'm trying to do is collision between bullets and enemies (invaders). With one bullet class, one invader class and one Collision class. I've gotten the collision to "kind of" work, meaning that one bullet can kill invaders, the problem I'm facing right now is, how do you make it so all the bullets can hit the invaders? I've tried returning a reference to the bullet vector, a pointer to the vector, the vector, the sprites in the vector and so on. I have identified the error in the code below, the problem is that the i value dosen't increase when I return the sprite.

Here's some example code.


Bullet.cpp

sf::Sprite Bullet::BulletPosition(){

        for (int i = 0; i < bullets_straight.size(); ++i){

                return bullets_straight[i];
        }
}

Collision.cpp

bool Collision::SpriteCollision(sf::Sprite _sprite1, sf::Sprite _sprite2){

        if (_sprite1.getPosition().x > _sprite2.getPosition().x || _sprite1.getPosition().x +8 < _sprite2.getPosition().x - 32
                || _sprite1.getPosition().y > _sprite2.getPosition().y + 32 || _sprite1.getPosition().y < _sprite2.getPosition().y - 32){
                return false;
        }
}

Invader.cpp

for (int i = 0; i < invaders.size(); ++i){


                if (SpriteCollision(invaders[i], _sprite)){
                        invaders.erase(invaders.begin() + i);
                        check_direction.erase(check_direction.begin() + i);
                }



        }
}

I  will make the collision better later on but right now I just, want to get it working.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10829
    • View Profile
    • development blog
    • Email
AW: Re: Collsion with multiple bullets and multiple enemies
« Reply #1 on: September 13, 2014, 04:30:34 pm »
Bullet.cpp

sf::Sprite Bullet::BulletPosition(){

for (int i = 0; i < bullets_straight.size(); ++i){

return bullets_straight[i];
}
}
You might want to refresh your knowledge of C++ functions, especially what the keyword "return" does.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Tim0n

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: AW: Re: Collsion with multiple bullets and multiple enemies
« Reply #2 on: September 13, 2014, 10:59:10 pm »
You might want to refresh your knowledge of C++ functions, especially what the keyword "return" does.

Yea thank you for that tip! Read up some about functions and so on, and yea.. When return is called the whole function is stopped. But haven't really found out much else on how to solve my problem. But I hope you can answer one more question from me! :D

From your experience, is it possible to use three classes, a bullet, invader and collision class, in which the bullet and the invader class use vectors to create and delete bullets and invaders. And make collision between them happen? If it is possible I will keep trying till I succeed! :)

(hope you understood my very messed up question)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10829
    • View Profile
    • development blog
    • Email
Re: Collsion with multiple bullets and multiple enemies
« Reply #3 on: September 13, 2014, 11:26:49 pm »
Well a question with "is it possible" and then talking about C++, the answer will mostly always be "yes", because you can do about anything you can imagine for an application in C++, the problems lay more in the details. ;)

Personally I wouldn't work with a collision class, since a collision is not a "thing".
What you probably would want to think of in more detail is: Where do your invaders and bullets "live" in?
One possible way might be to have something like a world class, which holds all the entities, including the invaders, the player and the bullets, and where you can easily check collisions between all the different entities.

I think you might find the SFML Game Development book very interesting, since it teaches exactly such designs and can give a deeper look into SFML itself.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything