Collision detection and response
Now that our world is full of entities, let's implement interactions between them. Most interactions occur in the form of a collision; two airplanes collide and explode, projectiles of the player's Gatling gun perforate an enemy, and a pickup is collected by the player, and so on.
First, we write a function that computes the
bounding rectangle of an entity. This is the smallest possible rectangle that completely contains the entity. As such, it represents an approximation of the entity's shape, which makes computations simpler. Here is an example implementation: getWorldTransform()
multiplies the sf::Transform
objects from the scene root to the leaf. sf::Transform::transformRect()
transforms a rectangle, and may enlarge it if there is a rotation (since the rectangle has to remain axis-aligned). sf::Sprite::getGlobalBounds()
returns the sprite's bounding rectangle relative to the aircraft.
sf::FloatRect Aircraft::getBoundingRect() const { return getWorldTransform...