i added new video for latest update, after implemented sprite masking and pixel collision detection.
here snippet from sprite masking by using sf::RenderTexture and sf::Image
void Shield::updateCurrent(sf::Time dt, CommandQueue& commands)
{
if (!mOnHit)
return;
mRenderTexture.clear();
mRenderTexture.draw(*this, sf::BlendNone);
mRenderTexture.display();
float radius = (mRectOnHit.height > ExplosionRadius) ? mRectOnHit.height : ExplosionRadius;
sf::CircleShape circle(radius, 10);
circle.setPosition(mPositionOnHit.x, mPositionOnHit.y + circle.getRadius() / 2.f * mSign);
circle.setFillColor(sf::Color::Transparent);
centerOrigin(circle);
mRenderTexture.draw(circle, sf::BlendNone);
mRenderTexture.display();
sf::Vector2u position(static_cast<std::size_t>(getPosition().x - mTexture.getSize().x / 2u), static_cast<std::size_t>(getPosition().y - mTexture.getSize().y / 2u));
mImage.copy(mRenderTexture.getTexture().copyToImage(), 0u, 0u, sf::IntRect(position.x, position.y, mTexture.getSize().x, mTexture.getSize().y));
mImage.createMaskFromColor(sf::Color::Transparent);
updateSprite();
}
and here the pixel collision detection for bullets:
bool PixelcollidesPair(const Shield& shield, const Projectile& bullet)
{
auto bulletBounds = static_cast<sf::Rect<std::size_t>>(bullet.getBoundingRect());
auto shieldBounds = static_cast<sf::Rect<std::size_t>>(shield.getBoundingRect());
auto width = bulletBounds.left + bulletBounds.width;
auto height = bulletBounds.top + bulletBounds.height;
sf::Vector2u position(bulletBounds.left, bulletBounds.top);
if (!bulletBounds.intersects(shieldBounds))
return false;
for (auto x = position.x; x < width; ++x)
{
for (auto y = position.y; y < height; ++y)
{
auto relX = x - shieldBounds.left;
auto relY = (bullet.getCategory() & Category::PlayerProjectile) ? y - shieldBounds.top : y - shieldBounds.top - bulletBounds.height;
if (shield.getPixel(relX, relY))
return true;
}
}
return false;
}