SFML community forums

Help => Graphics => Topic started by: idan_marciano on May 17, 2017, 10:45:29 pm

Title: check if mouse clicked on sprite.
Post by: idan_marciano on May 17, 2017, 10:45:29 pm
helllo,
i am trying to make "pick a stick" game. and i have some problem recognize the specefic stick i press with the mouse.
i have created few sprite each of them is a stick(each of them have a diffrent angle).
i am trying to click on one of them and recognize them with out the blocking rectangle aligned to axis's.

in general i am trying to write somethin like this:

Vector2f mouse_location((float)event.mouseButton.x, (float)event.mouseButton.y);
m_graph.eventHandler(mouse_location);
....
-----event handler function-------


vector<Stick>::iterator stick = m_sticks.begin();
   for (; stick != m_sticks.end(); ++stick)
   {
      if ((*stick).isContain(mouse_location))
                           do something....
       }



-----is contain function-------
  return m_sprite.getGlobalBounds().contain(mouse_location);
Title: Re: check if mouse clicked on sprite.
Post by: Laurent on May 18, 2017, 08:23:40 am
And what's your problem?
Title: Re: check if mouse clicked on sprite.
Post by: idan_marciano on May 18, 2017, 08:45:39 am
The get global bounds gives me the blocked rectangle aligend with axis.
The stick is not aligend to axis so when i press at coordinets near the stick the blocked rectangle
Contain the (x,y) of mouse location  and its not the currect answer so how i can get the tranformed rectangle and check if the transformed rect is containing the (x,y).


Thanks.
Title: Re: check if mouse clicked on sprite.
Post by: Laurent on May 18, 2017, 04:54:47 pm
You have two solutions:
- compute the OBB (oriented bounding rectangle) of your stick and perform a point-in-obb test (Google for the algorithm)
- apply the inverse transformation (rotation) to the point so that you end up in a coordinate system where your stick is aligned on the axes, and use the sf::Rect::contains function.
Title: Re: check if mouse clicked on sprite.
Post by: idan_marciano on May 18, 2017, 07:26:58 pm
Thank alot .. used the second option..