Hi!
I need some help coding a script that makes it possible to move nodes up and down, and change the triangles’ size, position and pivot.
In the image above, the 4 spheres, represent the nodes, and the triangles speak for themselves.
When I move a node up or down, the triangle(s) should adapt with them. For example if I move the top left node up:
But then of course, that the triangles actually touch the node and fill the square when viewed from above.
I’ve done some experimental things and math and came to the conclusion that in order to have the triangles change in size, position and pivot to ensure they touch each node and fill the square together we need to do this:
-
First of all, we need to assign base nodes for each triangle, let’s select the bottom left one as the base for each triangle. And set the pivot of the triangle to the bottom left edge.
-
After that, we need to know where the other 2 nodes are, are they on the left or right, or in front or in the back?
-
After we’ve located the remaining 2 nodes, we compare their x vector with the base node’s x vector.
-
If a node differs in the x vector, we check if they are alligned on the y axis or the z axis (assuming the triangles are laid flat out and not pointing up or down)
-
Once we identified on which axis the nodes are that differ in height, we can use the formulas for the required node.
-
When the node is on the y axis
y -axis formulas
- First we need to calculate the distance between the 2 nodes.
We do this by using the euclidean distance formula:
distance = square root( (x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2 )
- After that we can calculate the angle between the 2 nodes:
We first need to calculate the difference between the 2 positions of the nodes.
We simply extract the base node position from the node’s position we want to calculate.
After that we got the difference vector, from the difference vector, either the x, y or z is 0, meaning that we should not use that one in the following formula.
angle = 90 - (arctan(delta y / delta x))
please note that I am not quite sure why doing it minus 90 works, and I am also not sure if it works for all outcomes.
- Now that we have both the distance and the angle, we can calculate the new size, position and pivot.
- For the size, it is as simple as checking in which axis the node lies, y or z. If it lies in the y axis, you add the distance to the y value of Size samething for when it is lying on the z axis.
- For position:
- posX2 = posX1 + (-(delta Size y / 2) * sin(angle))
- posY2 = posY1 + ((delta Size y / 2) * cos(angle))
- poxZ2 = posZ1
- For pivot:
- pX2 = pX1
- pY2 = pY1 - (delta Size y / 2)
- pZ2 = pZ1
When the node is on the z axis
z-axis formulas
- Again we need to calculate the distance and angle first, these formulas are universal.
- Calculating new size, position and pivot:
- As mentioned in “y-axis formulas” the calculated distance needs to be added up to the z value of “Size”
- Position:
- posX2 = posX1
- posY2 = posY1 + ((delta Size z / 2) * sin(angle))
- pos Z2 = posZ1 - ((delta Size z / 2) * cos(angle))
- Pivot:
- pX2 = pX1
- pY2 = pY1
- pZ2 = pZ1 + (delta Size z / 2)
So, I have the math to make it happen, and it works, I have tested it in Roblox Studio without code and it succesfully works.
The thing is, I don’t really know how to code it to ensure that it works through scripts. So far I’ve only gotten to the point where I can drag the nodes up and down, and have the triangles change position, size etc but not really how I expected it.
Any suggestions, help etc is appreciated!