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

Author Topic: How to create a Rect, the same size/location as a Shape  (Read 1668 times)

0 Members and 1 Guest are viewing this topic.

Idea

  • Newbie
  • *
  • Posts: 3
    • View Profile
How to create a Rect, the same size/location as a Shape
« on: January 24, 2012, 12:19:55 am »
Hello, I'm trying to create a rect, that is the same size as a shape and I've been having trouble.
Here's my code.
Code: [Select]


int main()
{
    //declare variables
    bool Falling;
    Falling = true;
    // Create the main window
    sf::RenderWindow App(sf::VideoMode(800, 600), "SFML window");
    const sf::Input& Input = App.GetInput();

    // Load a sprite to display
    sf::Shape Player = sf::Shape::Rectangle(50,50,100,100,sf::Color::White);
    Player.SetPosition(200,200);
    sf::Shape CollisionTest = sf::Shape::Rectangle(50,50,150,150,sf::Color::Cyan);
    CollisionTest.SetPosition(200,400);

    // Start the game loop
    while (App.IsOpened())
    {
        //timer
        float Time = Clock.GetElapsedTime();
        Clock.Reset();

        //Rectangles for Collision Detection
        sf::IntRect ghostRect;
        ghostRect.Left = Player.GetPosition().x;
        ghostRect.Top = Player.GetPosition().y;
        ghostRect.Right = Player.GetScale().x;
        ghostRect.Bottom = Player.GetScale().y;

        sf::IntRect TestRect;
        TestRect.Left = CollisionTest.GetPosition().x;
        TestRect.Top = CollisionTest.GetPosition().y;
        TestRect.Right = CollisionTest.GetScale().x;
        TestRect.Bottom = CollisionTest.GetScale().y;



        //Handles various If statements such as falling,input, and collision

        if(Falling == true)
        {
            Player.Move(0,100*Time);
        }

        if(App.GetInput().IsKeyDown(sf::Key::A))
        {
            Player.Move(-100 * Time,0);
        }
        if(App.GetInput().IsKeyDown(sf::Key::D))
        {
            Player.Move(100 * Time,0);
        }
        if(App.GetInput().IsKeyDown(sf::Key::Left))
        {
            Player.Move(-100 * Time,0);
        }
        if(App.GetInput().IsKeyDown(sf::Key::Right))
        {
            Player.Move(100 * Time,0);
        }
        if(ghostRect.Intersects(TestRect))
        {
            break;
        }
}

Some code has been taken out because it has nothing to do with the Rectangle problem.

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
How to create a Rect, the same size/location as a Shape
« Reply #1 on: January 24, 2012, 03:34:37 pm »
Obviously these lines are completely wrong. You need the right and bottom position, not the scale.
Code: [Select]
ghostRect.Right = Player.GetScale().x;
ghostRect.Bottom = Player.GetScale().y;

But I wouldn't know how to get the right and bottom position either.

You should use SFML 2, I know that it is possible there with sf::RectangleShape (but you will have to adjust your code because SFML2 is a little different).
TGUI: C++ SFML GUI

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
How to create a Rect, the same size/location as a Shape
« Reply #2 on: January 24, 2012, 08:13:04 pm »
In SFML 2, you can also use GetGlobal/LocalBounds().
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything