Gee I didn't think I'd be back here this soon, but this time it's a simple problem:
I'm trying to make Boids, and I wrote this class for a single boid:
Boid.h:class Boid {
public:
sf::ConvexShape shape;
Boid(sf::Vector2f head) {// The 'head' parameter stores the coordinates of the "head" of the boid, with other two points being relative to the head.
shape.setPointCount(3);
shape.setPoint(0, head);
shape.setPoint(1, sf::Vector2f(head.x - 5, head.y + 20));
shape.setPoint(2, sf::Vector2f(head.x + 5, head.y + 20));
shape.setFillColor(sf::Color::Blue);
}
};
And then wrote this code in the main file to generate a 100 boids with random positions within the confines of the window (with 'w' being the name of the sf::RenderWindow):
main.cpp:std::vector<Boid>boids;
for (int i = 0; i < 99; i++) {
float spawn_x = rand() % w.getSize().x;
float spawn_y = rand() % w.getSize().y;
float angle = rand() % 360;
boids.push_back(Boid({ spawn_x,spawn_y }));
}
The result:
Ok, so far so good, but then I tried getting all the boids to rotate, and after messing around with for loops for a bit, I realized they were rotating around the top left corner, meaning that all of their origins were set to the point(0,0), so I kept that in mind and trying to set the origin of each boid to it's center modifying the code to make each boid have a random rotation when spawned:
The code now looked like this:
Boid.h:class Boid {
public:
sf::ConvexShape shape;
Boid(sf::Vector2f head, float angle) {// Added the 'angle' parameter which stores the rotation of the boid.
shape.setPointCount(3);
shape.setPoint(0, head);
shape.setPoint(1, sf::Vector2f(head.x - 5, head.y + 20));
shape.setPoint(2, sf::Vector2f(head.x + 5, head.y + 20));
//shape.setOrigin(sf::Vector2f(shape.getPoint(0).x, shape.getPoint(0).y + 10));
shape.setOrigin(sf::Vector2f(head.x, head.y + 10));
shape.setRotation(angle);
shape.setFillColor(sf::Color::Blue);
}
};
main.cpp:std::vector<Boid>boids;
for (int i = 0; i < 99; i++) {
float spawn_x = rand() % w.getSize().x;
float spawn_y = rand() % w.getSize().y;
float angle = rand() % 360;
boids.push_back(Boid({ spawn_x,spawn_y }, angle));
}
The result:
From that weird blue quadrant in the corner, you can tell the boids do have different rotations, and they do have their origins in their centers(I tried changing the origins to the positions of the heads of each boid, and the quadrant became larger, so this part is definitely working correctly), but why are they all positioned at the point(0,0)?
And why don't convex shapes have "local" origins like other drawables such as Sprites and RectangleShapes?