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

Drawing a 2D line between 2 points?

Asked by 5 years ago

I'm creating a gamepass for my game - this gamepass will allow users to purchase a 2D box ESP [only during frenzy mode], giving them an advantage over players who only have access to the radar (which I plan to have all users visible on at all times, during something I'm calling "frenzy mode" in my FPS).

To do this, I've created a simple implementation. When players leave, join, whatnot, I create a GUI for them which has 4 frames contained within it on each client that has purchased the 2d box esp. When its frenzy mode, I parent the gui's to the playergui. When it isn't, I parent it to replicatedstorage [note: I'm aware this isn't that secure, I'm just trying to get it >working< before worrying about that. I have ideas in mind].

Anyhow, to make my idea a reality - I need to draw a line 2 pixels thick [I'd settle for one if its too hard to take width into account, but I prefer 2 as 1 isn't very visible] between point A and point B.

This is fairly simple if I didn't have to account for rotation, if it's going up/down or left/right, etc. Just subtract to get the distance, use math.abs() to make it absolute, and resize the frame + position it accordingly.

However, I'm stuck when accounting for the fact that I need to be going both up/down and left/right for two lines each, AND accounting for rotation and whatnot.

I've already calculated the 2d position of the player, the topleft, topright, bottomleft, bottomright, all of that jazz - so I have the base coordinates.

The only thing left is to fill in this function, and it will be 100% functional - everything else is done, literally, including calling this function once calculating all of the math.

local function AdjustLine(LineHandle, PointOneX, PointOneY, PointTwoX, PointTwoY, Color, Thickness)
    --\\Fill in my line here... smh I suck at math...
end;

This is the function I'm trying to fill in. Now to make it clear here, -I'M NOT asking for a spoonfeed or fully written code. I'd just like to know if someone can shed some light on the math/implementation behind this (preferably explaining it like I'm 5, because I can barely comprehend algebra tbh).

And I hope this question is found relevant to others and not deleted/downvoted - because I'm sure while the reason behind it isn't exactly the same, SOMEONE else has to be or will be or has been wondering how to draw a line between 2 points.

0
EDIT: to make it clear, I'm trying to adjust 'LineHandle' to fit those requirements. enhanceddevelopment 17 — 5y
0
If you cannot do the math needed then you probably shouldn't be doing this. But nonetheless here's something that might help: https://www.robloxdev.com/api-reference/datatype/Ray User#24403 69 — 5y
0
I'm trying to learn the math, not be put down. I'm already 99% of the way there - I've calculated 2d positions [converted from 3d], gotten the topleft, topright, bottomright, bottomleft, etc and called the AdjustLine function. I'm literally just asking for a point in direction for a (I think) simply piece of math. enhanceddevelopment 17 — 5y
0
Also that's like saying I shouldn't learn the math if I already don't know it, it makes no sense @sjr04alt. Anyhow, rays aren't what I'm looking for :/ but thanks for trying. No offense to all of this btw. enhanceddevelopment 17 — 5y
View all comments (11 more)
0
In no way was my comment meant to be taken negatively. My comment had no intent of putting you down; according to this post you didn't even try to do some kind of research. User#24403 69 — 5y
0
Well you ARE being negative, so regardless of your intentions, go away. According to this post? I never said that, you're pulling words out of your butt and trying to put them in my mouth, which is gross. I've been researching this for a couple hours and cant find anything that applies to ROBLOX. Stop making assumptions, just go. enhanceddevelopment 17 — 5y
0
that's disgusting vocabulary, stop it. This seems to be only a bit advanced to you, that was my point. I'm not trying to say you're inexperienced, that's all. User#24403 69 — 5y
0
I've been programming since 2012, it's not beyond my grasp, I just don't know where I would study how to implement such a thing. enhanceddevelopment 17 — 5y
0
Literally just look up "draw 2d lines between two points roblox" User#24403 69 — 5y
0
Yes and that turns up one result of laggy, inefficient, unexplained, hard to read code. As I've said I've been researching this. Clearly you just make the assumption I'm lazy, so GO AWAY. enhanceddevelopment 17 — 5y
0
sjr, if you don't have any useful contributions to the question, stop trying to berate the op and leave it alone, thank you. Fifkee 2017 — 5y
0
also, can't you just create the esp on the client, if nobody else is going to see it except the client? you can also use billboard guis also generated from the client to make the box esp too. i may be attempting to understand the question the wrong way. lmk? Fifkee 2017 — 5y
0
all wrong wrong wrong wrong User#19524 175 — 5y
1
If it's wrong, then provide an answer. Don't be a stuck up jerk and keep the answer for yourself. As I stated beforehand, if you don't have any useful contributions, then keep it to yourself. And, While wait() do is acceptable practice. Just because one person can't read it doesn't mean that nobody else can. Stop spreading misinformation. Sorry to el burst yur bubble. Fifkee 2017 — 5y
0
This is the most heated comments thread I've ever seen on this site lol. RayCurse 1518 — 5y

1 answer

Log in to vote
3
Answered by
RayCurse 1518 Moderation Voter
5 years ago
Edited 5 years ago

You want to position LineHandle such that it connects two points. First for the sake of simplicity, it will make it much easier if the anchor points of all gui elements involved in the positioning of this line have their AnchorPoint set to 0.5 , 0.5. In addition, we're going to be using Vector2s for this problem as it's much easier to work with in this scenario.There are three things that need to be calculated: position, size, and rotation.


Position

Position is the easiest as it's literally just the middle of the two points. The position can be calculated by adding the two vectors up and then dividing it by two. This is exactly the same as finding the middle of two regular numbers because it uses the same logic. The position part would look like this:

--Position
local position = (PointOne + PointTwo) / 2

Size

Size can be calculated by finding the displacement vector between the two points and getting the magnitude of that. A displacement vector can be thought of as a line connecting two points (kind of like what we're doing right now). A displacement vector can be calculated by subtracting one vector from another. For example if you wanted to find the displacement between a vector called v1 and another one called v2, the displacement would be v2 - v1. The magnitude of a vector is its length. The formula for calculating magnitude is irrelevant here because it is already done for us and can be accessed by doing vector.Magnitude. By getting the magnitude of the displacement vector, we are essentially getting the distance between the two points (remember that we will be converting all our vectors in to UDim2s later).

--Size
local size = Vector2.new((PointOne - PointTwo).Magnitude , Thickness)

Rotation

This seems like the trickiest part because it requires a tad of trigonometry but it's not all that hard. First, we get the displacement vector again (explained in the prior section). In order to get our rotation, we will need to find the angle this vector makes with the x axis. If we think of this vector like the hypotenuse a triangle, we find that the x value of this vector is the adjacent side and the y value of this vector is the opposite side. In order to get our angle, we simply need to take the inverse tangent of opposite divided by adjacent or in this case, our y component divided by the x component of our vector. This is how it looks like:

--Rotation
local displacement = PointTwo - PointOne
local rotation = math.atan(direction.Y / direction.X)

Now, all that's left is to change the color of our line handle and convert our position and size into UDim2s. We also need to convert our rotation from radians into degrees. This is how the final function would look like:

function adjustLine(LineHandle , PointOne , PointTwo , Color , Thickness)

    --Position
    local position = (PointOne + PointTwo) / 2

    --Size
    local size = Vector2.new((PointOne - PointTwo).Magnitude , Thickness)

    --Rotation
    local displacement = PointTwo - PointOne
    local rotation = math.atan(direction.Y / direction.X)

    LineHandle.Size = UDim2.new(0 , size.X , 0 , size.Y)
    LineHandle.Position = UDim2.new(0 , position.X , 0 , position.Y)
    LineHandle.Rotation = math.deg(rotation)
    LineHandle.BackgroundColor3 = Color
end

Example Usage:

local line = ...
local frame1 = ...
local frame2 = ...

adjustLine(line , frame1.AbsolutePosition , frame2.AbsolutePosition , Color3.fromRGB(61, 128, 252) , 2)
Ad

Answer this question