void QChain::cutCircle(Circle circle) { if (m_vertices.size() == 0) return; circle.setCenter(circle.pos()); QPainterPath cr; cr.addEllipse(circle.x - circle.r, circle.y - circle.r, 2 * circle.r, 2 * circle.r); QPolygonF polygon; for (QPointF p : m_vertices) polygon.append(p); QPainterPath chain; chain.addPolygon(polygon); if (!chain.intersects(cr)) return; chain = chain.subtracted(cr); for (const QPolygonF &poly : chain.toSubpathPolygons()) { std::vector<Vector2d> pts(poly.begin(), poly.end() - 1); if (std::fabs(Geometry::area(pts.begin(), pts.end())) > 5.f) { auto chain = std::make_unique<QChain>(world()); chain->setVertices(std::vector<QPointF>(pts.begin(), pts.end())); chain->initializeLater(world()); world()->itemSet()->addBody(std::move(chain)); } } m_vertices.clear(); destroyLater(); }
void Bullet::beginContact(QFixture* f, b2Contact*) { if (f->isSensor()) return; if (dynamic_cast<Enemy*>(f->parent())) m_punchSound->play(); else { m_destroyed = true; destroyLater(); } }
void Bullet::beginContact(QFixture *other, b2Contact *) { if (other->isSensor()) return; destroyLater(); ParticleSystem *p = static_cast<World *>(world())->particleSystem(); p->addExplosion(worldCenter(), 5, 0.1, 20); if (QChain *chain = dynamic_cast<QChain *>(other->body())) { chain->cutCircle(Circle(Vector2d(worldCenter()), 5)); } }