I’m having some trouble detecting a collision between a circle and a rotated rectangular frame. I looked at previous topics on this issue, such as this one, but none of them take into account the rotation of the rectangle frame and they assume that it is a non-rotated frame.
I am trying to get it so that when the circle intersects the rotated rectangular frame, the frame turns green. If the circle is not intersecting the rotated frame, then the frame will stay red.
This is what I have so far. When I manually set the radius of it to something like 500, it works but the collision is still off by quite a bit.
local c = script.Parent.Circle
local r = script.Parent.Frame
local function getCollision()
local rad = c.Size.Y.Offset/2
local cx = c.AbsolutePosition.X;
local cy = c.AbsolutePosition.Y;
local rx = r.Position.X.Offset;
local ry = r.Position.Y.Offset;
local rw = r.Size.X.Offset;
local rh = r.Size.Y.Offset;
local testX = cx;
local testY = cy;
if (cx < rx) then
testX = rx;
elseif (cx > rx+(rw)) then
testX = rx+(rw);
end
if (cy < ry) then
testY = ry;
elseif (cy > ry+(rh)) then
testY = ry+(rh);
end
local distX = cx-testX;
local distY = cy-testY;
local distance = math.sqrt((distX*distX) + (distY*distY));
if (distance <= rad) then
r.BackgroundColor3 = BrickColor.Green().Color
else
r.BackgroundColor3 = BrickColor.Red().Color
end
end
game:GetService("RunService").RenderStepped:Connect(function()
getCollision()
end)
-- Moves the circle (for collision testing purposes)
spawn(function()
while true do
for i = 0, 1, 0.01 do
c.Position = UDim2.new(i, 0, 0.6, 0)
wait()
end
wait(1)
for i = 1, 0, -0.01 do
c.Position = UDim2.new(i, 0, 0.6, 0)
wait()
end
end
end)