QPointF intersects(const LineWrapper &other)/* const */{
        KisVector2D n0 = m_lineEquation.normal();
        KisVector2D n1 = other.m_lineEquation.normal();

        // Ensure vectors have the same direction
        if((n0(0) > 0) != (n1(0) > 0)) {
            n1 = -n1;
        }

        if(qFuzzyCompare(n0(0), n1(0)) &&
           qFuzzyCompare(n0(1), n1(1))) {

            const KisVector2D nearPoint(0,0);
            const KisVector2D farPoint(1e10,1e10);

            KisVector2D otherPt = other.m_lineEquation.projection(nearPoint);
            KisVector2D otherPtProj = m_lineEquation.projection(otherPt);

            KisVector2D newOffset = otherPt + 0.5 * (otherPtProj - otherPt);
            LineEquation tempLine(n0, newOffset);
            // Just throw it somewhere towards infinity...
            return toQPointF(tempLine.projection(farPoint));
        }

        return toQPointF(m_lineEquation.intersection(other.m_lineEquation));
    }
    void init(const KisVector2D &p0, const KisVector2D &p1) {
        m_lineEquation =
            LineEquation::Through(p0, p1);

        m_p0 = toQPointF(p0);
        m_p1 = toQPointF(p1);
    }
Exemplo n.º 3
0
void DebugDraw::DrawCircle(const b2Vec2 &center, float32 radius,
                           const b2Color &color)
{
    mP->setPen(toQColor(color));
    mP->setBrush(Qt::NoBrush);
    mP->drawEllipse(toQPointF(center),
                    radius * scaleRatio,
                    radius * scaleRatio);
}
Exemplo n.º 4
0
static QPolygonF toQPolygonF(const b2Vec2 *vertices, int32 vertexCount)
{
    QPolygonF polygon;
    polygon.reserve(vertexCount);

    for (int i = 0; i < vertexCount; ++i)
        polygon.append(toQPointF(vertices[i]));

    return polygon;
}
Exemplo n.º 5
0
void DebugDraw::DrawSolidCircle(const b2Vec2 &center, float32 radius,
                                const b2Vec2 &axis, const b2Color &color)
{
    Q_UNUSED(axis)

    mP->setPen(Qt::NoPen);
    mP->setBrush(toQColor(color));
    mP->drawEllipse(toQPointF(center),
                   radius * scaleRatio,
                   radius * scaleRatio);
}
Exemplo n.º 6
0
void DebugDraw::DrawSegment(const b2Vec2 &p1, const b2Vec2 &p2,
                            const b2Color &color)
{
    mP->setPen(toQColor(color));
    mP->drawLine(toQPointF(p1), toQPointF(p2));
}
Exemplo n.º 7
0
void Drawer::paintEvent(QPaintEvent* event)
{
  if (!world) return;

  // handle timestamp
  frame_stamps.push_front(time.elapsed());
  while (frame_stamps.size()>30) frame_stamps.pop_back();

  if (world->getTime()>cursorMovedTime+GameManager::cursorHideTime()) setCursor(QCursor(Qt::BlankCursor));
 
  const float dt = world->getTime()-data.getLastTransitionTime();

  static QRectF message_position(-75,-130,150,20);

  QPainter painter(this);
  painter.setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform,true);

  { // clear background
      //painter.beginNativePainting();
      //qglClearColor(Qt::black);
      //glClear(GL_COLOR_BUFFER_BIT);
      //painter.endNativePainting();
      painter.save();
      painter.setPen(Qt::NoPen);
      painter.setBrush(Qt::black);
      painter.drawRect(rect());
      painter.restore();
  }

  painter.setFont(drawingFont);
  painter.setPen(drawingPen);

  painter.translate(width()/2.,height());
  painter.translate(panningPosition);
  if (panning) painter.translate(panningPositionCurrent-panningPositionStart);
  painter.scale(scale,-scale);

  { // draw background
      data.getBall().drawPositions(trajectoryImage);

      painter.save();
      painter.scale(1,-1);
      QImage current = noises[noise_current].toImage();
      QPainter composition(&current);
      composition.setCompositionMode(QPainter::CompositionMode_Overlay);
      composition.drawPixmap(0,0,trajectoryImage);
      composition.end();
      painter.drawImage(QRectF(-GameManager::courtWidth()/2,-GameManager::sceneHeight(),GameManager::courtWidth(),GameManager::sceneHeight()),current,current.rect());
      //painter.drawPixmap(QRectF(-GameManager::courtWidth()/2,-GameManager::sceneHeight(),GameManager::courtWidth(),GameManager::sceneHeight()),trajectoryImage,trajectoryImage.rect());
      painter.restore();
      noise_current++;
      noise_current%=GameManager::numberOfNoiseBackground();
  }

  { // draw score
      painter.save();
      painter.scale(.1,-.1);
      //painter.drawRect(QRectF(-32,-30,30,25));
      painter.drawText(QRectF(-34.5,-30,30,25),Qt::AlignRight|Qt::AlignTop,QString("%1").arg(data.getTeam(Team::LEFT).getScore()));
      //painter.drawRect(QRectF(2,-30,30,25));
      painter.drawText(QRectF(4,-30,30,25),Qt::AlignLeft|Qt::AlignTop,QString("%1").arg(data.getTeam(Team::RIGHT).getScore()));
      painter.restore();
  }

  { // draw sets
      painter.save();
      painter.setPen(Qt::NoPen);
      painter.setBrush(drawingPen.color());

      painter.save();
      painter.translate(-GameManager::courtWidth()/2,2);
      for (int kk=0; kk<data.getTeam(Team::LEFT).getSet() && kk<4; kk++) {
	  painter.translate(1.2,0);
	  painter.drawEllipse(QRectF(-.5,-.5,1,1));
      }
      painter.restore();

      painter.save();
      painter.translate(GameManager::courtWidth()/2,2);
      for (int kk=0; kk<data.getTeam(Team::RIGHT).getSet() && kk<4; kk++) {
	  painter.translate(-1.2,0);
	  painter.drawEllipse(QRectF(-.5,-.5,1,1));
      }
      painter.restore();

      painter.restore();
  }

  if (data.getState()==GameData::STARTING) { // draw ready overlay
      painter.save();
      painter.scale(.1,-.1);
      painter.drawText(message_position,Qt::AlignCenter,"READY");
      painter.restore();
  }

  if (data.getState()==GameData::PLAYING && dt<1. && (dt-static_cast<int>(dt/.25)*.25<.25/2.)) { // draw go overlay
      painter.save();
      painter.scale(.1,-.1);
      painter.drawText(message_position,Qt::AlignCenter,"GO");
      painter.restore();
  }

  if (data.getState()==GameData::FINISHED) { // draw ready overlay
      bool aa = (dt-static_cast<int>(dt/.5)*.5<.5/2.);

      const Team& team = data.getLastScoringTeam();
      const Player* player = data.getLastScoringPlayer();

      QString message = QString("%1 SCORED").arg(team.getName());
      if (player) {
	  if (player->getTeam().getField()==team.getField()) message = QString("%1 SCORED").arg(player->getName());
	  else message = QString("%1 FAILED").arg(player->getName());
      }

      painter.save();
      painter.scale(.1,-.1);
      painter.drawText(message_position,Qt::AlignCenter,message); // finished
      if (aa) { // press space
	  QFont font(drawingFont);
	  font.setPixelSize(drawingFont.pixelSize()/2);
	  painter.setFont(font);
	  QRectF rectangle(message_position);
	  rectangle.setTop(rectangle.top()+25);
	  rectangle.setBottom(rectangle.bottom()+25);
	  painter.drawText(rectangle,Qt::AlignCenter,"press space");
      }
      //{ // win animation
      //    painter.save();
      //    if (data.getLastScoringTeam().getField()==Team::LEFT) painter.translate(width()/4.,300);
      //    else painter.translate(3.*width()/4.,300);
      //    QRect rectangle(-50,-50,100,100);
      //    if (aa) painter.drawPixmap(rectangle,win00,win00.rect());
      //    else painter.drawPixmap(rectangle,win01,win01.rect());
      //    painter.restore();
      //}
      //{ // lose animation
      //    painter.save();
      //    if (data.getLastScoringTeam().getField()==Team::RIGHT) painter.translate(width()/4.,300);
      //    else painter.translate(3.*width()/4.,300);
      //    QRect rectangle(-50,-50,100,100);
      //    if (aa) painter.drawPixmap(rectangle,lose00,lose00.rect());
      //    else painter.drawPixmap(rectangle,lose01,lose01.rect());
      //    painter.restore();
      //}
      painter.restore();
  }

  { // draw ball
      const Ball& ball = data.getBall();
      const b2Body* body = ball.getBody();

      painter.save();
      painter.translate(0,GameManager::groundLevel());
      painter.translate(toQPointF(body->GetPosition()));
      painter.scale(1,-1);
      painter.rotate(-body->GetAngle()*180/b2_pi);
      painter.drawPixmap(QRectF(-GameManager::ballRadius(),-GameManager::ballRadius(),2*GameManager::ballRadius(),2*GameManager::ballRadius()),ballImage,ballImage.rect());
      painter.restore();


      if (body->GetPosition().y>GameManager::sceneHeight()+GameManager::ballRadius()) {
	  float mini_ball_size = 1.-(body->GetPosition().y-GameManager::sceneHeight()-GameManager::ballRadius())/15;
	  if (mini_ball_size<.2) mini_ball_size = .2;

	  painter.save();
	  painter.translate(QPointF(body->GetPosition().x,GameManager::sceneHeight()-.6));
	  painter.scale(1,-1);
	  painter.drawPixmap(QRectF(-.4,-.4,.8,.8),arrowImage,arrowImage.rect());
	  painter.translate(0,1);
	  painter.rotate(-body->GetAngle()*180/b2_pi);
	  painter.drawPixmap(QRectF(-mini_ball_size/2.,-mini_ball_size/2.,mini_ball_size,mini_ball_size),ballImage,ballImage.rect());
	  painter.restore();
      }
  }

  { // draw left player
      const Player& player = data.getLeftPlayer();
      const b2Body* body = player.getBody();

      painter.save();
      painter.translate(0,GameManager::groundLevel());
      painter.translate(toQPointF(body->GetPosition()));
      painter.scale(1,-1);
      painter.drawPixmap(QRectF(-GameManager::playerRadius(),-GameManager::playerRadius(),2*GameManager::playerRadius(),GameManager::playerRadius()),leftPlayerImage,leftPlayerImage.rect());
      //if (data.isLastTouchingPlayer(player)) painter.drawRect(QRectF(-GameManager::playerRadius(),-GameManager::playerRadius(),2*GameManager::playerRadius(),GameManager::playerRadius()));
      //if (data.getState()==GameData::STARTING || (data.getState()==GameData::PLAYING && dt<1. && (dt-static_cast<int>(dt/.25)*.25<.25/2.))) drawPlayerName(player,painter);
      if (data.getState()==GameData::STARTING && dt<1. && (dt-static_cast<int>(dt/.25)*.25<.25/2.)) drawPlayerName(player,painter);
      //if (data.getState()==GameData::STARTING) drawPlayerName(player,painter);
      painter.restore();
  }

  { // draw right player
      const Player& player = data.getRightPlayer();
      const b2Body* body = player.getBody();

      painter.save();
      painter.translate(0,GameManager::groundLevel());
      painter.translate(toQPointF(body->GetPosition()));
      painter.scale(1,-1);
      painter.drawPixmap(QRectF(-GameManager::playerRadius(),-GameManager::playerRadius(),2*GameManager::playerRadius(),GameManager::playerRadius()),rightPlayerImage,rightPlayerImage.rect());
      //if (data.isLastTouchingPlayer(player)) painter.drawRect(QRectF(-GameManager::playerRadius(),-GameManager::playerRadius(),2*GameManager::playerRadius(),GameManager::playerRadius()));
      //if (data.getState()==GameData::STARTING || (data.getState()==GameData::PLAYING && dt<1. && (dt-static_cast<int>(dt/.25)*.25<.25/2.))) drawPlayerName(player,painter);
      if (data.getState()==GameData::STARTING && dt<1. && (dt-static_cast<int>(dt/.25)*.25<.25/2.)) drawPlayerName(player,painter);
      //if (data.getState()==GameData::STARTING) drawPlayerName(player,painter);
      painter.restore();
  }

  /*{ // draw bird
      const Bird& player = data.getBird();
      const b2Body* body = player.getBody();

      painter.save();
      painter.translate(0,GameManager::groundLevel());
      painter.translate(toQPointF(body->GetPosition()));
      painter.scale(1,-1);
      painter.rotate(-body->GetAngle()*180/b2_pi);
      painter.drawPixmap(QRectF(-GameManager::birdSize(),-GameManager::birdSize(),2*GameManager::birdSize(),2*GameManager::birdSize()),birdImage,birdImage.rect());
      painter.restore();
  }*/

  { // draw ground and net
      painter.save();
      painter.translate(0,GameManager::groundLevel());
      painter.drawLine(QPointF(-GameManager::courtWidth()/2,-drawingPen.widthF()/2),QPointF(GameManager::courtWidth()/2,-drawingPen.widthF()/2));
      painter.drawLine(QPointF(0,0),QPointF(0,GameManager::netHeight()-GameManager::netWidth()/3));
      painter.restore();
  }

  { // draw tv frame
      painter.save();
      painter.translate(0,GameManager::sceneHeight()/2);
      painter.scale(1.04,-1.04);
      painter.drawPixmap(QRectF(-GameManager::courtWidth()/2,-GameManager::sceneHeight()/2,GameManager::courtWidth(),GameManager::sceneHeight()),frame,frame.rect());
      painter.restore();
  }

  if (debugdraw) { // draw state overlay
      QString state_string;
      switch (data.getState()) {
	  case GameData::INIT:
	      state_string = "init";
	      break;
	  case GameData::STARTING:
	      state_string = QString("starting");
	      break;
	  case GameData::PLAYING:
	      state_string = "playing";
	      break;
	  case GameData::FINISHED:
	      state_string = "finished";
	      break;
      }

      float frame_fps = 0;
      float physics_fps = 0;
      if (!frame_stamps.empty()) frame_fps = 1000.*(frame_stamps.size()-1)/(frame_stamps.front()-frame_stamps.back());
      if (!physics_stamps.empty()) physics_fps = 1000.*(physics_stamps.size()-1)/(physics_stamps.front()-physics_stamps.back());

      painter.save();
      painter.resetTransform();
      painter.setFont(debugFont);
      painter.setPen(debugPen);
      painter.drawText(5,20,"state " + state_string);
      painter.drawText(5,40,QString("transition %1s").arg(dt,0,'f',2));
      painter.drawText(5,60,QString("fps %1 pps %2").arg(frame_fps,0,'f',2).arg(physics_fps,0,'f',2));
      painter.restore();
  }


  if (debugdraw) { // debug draw scene
      painter.save();
      painter.setFont(debugFont);
      painter.setPen(debugPen);
      painter.translate(0,GameManager::groundLevel());

      // draw bodies
      for (const b2Body* body=world->getFirstBody(); body!=NULL; body=body->GetNext()) {
	  painter.save();
	  painter.translate(toQPointF(body->GetPosition()));
	  painter.rotate(body->GetAngle()*180/b2_pi);

	  if (body->IsAwake()) painter.setBrush(QBrush(QColor::fromRgbF(1,1,0,.3)));
	  else painter.setBrush(QBrush(QColor::fromRgbF(0,0,1,.3)));

	  for (const b2Fixture* fixture=body->GetFixtureList(); fixture!=NULL; fixture=fixture->GetNext()) {
	      if (fixture->GetShape()->GetType()==b2Shape::e_polygon) {
		  const b2PolygonShape* shape = static_cast<const b2PolygonShape*>(fixture->GetShape());
		  int vertexCount = shape->GetVertexCount();
		  QPolygonF polygon;
		  for (int kk=0; kk<vertexCount; kk++) { polygon << toQPointF(shape->GetVertex(kk)); }
		  painter.drawPolygon(polygon);
	      } else if (fixture->GetShape()->GetType()==b2Shape::e_circle) {
		  const b2CircleShape* shape = static_cast<const b2CircleShape*>(fixture->GetShape());
		  painter.drawEllipse(QRectF(-shape->m_radius,-shape->m_radius,2.*shape->m_radius,2.*shape->m_radius));
		  painter.drawLine(QPointF(0,0),QPointF(shape->m_radius,0));
	      } else Q_ASSERT(false);
	  }

	  painter.restore();
      }

      //{ // draw joints
      //    painter.save();
      //    painter.setPen(QPen(QColor::fromRgbF(0,1,0)));
      //    for (const b2Joint* joint=world->getFirstJoint(); joint!=NULL; joint=joint->GetNext()) {
      //        painter.drawLine(toQPointF(joint->GetAnchorA()),toQPointF(joint->GetAnchorB()));
      //    }
      //    painter.restore();
      //}

      painter.restore();
  }


}