2D Collision Detection in C++ I'm attempting to rewrite the classic snake game in c++. What I am inquiring about is my implementation for a 2D collision detection function. I am utilizing SFML to create the window and shapes. The two objects that I am checking are squares of the same size. I would really appreciate any feedback that is given, as I'm still a beginner when it comes to c++. 

This function works as intended. I'm looking for performance improvements, and general c++ practices that I should follow. If I need to add any more code, please let me know.

Game.h

#include &lt;SFML/Graphics.hpp&gt;
#include &lt;SFML/System.hpp&gt;
#include &lt;SFML/Window.hpp&gt;
#include &lt;SFML/Audio.hpp&gt;
#include &lt;SFML/Network.hpp&gt;

class Game {

    // Some code omitted for brevity //

    private:
        sf::RectangleShape enemy;
        sf::RectangleShape player;

    public:
        const bool isColliding(sf::RectangleShape player, sf::RectangleShape enemy) const;

    // Some code omitted for brevity //

};


Game.cpp

/*
    Determines if the player is colliding with the enemy.

    @param sf::RectangleShape player - Player object.
    @param sf::RectangleShape enemy - Enemy object.

    @return bool - True if colliding, False otherwise.
*/
const bool Game::isColliding(sf::RectangleShape player, sf::RectangleShape enemy) const {

    sf::Vector2f playerPosition = this-&gt;player.getPosition();
    sf::Vector2f enemyPosition = this-&gt;enemy.getPosition();

    float playerWidth = this-&gt;player.getSize().x;
    float playerHeight = this-&gt;player.getSize().y;

    return (
        ((playerPosition.x + playerWidth) &gt; enemyPosition.x &amp;&amp; (playerPosition.x - playerWidth) &lt; enemyPosition.x) &amp;&amp;
        ((playerPosition.y + playerHeight) &gt; enemyPosition.y &amp;&amp; (playerPosition.y - playerHeight) &lt; enemyPosition.y)
    );

}
const bool Game::isColliding(...

This indicates that the returned type is const, and should not be modified by any part of your code. But it's not a reference, so it doesn't make sense. It should compile, but it's a little confusing. Removing that const is clearer.

see this SO question for more information on that.



Your parameters to isColliding should be const references:

bool Game::isColliding(const sf::RectangleShape&amp; player, const sf::RectangleShape&amp; enemy) const


because you don't modify them, and references will prevent each object form being copied.



You never use the player parameter that you pass to the isColliding function. this-&gt;player is not the same as player in this context. It seems like you want to check the Game's current player object against collisions with arbitrary objects. You either


Make the function static (so it doesn't rely on a particular Game state) and pass two arbitrary objects to be checked for collision, or
Change the function name to something that indicates it will only be used with the player object.


One of:

static bool Game::isColliding(const sf::RectangleShape&amp; a, const sf::RectangleShape&amp; b)


or

bool Game::isPlayerColliding(const sf::RectangleShape&amp; other) const




These Vector2d assignments can be made into const references to prevent copy construction.

    const sf::Vector2f&amp; playerPosition = this-&gt;player.getPosition();
    const sf::Vector2f&amp; enemyPosition = this-&gt;enemy.getPosition();