SFML community forums
Help => Graphics => Topic started by: Crazed on January 15, 2009, 11:30:30 pm
-
Im trying to check collision between two sprites so I am doing:
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?
-
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.
-
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.
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
-
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).
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?
-
Use sf::Drawable::TransformToLocal (http://www.sfml-dev.org/documentation/1.4/classsf_1_1Drawable.htm#809f25ae0b3a2015ec8a44fe9f5a0008) on your points when comparing them.
-
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!
-
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.
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]);
-
Thanks a lot! I got it working.
But just for the record, Contains doesnt take a Vector2f, it takes 2 ints.