Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I print something when a Frame touches an another one?

Asked by
p0vd 207 Moderation Voter
4 years ago

I am currently looking for a script when a Frame touches the other one, it prints something out. I do not have a code, but I have a game if more info is needed: https://www.roblox.com/games/3481420494/frisk-moving-test

0
I would think you'd use positions The_Pr0fessor 595 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

This function takes four Vector2 positions ("corners"). Returns true if the produced rectangles overlap, false if they don't.

function PositionsOverlap (corner1, corner2, corner3, corner4)
    -- corner1 is a corner of the first frame
    -- corner2 is the corner opposite to corner1 in the first frame
    -- same idea for corner3 and corner4, but they're for the second frame

    local adj_cornerm = Vector2.new (math.min (corner1.X, corner2.X), math.min (corner1.Y, corner2.Y))
    local adj_cornerM = Vector2.new (math.max (corner1.X, corner2.X), math.max (corner1.Y, corner2.Y))
    local rel_cornerA = Vector2.new (math.min (corner3.X, corner4.X), math.min (corner3.Y, corner4.Y)) - adj_cornerm
    local rel_cornerB = Vector2.new (math.max (corner3.X, corner4.X), math.max (corner3.Y, corner4.Y)) - adj_cornerm
    local rel_cornerC = Vector2.new (math.min (corner3.X, corner4.X), math.max (corner3.Y, corner4.Y)) - adj_cornerm
    local rel_cornerD = Vector2.new (math.max (corner3.X, corner4.X), math.min (corner3.Y, corner4.Y)) - adj_cornerm
    local width = adj_cornerM.X - adj_cornerm.X
    local height = adj_cornerM.Y - adj_cornerm.Y
    for _, corner in pairs ({rel_cornerA, rel_cornerB, rel_cornerC, rel_cornerD}) do
        if corner.X >= 0 and corner.Y >= 0 and corner.X < width and corner.Y < height then
            return true
        end
    end
    return rel_cornerA.X <= 0 and rel_cornerA.Y <= 0 and rel_cornerB.X >= width - 1 and rel_cornerB.Y >= height - 1
end

-- this prints true
print (PositionsOverlap (Vector2.new (0, 0), Vector2.new (2, 2), Vector2.new (1, 1), Vector2.new (3, 3)))

-- this prints false
print (PositionsOverlap (Vector2.new (0, 0), Vector2.new (1, 1), Vector2.new (2, 2), Vector2.new (3, 3)))

-- this prints true
print (PositionsOverlap (Vector2.new (6, 10), Vector2.new (7, 12), Vector2.new (6.5, 11), Vector2.new (1, 3)))

-- this prints true
print (PositionsOverlap (Vector2.new (6, 10), Vector2.new (7, 12), Vector2.new (20, 20), Vector2.new (1, 3)))
0
thank you! p0vd 207 — 4y
0
for some reason i find this funny that something that sounds simple is such a huge script. p0vd 207 — 4y
Ad

Answer this question