SFML community forums

Help => Graphics => Topic started by: Giblit on February 13, 2014, 12:25:51 am

Title: [Solved]sf::ConvexShape Problem
Post by: Giblit on February 13, 2014, 12:25:51 am
I am not sure what I am doing wrong.

Here is what I am trying to get:
(http://s14.postimg.org/qkrf5o0n1/Expected.png) (http://postimage.org/)

but I am getting this instead:
(http://s12.postimg.org/srcn57ccp/Result.png) (http://postimage.org/)

The code I have is:

Code: [Select]
    sf::ConvexShape *Temp = new sf::ConvexShape( 6 );
    Temp->setPoint( 0 , sf::Vector2f( 0 , 0 ) );
    Temp->setPoint( 1 , sf::Vector2f( 20 , 0 ) );
    Temp->setPoint( 2 , sf::Vector2f( 20 , 40 ) ); //for some reason its assigning 20 , 30 instead
    Temp->setPoint( 3 , sf::Vector2f( 40 , 40 ) );
    Temp->setPoint( 4 , sf::Vector2f( 40 , 60 ) );
    Temp->setPoint( 5 , sf::Vector2f( 0 , 60 ) );

When I'm saying squares below I'm talking about a 20 x 20 square top right corner
The first point would be 0 squares over and 0 squares down so 0 , 0
The second point would be 1 square over 0 squares down so 20 , 0
The third point would be 1 square over 2 squares down so 20 , 40
The fourth point would be 2 squares over 2 squares down so 40 , 40
The fifth point would be 2 squares over and 3 squares down so 40 , 60
the sixth point would be 0 squares over and 3 squares down so 0 , 60

The point that seems to be getting messed up is the 3rd point it is supposed to be at 20 , 40 but for some reason it looks like it is at 20 , 30. When I output the point though it says it is at 20 , 40 so I am really confused.

Here is complete code:
#include <SFML/Graphics/Shape.hpp>
#include <SFML/Graphics/ConvexShape.hpp>
#include <SFML/System/Vector2.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/VideoMode.hpp>
#include <SFML/Window/Event.hpp>
#include <iostream>

class Tetrominio
{
    public:
        Tetrominio( void ){}
        virtual ~Tetrominio( void ){}
        void Draw( sf::RenderWindow &Window ){ Window.draw( *Shape ); }
    protected:
        sf::Shape *Shape;
};

class L : public Tetrominio
{
    public:
        L( void );
        ~L( void );
};

L::L( void )
{

    sf::ConvexShape *Temp = new sf::ConvexShape( 6 );
    Temp->setPoint( 0 , sf::Vector2f( 0 , 0 ) );
    Temp->setPoint( 1 , sf::Vector2f( 20 , 0 ) );
    Temp->setPoint( 2 , sf::Vector2f( 20 , 40 ) );
    Temp->setPoint( 3 , sf::Vector2f( 40 , 40 ) );
    Temp->setPoint( 4 , sf::Vector2f( 40 , 60 ) );
    Temp->setPoint( 5 , sf::Vector2f( 0 , 60 ) );

    std::cout << "Third point: " << Temp->getPoint( 2 ).x << ',' << Temp->getPoint( 2 ).y << std::endl; //testing the point
    Temp->setOrigin( 10 , 50 );

    Temp->setPosition( 320 , 200 );

    Shape = Temp;
}

L::~L( void )
{
    if( Shape )
        delete Shape;
}

int main()
{
    const int Width = 640 , Height = 400 , BPP = 32;
    const std::string Title = "Test";
    sf::RenderWindow Window( sf::VideoMode( Width , Height , BPP ) , Title );
    L test;

    test.Draw( Window );
    Window.display();

    sf::Event Event;
    while( Window.isOpen() )
    {
        while( Window.pollEvent(Event) )
        {
            if( Event.type == sf::Event::Closed )
                Window.close();
        }
    }
}
---
Third point: 20,40

Any suggestions would be greatly appreciated :)
Title: Re: sf::ConvexShape Problem
Post by: Geheim on February 13, 2014, 01:35:28 am
The problem with your shape is, that it is not a convex shape, but a concave one! (http://en.wikipedia.org/wiki/Convex_and_concave_polygons)

It is also said in the documentation (http://www.sfml-dev.org/documentation/2.1/classsf_1_1ConvexShape.php#details) : "It is important to keep in mind that a convex shape must always be... convex, otherwise it may not be drawn correctly."

I assume you are trying to make a tetris clone (or at least something similar)? I recommend using sf::RectangleShape instead (for each tile of the tetromino) ;)
Title: Re: sf::ConvexShape Problem
Post by: zsbzsb on February 13, 2014, 01:45:16 am
I recommend using sf::RectangleShape instead (for each tile of the tetromino) ;)

Or better yet sf::VertexArray of quads for each square  ;)
Title: Re: sf::ConvexShape Problem
Post by: Geheim on February 13, 2014, 01:52:04 am
Oh yeah of course, how could I forget that  :o haha
Title: Re: sf::ConvexShape Problem
Post by: Giblit on February 13, 2014, 01:58:28 am
Oh god I was thinking concave was rounded curve in and not anything that goes in major brain fart. Thanks for the help guys and yeah I made a few tetris clones using sf::Rectangle was trying to see if I could try out some other sfml features. I will have to try out the vertex array I haven't used that yet sorry to have wasted time by forgetting what concave was :P
Title: Re: [Solved]sf::ConvexShape Problem
Post by: Hapax on February 13, 2014, 02:01:27 am
Concave is when a shape has a hole in it. I remember it by thinking it looks like a cave  :o
Title: Re: [Solved]sf::ConvexShape Problem
Post by: Nexus on February 13, 2014, 09:20:32 am
For concave shapes, you can also use thor::ConcaveShape (http://www.bromeon.ch/libraries/thor/v2.0/doc/classthor_1_1_concave_shape.html).
Title: Re: [Solved]sf::ConvexShape Problem
Post by: Giblit on February 13, 2014, 08:50:26 pm
Looks like a pretty cool sfml extension I'll have to give it a try some time.