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

.Magnitude not working correctly on GUI frames?

Asked by
Cizox 80
5 years ago

I am trying to make a graph that connects two "nodes" (just a GUI Frame) together by creating a thin GUI frame whose ends are connected to either node. The issue is that when the nodes start to become farther away from each other, the part of the script that is supposed to calculate the length of the line connecting the two nodes begins to overestimate the distance. Heres an example; I am trying to graph the function "tan(x)". Here is what it looks like:

https://imgur.com/a/odpV7Oe

As you can see, the nodes are represented by the red dots on the screen, and the black lines are the GUI frames connecting the red nodes. On the section of the graph where the nodes are more condensed, the script does a fine job in connecting them. When the nodes start to become distant, as marked by the highlighted blue line, the script starts to make the length of the GUI line longer than it actually is, and ends up making the lines break out of the graph frame. Here is the code that connects two nodes together:

local function connectNodes(firstNode, secondNode, reference)
    --Position
    local firstPosition = firstNode.Position
    local secondPosition = secondNode.Position

    local firstX = firstNode.Position.X.Scale
    local secondX = secondNode.Position.X.Scale

    local firstY = firstNode.Position.Y.Scale
    local secondY = secondNode.Position.Y.Scale

    local newX = (firstX + secondX) / 2
    local newY = (firstY + secondY) / 2

    local newPosition = Vector2.new(newX, newY)

    --Size
    local firstNodeCoordinates = Vector2.new(firstX, firstY)
    local secondNodeCoordinates = Vector2.new(secondX, secondY)

    local newSize = (firstNodeCoordinates - secondNodeCoordinates).Magnitude

    if (newSize > 0.5) then
        return nil
    end

    --Rotation
    local displacement = secondNode.AbsolutePosition - firstNode.AbsolutePosition
    local rotation = math.atan2(displacement.Y , displacement.X)

    local newEdge = edge:Clone()
    newEdge.Position = UDim2.new(newPosition.X, 0, newPosition.Y, 0)
    newEdge.Size = UDim2.new(newSize, 0, 0, 2)
    newEdge.Rotation = math.deg(rotation)
    newEdge.Parent = reference
end

I am not sure if it has something to do with the way I am getting the position of the nodes, or if it is because i'm strictly using Scale (Using AbsolutePosition for firstPosition and secondPosition does end up making them the right size, but the lines are then created off the graph), or maybe something else. Any help is appreciated.

Answer this question