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

Author Topic: Hitbox  (Read 1607 times)

0 Members and 1 Guest are viewing this topic.

Baro

  • Newbie
  • *
  • Posts: 6
    • View Profile
Hitbox
« on: March 18, 2022, 12:52:34 am »
Hello! A new programmer here!. I`m developing a videogame, because it`s the final proyect of one of my assignments.

Right now im making the menu, and i dont know how to make the mouse interact with the buttons. I mean, i know how to make the button react to the click of the mouse, but i can`t find a way to verify if the mouse is inside the hitbox of the button

Any help is apprecitaed.

Thank you!~
« Last Edit: March 18, 2022, 12:55:47 am by Baro »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Hitbox
« Reply #1 on: March 18, 2022, 07:28:24 am »
Hello.
If your buttons or menu elements are sf::Sprite or sf::RectangleShape you can get the bounding box of your button with the getGlobalBounds function. Once you have its bounding box you can use it as an hitbox and use its contains function to check if the mouse coordinates are inside of it.
(You can also write your own hitboxes instead of using the bounding boxes if you want a smaller clickable region for example, but I think you'll get the idea)