/*! \since 5.1 Returns the distance that this vertex is from a line defined by \a point and the unit vector \a direction. If \a direction is a null vector, then it does not define a line. In that case, the distance from \a point to this vertex is returned. \sa distanceToPoint() */ float QVector2D::distanceToLine (const QVector2D& point, const QVector2D& direction) const { if (direction.isNull()) return (*this - point).length(); QVector2D p = point + dotProduct(*this - point, direction) * direction; return (*this - p).length(); }
Bullet* Cannon::Shoot(Enemy *enemy, long elapsed) { if (elapsed - lastshot < shotinterval) return 0; int xa = this->pixmap.size().width() - this->SizeX; int ya = this->pixmap.size().height() - this->SizeY; QVector2D tcenter(center.x() - xa * 0.5, center.y() - ya * 0.5); QVector2D target = GetInterSect(enemy->center, enemy->next_checkpoint, Bullet::speed, Enemy::speed, tcenter, enemy->center); if (target.isNull()) target = GetInterSect2((enemy->center - enemy->next_checkpoint).length(), enemy->next_checkpoint, enemy->next_checkpoint2, Bullet::speed, Enemy::speed, tcenter, enemy->center); if (target.isNull() || (this->center - target).length() > this->range) return 0; lastshot = elapsed; Bullet*b = new Bullet(tcenter, target, elapsed, QPixmap("bullet.png")); b->enemy = enemy; return b; }
QVector2D NavigationMath::rayCircleIntersection( bool & valid , const QVector2D & origin , const QVector2D & ray , const float radius) { if(ray.isNull()) { valid = false; return QVector2D(); } const float a = ray.lengthSquared(); const float b = 2. * QVector2D::dotProduct(ray, origin); const float c = origin.lengthSquared() - static_cast<float>(radius * radius); const float delta = b * b - 4. * a * c; if (delta < 0.0) { valid = false; return QVector2D(); } const float s = sqrt(delta); // the two intersections const float t0 = (-b + s) / (2.0 * a); const float t1 = (-b - s) / (2.0 * a); // nearest one const float t = qMin<float>(t0, t1); valid = true; return t * ray + origin; }