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 ...
Get SFML Game Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.