在SFML中绘制凸形图时的奇怪行为

时间:2016-09-14 08:54:49

标签: c++ graphics game-engine sfml

我正在使用SFML库绘制凸形图:

#include <SFML/Graphics.hpp>

using namespace sf;

int main()
{
  RenderWindow window(VideoMode(400, 400), "SFML window");

  ConvexShape convex;

  convex.setPointCount(8);
  convex.setPoint(0, sf::Vector2f(0, 0));
  convex.setPoint(1, sf::Vector2f(180, 0));
  convex.setPoint(2, sf::Vector2f(180, 90));
  convex.setPoint(3, sf::Vector2f(100, 90));
  convex.setPoint(4, sf::Vector2f(100, 180));
  convex.setPoint(5, sf::Vector2f(30, 180));
  convex.setPoint(6, sf::Vector2f(30, 90));
  convex.setPoint(7, sf::Vector2f(0, 90));
  convex.setPosition(100, 100);

  while (window.isOpen())
  {
    sf::Event event;
    while (window.pollEvent(event))
    {
      if (event.type == sf::Event::Closed)
        window.close();
    }
    window.clear();
    window.draw(convex);
    window.display();
  }
  return EXIT_SUCCESS;
}

按顺时针顺序定义的点数,一切正常! convex1

但是当我稍微改变两个坐标时,我得到了一些我真正不期望的东西:

#include <SFML/Graphics.hpp>

using namespace sf;

int main()
{
  RenderWindow window(VideoMode(400, 400), "SFML window");

  ConvexShape convex;

  convex.setPointCount(8);
  convex.setPoint(0, sf::Vector2f(0, 0));
  convex.setPoint(1, sf::Vector2f(180, 0));
  convex.setPoint(2, sf::Vector2f(180, 90));
  convex.setPoint(3, sf::Vector2f(100, 90));
  convex.setPoint(4, sf::Vector2f(100, 200));   // CHANGED 180 to 200
  convex.setPoint(5, sf::Vector2f(30, 200));    // CHANGED 180 to 200
  convex.setPoint(6, sf::Vector2f(30, 90));
  convex.setPoint(7, sf::Vector2f(0, 90));
  convex.setPosition(50, 50);

  while (window.isOpen())
  {
    sf::Event event;
    while (window.pollEvent(event))
    {
      if (event.type == sf::Event::Closed)
        window.close();
    }
    window.clear();
    window.draw(convex);
    window.display();
  }
  return EXIT_SUCCESS;
}

我收到以下窗口: convex2

为什么会出现这种情况?

1 个答案:

答案 0 :(得分:0)

这些坐标与您的凸形状中的点有关,您应该仅使用这些点来更改对象的格式。为了移动整个对象,您应该使用以下命令:

convex.move(x Position, y Position);