Why is this not returnin the right Vector2 relative to this circle

I have a circle and I click on somewhere in the circle, I want another smaller circle to go there when I do.
If I click out of the circle however, the magnitude should clamp and return the Vector2 on the circumference of the circle from the origin of the circle.

Here’s a function I wrote if someone knows wats wrong I’d like to know:

local function getClosestBounds(pX, pY)
    local pXY = Vector2.new(pX, pY)
    local relativeLen = (pXY - circle.AbsolutePosition).Magnitude
    local clampedLen = math.clamp(relativeLen, 0, circle.AbsoluteSize.X)
    local alpha = clampedLen / relativeLen
    return pXY:Lerp(circle.AbsolutePosition, alpha)
end

Wouldn’t circle.AbsoluteSize.X be the diameter of the circle? Shouldn’t you be using the radius instead?
So it should be: circle.AbsoluteSize.X / 2.

That’s not the problemo, doesn’t really change anythin.
Some additional data: AnchorPoint is Vector2.new(0.5, 0.5)

To my knowledge, AbsolutePosition calculates the position where the anchor point is always Vector2.new(0, 0) no matter if you change it.

If you do some simple testing, you can verify it.

image

(AbsolutePosition should be the same as Position but it’s not).

And how does this help me I need my code corrected because I’m completely lost as to why it’s off lol

To account for the offset you need to add the radius.

local function getClosestBounds(pX, pY)
    local pXY = Vector2.new(pX, pY)
    local centre = circle.AbsolutePosition + (circle.AbsoluteSize / 2)
    local relativeLen = (pXY - centre).Magnitude
    local clampedLen = math.clamp(relativeLen, 0, circle.AbsoluteSize.X / 2)
    local alpha = clampedLen / relativeLen
    return centre:Lerp(pXY, alpha)
end
3 Likes

This works however strangely it doesn’t clamp at the circumference along the bottom side of the circle, it’s like the lerp is shorter than it should be

Show a screenshot.


Hey (@Waffloid is a cutie)
After some ponderin and deep self reflectance I figured it out, offsettin the Y value by negative 30 and subtractin both the returned Lerp X and Y values by the Parent component’s AbsolutePosition X and Y values, I found that it functions perfectly. The explanation my friend came up for this is that because of the topbar, ScreenGuis set positions relative. It’d seem that even though my ScreenGui instance has IgnoreGuiInset enabled, this behaviour is ignorant of it.

2 Likes