How can I detect if mouse position is over the circle position using Gosu Ruby?

371 views Asked by At

I have a circle that is drawn on the window, how can I define its boundaries ? So far, I can place it in an invisible rectangle and so I detect the mouse, but this is not what I want

CIRCLE_STEP = 10

def draw_circle(cx,cy,r,color)
    0.step(360, CIRCLE_STEP) do |a1|
    a2 = a1 + CIRCLE_STEP
    $window.draw_line cx + Gosu.offset_x(a1, r), cy + Gosu.offset_y(a1, r), color, cx + Gosu.offset_x(a2, r), cy + Gosu.offset_y(a2, r), color, 10
    end
  end

def update
   if mouse_over_button($window.mouse_x, $window.mouse_y, 180)
     @color = Gosu::Color::GREEN
   else
     @color = Gosu::Color.argb(255, 240, 232, 196) 
   end
 end

def mouse_over_button(mouse_x, mouse_y, shift)
    mouse_x.between?(get_rect_width, get_rect_width + shift) && mouse_y.between?(get_rect_height, get_rect_height + shift)
  end

 def get_rect_width()
   $window.width / 2  
 end
 
 def get_rect_height()
   $window.height / 2 + 200  
 end

Is there any other more efficient way ?

1

There are 1 answers

0
Tyler Heinings On BEST ANSWER

Use the Center of the circle's screen position, then take the mouse's screen position. Then offset by the circles radius.

Gosu::Window#mouse_

my = Gosu::Window#mouse_x
mx = Gosu::Window#mouse_y

if (my - circle_y).abs + (mx - circle_x).abs < circle_radius
  # mouse is over circle
end