Discord
Login
Community
DARK THEME

Does anyone know the most efficient way to detect a mouse hovering over a rectangle?

I do know you can track the x and y positions of the mouse to detect if it is hover over the positions of the rectangle but I was wondering if there is any other way.

Personally I use abs() as the fastest way. Psuedo code:

if abs(rectangle.x-mouse.x)<=rectangle.width/2 and abs(rectangle.y-mouse.y)<rectangle.height/2 then
 ... Mouse is within the rectangle
end

If you have few areas to test, a for loop is efficient and you test each area using the AABB algorithm.

If you need to check sets of areas and the areas themselves are small, you can use quadTree + AABB search.

Or you can use my crappy collision detection library. (Don't use it, use AABB algorithm)

https://microstudio.dev/i/DanMan97/myowncollisiondetectionlibrary/

i just use to if statements which compares the x y of the colliders with the mouse (it uses no complex math and is by far the easiest way i know of)

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community