Hey there!
My math is terrible and I'm having trouble calculating moving a set of points around some distance from the mouse.
Imagine I have a square in the center of the screen using the DrawBox function, what i'd like to achieve is when I click and drag inside the bounds of the box, I should be able to drag the box around while it maintains it's shape.
I wrote a distance function to get the distance from the mouse position some point. This seems to work, but I think I'm missing an variable in the calculation because when I click on the box it expands.
Some of the code snippets I've used are:
do
if (IsMouseInsideRectangle(collision_box_x1, collision_box_y1, collision_box_x2, collision_box_y2))
box_color = RED
if (GetRawMouseLeftPressed())
dist_x1y1 = GetPointDistance(collision_box_x1, collision_box_y1, mouse_x, mouse_y)
dist_x1y2 = GetPointDistance(collision_box_x1, collision_box_y2, mouse_x, mouse_y)
dist_x2y1 = GetPointDistance(collision_box_x2, collision_box_y1, mouse_x, mouse_y)
dist_x2y2 = GetPointDistance(collision_box_x2, collision_box_y2, mouse_x, mouse_y)
endif
if (GetRawMouseLeftState())
box_color = BLUE
collision_box_x1 = mouse_x - dist_x1y1
collision_box_y1 = mouse_y - dist_x1y2
endif
else
box_color = GREY
endif
DrawBox(collision_box_x1, collision_box_y1, collision_box_x2, collision_box_y2, box_color, box_color, box_color, box_color, FALSE)
Sync()
loop
function GetPointDistance(x1, y1, x2, y2)
to_return = Sqrt(Pow(x2 - x1, 2) + Pow(y2 - y1, 2) * 1.0)
endfunction to_return
Real programmers don't comment their code, because it should be written with readability in mind.