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

GUI Collision is really complicated?

Asked by
Griffi0n 315 Moderation Voter
5 years ago

I DO NOT WANT A SCRIPT FOR THIS. I just want a concept of how to do it. I already sto-- I mean borrowed Merely's function for this (I definitely could have made my own, I just didn't feel like it). Setting the object's position when it collides back to the previous position is really just no. It does not work well with momentum. I tried using collision vectors but that didn't work and is probably pretty clunky. I am using AABB collision so maybe I should try using raycasting? But I feel like that will have its own problems.

Here is my function (slightly modified). Ignore the bad tab indentation (it's already tabbed)

    collides = function(self, gui2, needsVector)
        local gui1 = self.reference -- The first gui object. I am using self here because I'm using it everywhere right now.

        local gui1_topLeft = gui1.AbsolutePosition
        local gui1_bottomRight = gui1_topLeft + gui1.AbsoluteSize

        local gui2_topLeft = gui2.AbsolutePosition
        local gui2_bottomRight = gui2_topLeft + gui2.AbsoluteSize

        local collisionVector
        if needsVector then
            if (gui1_topLeft.X < gui2_bottomRight.X) and (gui1_topLeft.Y < gui2_bottomRight.Y and gui1_bottomRight.Y > gui2_topLeft.Y) then
                collisionVector = Vector2.new(-1, 0)
            elseif (gui1_bottomRight.X > gui2_topLeft.X) and (gui1_topLeft.Y < gui2_bottomRight.Y and gui1_bottomRight.Y > gui2_topLeft.Y) then
                collisionVector = Vector2.new(1, 0)
            elseif (gui1_topLeft.X < gui2_bottomRight.X and gui1_bottomRight.X > gui2_topLeft.X) and (gui1_topLeft.Y < gui2_bottomRight.Y) then
                collisionVector = Vector2.new(0, -1)
            elseif (gui1_topLeft.X < gui2_bottomRight.X and gui1_bottomRight.X > gui2_topLeft.X) and (gui1_bottomRight.Y > gui2_topLeft.Y) then
                collisionVector = Vector2.new(0, 1)
            end
        end

        return ((gui1_topLeft.X < gui2_bottomRight.X and gui1_bottomRight.X > gui2_topLeft.X) and (gui1_topLeft.Y < gui2_bottomRight.Y and gui1_bottomRight.Y > gui2_topLeft.Y)), collisionVector
    end

(Really inefficient and I will clean this up later)

A problem is when I collide with a object it will just default to this first if in needsVector because it's inside the object already so the first if is true automatically.

Raycasting would seem to fix this but then I would need a bunch of other stuff and I just want a way to do collisions (not just detect that is easy) without jittery movement.

I know this is probably really difficult and maybe I should just use Unity because it already has 2D tools but I would like to make a pure GUI game in ROBLOX.

I'm really just looking for an efficient and good looking to the player method of GUI collision (I have never done collision before)

0
This does not belong on scripting helpers. AlphaGamer150 101 — 5y
0
k where Griffi0n 315 — 5y
0
devforum? Griffi0n 315 — 5y
0
ya. AlphaGamer150 101 — 5y
View all comments (6 more)
0
sh forums? Griffi0n 315 — 5y
0
I cant post on the devforum Griffi0n 315 — 5y
0
nvm i checked the rules, it is allowed here. 'You can ask questions on scripting concepts or how certain functions, methods, or techniques work.' AlphaGamer150 101 — 5y
0
it will be less than a day before this isnt even on the front page Griffi0n 315 — 5y
0
Umm I don't know if this will help but there is a wiki article here https://www.robloxdev.com/articles/2D-Collision-Detection Vulkarin 581 — 5y
0
SAT method ( explained here https://www.youtube.com/watch?v=IELWpIGtjRg ) EpicMetatableMoment 1444 — 5y

Answer this question