У меня есть танк , он вращается вокруг своей оси, мне нужно сделать так, чтобы он сталкивался с любым твердым объектом. Вот мои наработки:
void Tank::ActionOnCollision(const sf::FloatRect &collided_object)
{
{
const auto kTankGlobalBounds = sprite_.getGlobalBounds();
const auto kTankOrigin = sprite_.getOrigin();
#pragma region Коллизия слева
if (kTankGlobalBounds.top < collided_object.top
&& kTankGlobalBounds.top + kTankGlobalBounds.height < collided_object.top + collided_object.height
&& kTankGlobalBounds.left < collided_object.left + collided_object.width
&& kTankGlobalBounds.left + kTankGlobalBounds.width > collided_object.left)
{
current_split_speed_ = 0;
sprite_.setPosition(kTankGlobalBounds.left + kTankOrigin.x,
collided_object.top - kTankGlobalBounds.height + kTankOrigin.y);
}
#pragma endregion
#pragma region Коллизия слева-сверху
if (kTankGlobalBounds.top > collided_object.top
&& kTankGlobalBounds.top + kTankGlobalBounds.height > collided_object.top + collided_object.height
&& kTankGlobalBounds.left < collided_object.left + collided_object.width
&& kTankGlobalBounds.left + kTankGlobalBounds.width > collided_object.left)
{
current_split_speed_ = 0;
sprite_.setPosition(kTankGlobalBounds.left + kTankOrigin.x,
collided_object.top + collided_object.height + kTankOrigin.y);
}
#pragma endregion
#pragma region Коолизия снизу
if (kTankGlobalBounds.top < collided_object.top
&& kTankGlobalBounds.top + kTankGlobalBounds.height
< collided_object.top + collided_object.height
&& kTankGlobalBounds.left < collided_object.left + collided_object.width
&& kTankGlobalBounds.left + kTankGlobalBounds.width > collided_object.left)
{
current_split_speed_ = 0;
sprite_.setPosition(kTankGlobalBounds.left + kTankOrigin.x,
collided_object.top - collided_object.height + kTankOrigin.y);
}
#pragma endregion
#pragma region Коллизия сверху
if (kTankGlobalBounds.top > collided_object.top
&& kTankGlobalBounds.top + kTankGlobalBounds.height
> collided_object.top + collided_object.height
&& kTankGlobalBounds.left < collided_object.left + collided_object.width
&& kTankGlobalBounds.left + kTankGlobalBounds.width > collided_object.left)
{
current_split_speed_ = 0;
sprite_.setPosition(kTankGlobalBounds.left + kTankOrigin.x,
collided_object.top + kTankGlobalBounds.height + kTankOrigin.y);
}
#pragma endregion
}
}