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

Author Topic: [SOLVED]Sprite Selection & Combined Transforms  (Read 2908 times)

0 Members and 1 Guest are viewing this topic.

Fitzy

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
[SOLVED]Sprite Selection & Combined Transforms
« on: June 22, 2016, 03:15:08 am »
Hello, guys!

I'm currently working on my own level editor using SFML as the rendering backend. I have recently added a scene hierarchy with parent and child relationships (Everything is working as intended).

My current problem is now with implementing sprite selection in the scene, using mouse. It is only returning true if I click within the sprites bounds at position (0, 0). I'm guessing this is due to combining the transforms for each sprite. Since the RootNode is sitting at position (0, 0).

My question is, how can I deal with a scene hierarchy of objects and check if the sprite is clicked using "sprite.getGlobalBounds().contains(sf::Mouse::getPosition(m_window)"?
« Last Edit: June 22, 2016, 10:39:56 pm by Fitzy »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Sprite Selection & Combined Transforms
« Reply #1 on: June 22, 2016, 07:45:27 am »
Transform the mouse position by the combined transform of the sprite.
Laurent Gomila - SFML developer

Fitzy

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Sprite Selection & Combined Transforms
« Reply #2 on: June 22, 2016, 04:35:59 pm »
Thanks for taking the time to reply :).

I've tried transforming the mouse to the combined sprites transform but I get the same result (All sprites become selected from a rect of [0, 0, 200, 100] positioned at (0, 0)). All my sprites getTransform() are returning identity transforms for some odd reason. All my sprites are moved by setPosition on initialization.

I am currently trying transform.transformPoint(), and it's giving me weird results but, I can now select each sprite individually but the calculations are off (see attachments for visual representation).

Here is a minimal example of code:
Code: [Select]
auto sprite = object->getSpriteComponent()->getSprite();

auto spriteTransform = object->getCombinedTransform();

if (sprite.getGlobalBounds().contains(spriteTransform.transformPoint(m_currentMousePosition.x, m_currentMousePosition.y)))
{
object->setSelected(true);
}
else
{
object->setSelected(false);
}

m_currentMousePosition is continuously updated through Qt's widget mouse position which is equivalent to sf::Mouse::getPosition(window).

Any advice, no matter how small is very welcome.
« Last Edit: June 22, 2016, 05:13:15 pm by Fitzy »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Sprite Selection & Combined Transforms
« Reply #3 on: June 22, 2016, 05:57:19 pm »
I guess the parent -> child transform is propagated through sf::RenderState's transform, right? So this is something sf::Sprite::getTransform() doesn't know about. You have to build the combined matrix yourself by recursively combining transforms, from parent to the root of the scene.
Laurent Gomila - SFML developer

Fitzy

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Sprite Selection & Combined Transforms
« Reply #4 on: June 22, 2016, 06:14:36 pm »
Thanks for the reply.

That's exactly right, I am already working on using a custom sf::Transform instead of using sf::RenderStates to recursively combine transforms. This should theoretically fix the issue and I can use your first answer as the solution.

I will update shortly.

Fitzy

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Sprite Selection & Combined Transforms
« Reply #5 on: June 22, 2016, 10:28:11 pm »
I can confirm that it was indeed to do with the propagation through sf::RenderStates, my component system wasn't setup correctly and I was manipulating the sprites transform through sf::Renderstates.

Thanks a lot for the help.