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

Author Topic: Problem with shapes  (Read 3025 times)

0 Members and 1 Guest are viewing this topic.

Jerome96

  • Newbie
  • *
  • Posts: 2
    • View Profile
Problem with shapes
« 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

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: Problem with shapes
« Reply #1 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 or just use a sf::Transform like shown above?
Failing to succeed does not mean failing to progress!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with shapes
« Reply #2 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.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Problem with shapes
« Reply #3 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 ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: Problem with shapes
« Reply #4 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...?
Failing to succeed does not mean failing to progress!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with shapes
« Reply #5 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.
Laurent Gomila - SFML developer

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: Problem with shapes
« Reply #6 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  ::)
Failing to succeed does not mean failing to progress!

Jerome96

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Problem with shapes
« Reply #7 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 or just use a sf::Transform like shown above?

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 :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with shapes
« Reply #8 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.
Laurent Gomila - SFML developer