Hey everyone first time poster here, but love working with SFML. Anyways I have been reading the SFML book and found it quite good and it has it been very informative though I am stuck on the collision detection aspect in the book.
Which brings me to my problem. I have been trying to debug a collision detection error for a while now and just can't seem to find out what it is. I have a feeling it is something little I am missing.
But was hoping maybe someone more experienced with SFML then me can help me out. I'll post what I have been able to find out and the code related to the bug (If you need more just let me know and I can provide whatever is needed).
The bug seems to be that the game isn't detecting when the collision of 2 bounding boxes collide. The collision function never evalutes to true even when it should.
Collision Helper Function - From the SFML book used to help find if there is a collision between scene nodes.
bool collision(const SceneNode& lhs, const SceneNode& rhs)
{
return lhs.getBoundingRect().intersects(rhs.getBoundingRect());
}
checkNodeCollision - Basically it does a few optimizations as far as I can tell and also tests for the collision with the helper function.
void SceneNode::checkNodeCollision(SceneNode& node, std::set<Pair>& collisionPairs)
{
if (this != &node && collision(*this, node) && !isDestroyed() && !node.isDestroyed())
collisionPairs.insert(std::minmax(this, &node));
for (Ptr& child : children)
child->checkNodeCollision(node, collisionPairs);
}
Checks the collision for the whole scene graph.
void SceneNode::checkSceneCollision(SceneNode& sceneGraph, std::set<Pair>& collisionPairs)
{
checkNodeCollision(sceneGraph, collisionPairs);
for (Ptr& child : sceneGraph.children)
checkNodeCollision(*child, collisionPairs);
}
This is the base bounding rectangle for each scene node. By default it has no bounding rectangle
sf::FloatRect SceneNode::getBoundingRect() const
{
return sf::FloatRect();
}
Then all other entities in the game (Like the ships, projectiles, ect) have this overload
sf::FloatRect Aircraft::getBoundingRect() const
{
return getWorldTransform().transformRect(sprite.getGlobalBounds());
}
I believe that is all the code that deals with the actual collision detection. The handling of the collision I believe is fine (Though can't test it since no collisions register yet). IF more is needed I can post whatever is requested.
Now I also am displaying the bounding boxes on screen with a draw function so I don't believe they are the problem (Though I could be wrong I am quite new with SFML). Also have put cout statements in the collision function and it never tests true for any collision. So that leads me to think something is wrong with how I am navigating the scene graph but am not sure.
Also I am currently getting rid of entities that leave the screen using a collision detection system which works (Which is what gets me confused). That method looks like this.
void World::destroyEntitiesOutsideView()
{
Command command;
command.category = Category::Projectile | Category::EnemyAircraft;
command.action = derivedAction<Entity>(
[this] (Entity& entity, sf::Time)
{
[b]if (!getBattlefieldBounds().intersects(entity.getBoundingRect()))
{
entity.destroy();
}[/b]
});
commandQueue.push(command);
}
// Return a rectangle of what is currently in view
sf::FloatRect World::getBattlefieldBounds() const
{
return sf::FloatRect(worldView.getCenter() - worldView.getSize() / 2.f, worldView.getSize());
}
So when entities go off screen they are destroyed right away since they are not in the rectangle of battleBounds which is a tiny bit bigger then the window. So I am not really sure why that works and the others don't.
I'm not really sure what other information would help with this one so if there is anything else I am not mentioning feel free to ask. Also thank you so much in advance for any advice you can give. I'm not so much looking for a solution to the problem more along the lines of learning what I did wrong and how I can fix it.