I'm trying to draw Pac-man as part of an assignment using the convex shape class in SFML and I always get an extra piece drawn from the point 0,0
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
using namespace std;
#define PI 3.14f
#define Deg2Rad PI/180
ConvexShape Pac(float radius, Vector2f center)
{
int pointsCount = 315;
float theta = 0;
ConvexShape circle;
circle.setPointCount(pointsCount);
for (int i = 45, index = 0; i < 315; i++, index++)
{
Vector2f point;
theta = i * Deg2Rad;
point.x = center.x + radius * cos(theta);
point.y = center.y + radius * sin(theta);
cout << point.x << "," << point.y << endl;
circle.setPoint(index, point);
}
return circle;
}
int main()
{
RenderWindow window(VideoMode(500, 500), "Pac_man");
ConvexShape pacman;
pacman = Pac(100.0f, Vector2f(100, 100));
pacman.setFillColor(sf::Color::Yellow);
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case Event::Closed:
window.close();
break;
}
}
window.clear();
window.draw(pacman);
window.display();
}
return 0;
}
it always comes out like this
Your for loop only iterates 270 times (315 - 45), therefore only setting 270 out of 315 vertices. The other 45 vertices are at the default position of (0, 0).
Your next issue is that you do not create a vertex at the center. So you need one extra vertice for the center.
Once those two issues are sorted, the result is:
Edit: I also realise now that Pacman is NOT a convex shape, so "it may not be drawn correctly". To draw it correctly you will need to split it into either multiple convex shapes, or use a different primitive type (sf::TriangleFan?)