Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Setting the origin of sf::ConvexShapes. [SOLVED]  (Read 2399 times)

0 Members and 1 Guest are viewing this topic.

Riser

  • Newbie
  • *
  • Posts: 33
    • View Profile
Setting the origin of sf::ConvexShapes. [SOLVED]
« on: September 27, 2020, 03:07:49 pm »
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?
« Last Edit: September 27, 2020, 06:52:05 pm by Riser »

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Setting the origin of sf::ConvexShapes.
« Reply #1 on: September 27, 2020, 04:39:05 pm »
Convex shapes *do* have local origins. When you call setPoint() the position is relative to that origin. If you want to move one of your shapes just use setPosition() as the convex shape class inherits sf::Transformable as other drawables do.

Riser

  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: Setting the origin of sf::ConvexShapes.
« Reply #2 on: September 27, 2020, 06:51:49 pm »


I guess it was that simple huh? This is my first time working with convex shapes, hence the confusion.

Thank you!