Domanda

I'm a beginner with SFML and I'd like to learn about collisions.. I made the game class, the entity class and a entity manager to keep things right and I made a collision function to detect collision between 2 objects, but my question is : How to check collision against every object in the scene? I mean.. I have a Player class derived from Entity and I want to test if it collides with every object in the scene that is an entity but not a Player, can you help me?

Entity.h

#ifndef ENTITY_H_INCLUDED
#define ENTITY_H_INCLUDED
class Entity {
    public:
        Entity();
        ~Entity();
        virtual void Draw(sf::RenderWindow& mWindow);
        virtual void Update();
        virtual void Load(std::string file);
        virtual sf::Sprite GetEntity();
        bool IsLoaded();
        bool mIsLoaded;
        std::string mFile;
        sf::Texture mTexture;
        sf::Sprite mSprite;
};
#endif

EntityManager.h

#ifndef ENTITYMANAGER_H_INCLUDED
#define ENTITYMANAGER_H_INCLUDED
#include "Entity.h"
class EntityManager {
    public:
        void Add(std::string name, Entity* entity);
        void Remove(std::string name);
        Entity* Get(std::string name) const;
        int GetEntityCount() const;
        void DrawAll(sf::RenderWindow& mWindow);
        void UpdateAll();

    private:
        std::map <std::string, Entity*> mEntityContainer;
};
#endif

PlayerPlane.h

#ifndef PLAYERPLANE_H_INCLUDED
#define PLAYERPLANE_H_INCLUDED
#include "Entity.h"
class PlayerPlane : public Entity {
    public:
        PlayerPlane();
        ~PlayerPlane();
        void Update();
        void Draw(sf::RenderWindow& mWindow);
};
#endif

Game.h

#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
#include "EntityManager.h"

class Game {
    public:
        static void Run();
        static void GameLoop();

    private:
        static sf::RenderWindow mWindow;
        static EntityManager mEntityManager;
};
#endif

I hope someone will understand what I meant and give some advice or examples..

È stato utile?

Soluzione

What you can do is loop through all you entities and check that it is not the player, then check if there's a collision.

I guess you are creating you PlayerPlane object somewhere in your Game class, then you should maybe save a pointer to it because it's a special entity in your game.

Then you can just do in your GameLoop :

for (std::<std::string, Entity*>::iterator it = mEntityContainer.begin(); it != mEntityContainer.end(); ++it)
{
    if (it->second != pointerToPlayer)
    {
        checkCollision(it->second, pointerToPlayer);
    }
}

Or, more concisely in C++11 (-std=c+11 for gcc and clang, supported by default since VS2012) :

for (const auto& entity : mEntityContainer)
{
    if (entity.second != pointerToPlayer)
    {
        checkCollision(it.second, pointerToPlayer);
    }
}

Another idea is to verify in your collision function that the two entities passed as parameters do not have the same address (being different objects).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top