SFML community forums

Help => Graphics => Topic started by: Jerome96 on July 27, 2014, 06:39:55 pm

Title: Problem with shapes
Post by: Jerome96 on July 27, 2014, 06:39:55 pm
Hello everyone  :)

I am trying too create an arrow. I could not create it with sf::ConvexShape because it is not a convex shape. So I decided to split it up in a head part and a end part.
For the head part I chose sf::ConvexShape and for the end part sf:RectangleShape.

I tried to put them together and rotated them but I didn't get the right solution.

Here is the code:

void Interaction::createArrow(Object *from_object, Object *to_object)
{
        float x_from = from_object->getX();
        float y_from = from_object->getY();
        float x_to = to_object->getX();
        float y_to = to_object->getY();
        float x_dif = x_to - x_from;
        float y_dif = y_to - y_from;
        float length = sqrt(x_dif * x_dif + y_dif * y_dif);
        float angle = atan2(y_dif,x_dif) * 180 / PI;
       
        shape_end = sf::RectangleShape(sf::Vector2f(length-10,4));
        sf::Rect<float> rect = shape_end.getLocalBounds();
        shape_end.setOrigin(rect.left,rect.top);

        shape_head.setPointCount(7);
        shape_head.setOrigin(0,0);
        shape_head.setPoint(0,sf::Vector2f(10,10));
        shape_head.setPoint(1,sf::Vector2f(17,25));
        shape_head.setPoint(2,sf::Vector2f(12,15));
        shape_head.setPoint(3,sf::Vector2f(12,30));
        shape_head.setPoint(4,sf::Vector2f(8,30));
        shape_head.setPoint(5,sf::Vector2f(8,15));
        shape_head.setPoint(6,sf::Vector2f(3,25));
        shape_head.rotate(90);

        float x_origin = -(shape_head.getLocalBounds().left + shape_head.getLocalBounds().width+x_dif);
        float y_origin = -(shape_head.getLocalBounds().top + shape_head.getLocalBounds().height/2+y_dif);
        shape_head.setOrigin(x_origin,y_origin);
        shape_head.rotate(angle);
        shape_end.rotate(angle);

        shape_head.setOrigin(7,10);
        shape_end.setPosition(x_from,y_from);
        shape_head.setPosition(x_to,y_to);
}

I try to rotate the arrow so that it is pointing from the (x_from|y_from) to the (x_to|y_to) point. But the arrow head is always a little bit deferred.
What did I miss?

Thank you for your help :D
Title: Re: Problem with shapes
Post by: Geheim on July 27, 2014, 08:26:28 pm
Why not using a sf::VertexArray, which inherits from sf::Transformable and sf::Drawable, like shown here (http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php#creating-a-sfml-like-entity) or just use a sf::Transform like shown above (http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php#transforming-a-vertex-array)?
Title: Re: Problem with shapes
Post by: Laurent on July 27, 2014, 09:03:51 pm
Quote
Why not using a sf::VertexArray, which inherits from sf::Transformable and sf::Drawable
Not sf::Transformable.
Title: Re: Problem with shapes
Post by: Nexus on July 27, 2014, 09:11:47 pm
Have you checked with a debugger what the coordinates are, and why they are wrong?

You could also have a look at thor::Arrow (http://www.bromeon.ch/libraries/thor/v2.0/doc/classthor_1_1_arrow.html) ;)
Title: Re: Problem with shapes
Post by: Geheim on July 27, 2014, 10:24:45 pm
Not sf::Transformable.
Oh, but I thought Jerome96 wants to rotate the arrow?
I am not aware of an easy way without using sf::Transformable like in the tutorials, or have I overlooked something...?
Title: Re: Problem with shapes
Post by: Laurent on July 27, 2014, 10:31:25 pm
Quote
Oh, but I thought Jerome96 wants to rotate the arrow?
I am not aware of an easy way without using sf::Transformable like in the tutorials, or have I overlooked something...?
I was just correcting you saying that sf::VertexArray inherits sf::Transformable. It's not true.
Title: Re: Problem with shapes
Post by: Geheim on July 27, 2014, 10:44:13 pm
Ah right now it makes sence, sry^^
I meant making his own class which inherits those two for using the vertex array like in the tutorials. Should use a correct sentence next time  ::)
Title: Re: Problem with shapes
Post by: Jerome96 on July 28, 2014, 01:09:25 pm
Why not using a sf::VertexArray, which inherits from sf::Transformable and sf::Drawable, like shown here (http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php#creating-a-sfml-like-entity) or just use a sf::Transform like shown above (http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php#transforming-a-vertex-array)?

I am not sure which sf::PrimitiveType I should use for my
arrow. Any suggestions?

Have you checked with a debugger what the coordinates are, and why they are wrong?

I will check that and send you the coordinates and some pictures.

Thank you anyway :)
Title: Re: Problem with shapes
Post by: Laurent on July 28, 2014, 01:17:08 pm
Quote
I am not sure which sf::PrimitiveType I should use for my
arrow. Any suggestions?
Triangles. You'll need 3 of them, so 9 vertices. Sounds reasonable for an arrow.