Ruby2d_hitboxes

require 'ruby2d'

@quad = Quad.new(
  x1: 10, y1: 10,
  x2: 200, y2: 20,
  x3: 180, y3: 180,
  x4: 50, y4: 220,
  color: 'olive'
)

@selected = nil

@corners = [*1..4].each_with_object([]) {|i, o|
  o << eval("Circle.new(x: @quad.x#{i}, y: @quad.y#{i}, z: 2, radius: 5, sectors: 32, color: 'red')")
}

active = ->(o,i) { o.color='yellow'; @selected=i }
inactive = -> { @corners.each {_1.color='red'}; @selected=nil }

def upd(x, y)
  if @selected
    @corners[@selected].x = x
    @corners[@selected].y = y
    eval("@quad.x#{@selected+1} = x")
    eval("@quad.y#{@selected+1} = y")
  end
end

on :mouse_down do |e|
  @corners.each_with_index {|c, i| active.(c, i) if c.contains?(e.x, e.y) }
end

on :mouse_up do |e| upd(e.x, e.y); inactive.() end
on :mouse_move do |e| upd(e.x, e.y) end

update do
  sleep 0.025
end

show

This ugly code is a test for my little helper-app that defines hitboxes for a square image in order to use collision detection.