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

Author Topic: [Solved]sf::ConvexShape Problem  (Read 3397 times)

0 Members and 1 Guest are viewing this topic.

Giblit

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
[Solved]sf::ConvexShape Problem
« 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:


but I am getting this instead:


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 :)
« Last Edit: February 13, 2014, 01:58:41 am by Giblit »

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: sf::ConvexShape Problem
« Reply #1 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!

It is also said in the documentation : "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) ;)
Failing to succeed does not mean failing to progress!

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: sf::ConvexShape Problem
« Reply #2 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  ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: sf::ConvexShape Problem
« Reply #3 on: February 13, 2014, 01:52:04 am »
Oh yeah of course, how could I forget that  :o haha
Failing to succeed does not mean failing to progress!

Giblit

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: sf::ConvexShape Problem
« Reply #4 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

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [Solved]sf::ConvexShape Problem
« Reply #5 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
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: [Solved]sf::ConvexShape Problem
« Reply #6 on: February 13, 2014, 09:20:32 am »
For concave shapes, you can also use thor::ConcaveShape.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Giblit

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: [Solved]sf::ConvexShape Problem
« Reply #7 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.