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

Author Topic: Help with using Rect  (Read 3808 times)

0 Members and 1 Guest are viewing this topic.

Crazed

  • Newbie
  • *
  • Posts: 11
    • View Profile
Help with using Rect
« on: January 15, 2009, 11:30:30 pm »
Im trying to check collision between two sprites so I am doing:
Code: [Select]

rectA = player.GetSubRect();
rectB = portal.GetSubRect();
cout << rectA.Intersects(rectB);


Where rectA and rectB are IntRects.  No matter what Intersects is returning true.  Am I going about this the wrong way?

klusark

  • Newbie
  • *
  • Posts: 45
    • View Profile
Help with using Rect
« Reply #1 on: January 16, 2009, 04:11:33 am »
Have you checked all the numbers in the two rects with a debugger to make sure they are what you want them to be?
Edit: that function will get a rect defined as :mySubRect   (0, 0, Img.GetWidth(), Img.GetHeight()) therefore it will always collide.

Crazed

  • Newbie
  • *
  • Posts: 11
    • View Profile
Help with using Rect
« Reply #2 on: January 16, 2009, 06:59:27 am »
Im still not really sure why my code above didnt work, is it because it uses relative coordinates when im expecting it to use global coordinates (which I dont see a purpose for it to be limited to relative..that makes it useless as far as I can tell!)

But I found some code for collision testing under the French Wiki's Code Source section (no I dont know French but it was the only resource I could find since its still unclear why my above method wont work.)

Now I am once again having a similar problem.
Code: [Select]

mouseX = game.GetInput().GetMouseX();
mouseY = game.GetInput().GetMouseY();

for(int i = 0; i < 36; i++)
{
if(!tiles[i].GetSubRect().Contains(mouseX, mouseY))
game.Draw(tiles[i]);
}


If a mouse is over a tile, as a test I was expecting it to just not be drawn.  But NO MATTER WHAT, that Contains method returns TRUE.

Can someone please tell me what I am doing wrong?
Thanks

Crazed

  • Newbie
  • *
  • Posts: 11
    • View Profile
Help with using Rect
« Reply #3 on: January 16, 2009, 08:01:10 am »
I decided to dive in and make my own crude pixel-inside-sprite check.

Here is how I am checking if the mouse's coords are within the sprite.  I also show how I setup a few tiles (which are stored in an array of sprites).

Code: [Select]

tiles[0].SetPosition(0.0f, 400.0f);
tiles[0].SetRotation(45.0f);
tiles[1].SetPosition(48.0f, 400.0f-48.0f); //puts the 2nd tile by the 1st with
tiles[1].SetRotation(45.0f);                   //some space inbetween.

for(int i = 0; i < 36; i++)
{
    if(mouseX >= tiles[i].GetPosition().x)
        if(mouseX <= (tiles[i].GetSize().x + tiles[i].GetPosition().x))
     if(mouseY >= tiles[i].GetPosition().y)
       if(mouseY <= (tiles[i].GetSize().y + tiles[i].GetPosition().y))
     game.Draw(tiles[i]);
}


Im guessing that this isnt working because of the sprites being rotated.
But can someone please give me a solution!  There has to be a means to do this lol.  All I want to do is when the mouse is over a tile, then its going to highlight it, just like in a chess or checkers program.

EDIT:  Yep it is because of the tiles being rotated 45 degrees.  I undid this and it works perfectly.  Now can someone just help me figure out how to accomodate for the 45 degree rotation?

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Help with using Rect
« Reply #4 on: January 16, 2009, 08:21:52 am »
Use sf::Drawable::TransformToLocal on your points when comparing them.

Crazed

  • Newbie
  • *
  • Posts: 11
    • View Profile
Help with using Rect
« Reply #5 on: January 16, 2009, 08:25:47 am »
Did you mean for that link to be a youtube video of Mirrors Edge? lol..
EDIT: Ok I see you fixed the link, thanks!  Would you mind applying that to my code because ive shown many variations of my code as it has progressed and I would like to see how you mean to use it.  thanks!

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Help with using Rect
« Reply #6 on: January 16, 2009, 09:30:36 am »
Quote from: "Imbue"
For example, if you have an object that's 100 by 100 pixels, then you can convert a global mouse coordinate using TransformToLocal. After the transform, you can test for a click inside the object by doing x >= 0 && x <= 100 && y >= 0 && y <= 100. This will work even if the 100 by 100 object is scaled and rotated. It's quite useful.

Code: [Select]
   sf::Vector2f Size  = tiles[i].GetSize();
    sf::Vector2f Point = tiles[i].TransformToLocal(sf::Vector2f(mouseX, mouseY));

    if (tiles[i].GetSubRect().Contains(Point))
        game.Draw(tiles[i]);

Crazed

  • Newbie
  • *
  • Posts: 11
    • View Profile
Help with using Rect
« Reply #7 on: January 16, 2009, 08:28:52 pm »
Thanks a lot!  I got it working.
But just for the record, Contains doesnt take a Vector2f, it takes 2 ints.