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

Author Topic: TransformToGlobal and Rect::Intersects  (Read 1636 times)

0 Members and 1 Guest are viewing this topic.

Maetis

  • Newbie
  • *
  • Posts: 2
    • View Profile
TransformToGlobal and Rect::Intersects
« on: March 29, 2011, 06:46:59 pm »
Hi!
I'm having a hard time figuring out this...

I have a class for world boundries:
Code: [Select]


// RUUTU_W = 800
// RUUTU_H = 450

MaailmanRajat::MaailmanRajat():
rectUp_(0, -5, RUUTU_W, 0), rectDown_(0, RUUTU_H, RUUTU_W, RUUTU_H+5), rectLeft_(-5, 0, 0, RUUTU_H),
rectRight_(RUUTU_W, 0, RUUTU_W+5, RUUTU_H)
{

}

bool MaailmanRajat::TormasiSeinaan(sf::FloatRect hitbox)
{
        if (hitbox.Intersects(rectUp_))
        {
        return true;
        }
       
if (hitbox.Intersects(rectDown_))
        {
        return true;
        }
       
        if (hitbox.Intersects(rectLeft_))
        {
        return true;
        }
       
        if (hitbox.Intersects(rectRight_))
        {
        return true;
        }
       
       
        return false;
}


And I have a Player class where I'm checking if the player is out of world boundries (this should be changed to "the player will not be able to move outside boundries, but that's not the issue right now").

Code: [Select]

// Can the player move?
bool Pelaaja::pystyykoLiikkumaan(FKoordinaatti paamaara, sf::RenderWindow& App)
{
sf::Vector2f A;
sf::Vector2f B;

A.x = 0;
A.y = 0;
B.x = sprite_.GetSize().x;
B.y = sprite_.GetSize().y;

A = sprite_.TransformToGlobal(A);
B = sprite_.TransformToGlobal(B);

sf::FloatRect rect(A.x, A.y, B.x, B.y);

        // MaailmanRajat* rajat_;
return !(rajat_->TormasiSeinaan(rect));
}


Player class also has this at its constructor:
Code: [Select]
sprite_.SetCenter( (kuva_.GetWidth()/2), (kuva_.GetHeight()/2) );

But this doesn't work. Only the upper border works in some way but still in seemingly random way.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
TransformToGlobal and Rect::Intersects
« Reply #1 on: March 29, 2011, 07:05:50 pm »
Sprite::GetSize takes the scale factor in account, so when you apply TransformToGlobal you actually apply scale twice.

You must use GetSubRect().GetWidth()/GetHeight() for correct results.
Laurent Gomila - SFML developer

Maetis

  • Newbie
  • *
  • Posts: 2
    • View Profile
TransformToGlobal and Rect::Intersects
« Reply #2 on: March 29, 2011, 07:27:14 pm »
Thanks for a quick reply!

I changed
Code: [Select]
B.x = sprite_.GetSize().x;
B.y = sprite_.GetSize().y;

to
Code: [Select]
B.x = sprite_.GetSubRect().GetWidth().x;
B.y = sprite_.GetSubRect().GetHeight().y;


and now the left and upper borders do work, but the right and bottom borders still don't work. I'm guessing there's something wrong with the part with Intersects(), since hit detection isn't working either and I've done that basically the same way.

I remember seeing somewhere something about the center of the sprite related to TransfromToGlobal, but I'm not sure.


EDIT:
I checked that when going diagonally towards upper boundry, the Rect-hitbox has these values when the corner of the hitbox has passed the edge: 210.823, -1.96116, 184.11, 123.352. That seems to me that it should intersect with rectUp (0, -5, 800, 0), but it doesn't. What's going on?

EDIT2:
I noticed that if the Rect is not straight as in /_/ , then Intersect will not work, but if it has not turned very much, then it does work.

 

anything