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

Author Topic: check if mouse clicked on sprite.  (Read 2305 times)

0 Members and 1 Guest are viewing this topic.

idan_marciano

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
check if mouse clicked on sprite.
« 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);

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: check if mouse clicked on sprite.
« Reply #1 on: May 18, 2017, 08:23:40 am »
And what's your problem?
Laurent Gomila - SFML developer

idan_marciano

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: check if mouse clicked on sprite.
« Reply #2 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: check if mouse clicked on sprite.
« Reply #3 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.
Laurent Gomila - SFML developer

idan_marciano

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: check if mouse clicked on sprite.
« Reply #4 on: May 18, 2017, 07:26:58 pm »
Thank alot .. used the second option..