So, I guess the best thing I can do so far is begin writing my own thoughts. Maybe, something will solve this…
- Problem I have can be rephrased into:
“Stretch square onto paralelogram” In my case, Paralelogram is formed by triangle and it’s mirror:
To achieve this, I need to make the following:
- Place rectangle with SurfaceGui over it, so it will cover it fully.
- Rotate TextLabel so it will cover this paralelogram.
First of all, let’s instacalculate all what we can about triangle, later I can clear that up.
Secondly, we also need calculate CFrame of center:
local A, B, C = Vector3.new(0, 5, 0), Vector3.new(3, 7, 0), Vector3.new(0, 8, 3)
local a, b, c = A-B, B-C, C-A
local aLen, bLen, cLen = a.Magnitude, b.Magnitude, c.Magnitude
local AngleA = math.acos((bLen^2+cLen^2-aLen^2)/(2*bLen*cLen))
local AngleB = math.acos((aLen^2+cLen^2-bLen^2)/(2*aLen*cLen))
local AngleC = math.pi-AngleA-AngleB
local aH = c*math.sin(AngleB)
local bH = a*math.sin(AngleC)
local cH = b*math.sin(AngleA)
local HeightSplit = c*math.cos(AngleB)
local HeightPoint = B+c*HeightSplit
local LookVector = (A-HeightPoint).Unit
local UpVector = a.Unit:Cross(c.Unit)
local RightVector = UpVector:Cross(LookVector)
local Center = (B+C)/2
local Img1 = script.Image:Clone()
Img1.Name = "Image1"
Img1.Transparency = 0.5
Img1.Size = Vector3.new(3, 0.05, 3)
Img1.CFrame = CFrame.fromMatrix(Center, RightVector, UpVector, LookVector) * CFrame.Angles(0, 0, 0) - UpVector*0.025 - Vector3.new(0, 0.19, 0)
Img1.Parent = workspace
This results in this:
If we go back, we should remember, that goal is projecting square image on 2 triangles. So, we can see this as goal:
So from top, this will look as squares anyways.
How we can do that in 3D though? IDK.
Let’s try to calculate this red cutoff triangles. Their rotation.
What we have? So far, only all data of triangle.
Let’s look closer on triangle and what we want:
We want to get data of this “Red triangles”, tho they are outside and I mirrored them.
But, in this case, we have only hypothenuses of this triangles, right angle, and what it relative to.
So far, I don’t know any method abt solving triangles where only hypothenuse is known with right angle.
Let’s try to do random actions then…
Let’s try keep 1 triangle, thin one.
And also I’ll try to extrude other one’s hypothenuse so it will have same projection as other one:
Yes I made it! And also realized that I messed up with values a bit. So, abc, ABC now changed a bit to be proper ones.
Now, to how I did this step - due to Y axis being somewhat only consistant axis (relatively to other ones, XZ - will alter square, while Y won’t due to us projecting along it)
local A, B, C = Vector3.new(0, 5, 0), Vector3.new(3, 7, 0), Vector3.new(0, 8, 3)
local c, a, b = A-B, B-C, C-A
local aLen, bLen, cLen = a.Magnitude, b.Magnitude, c.Magnitude
local AngleA = math.acos((bLen^2+cLen^2-aLen^2)/(2*bLen*cLen))
local AngleB = math.acos((aLen^2+cLen^2-bLen^2)/(2*aLen*cLen))
local AngleC = math.pi-AngleA-AngleB
local aH = c*math.sin(AngleB)
local bH = a*math.sin(AngleC)
local cH = b*math.sin(AngleA)
local extrudeB = b.Y/c.Y
local extraB = A+c.Unit*(cLen*extrudeB)
local Att = workspace.Marks.BaseMark:Clone()
Att.Parent = workspace.Marks.BaseMark
Att.BillboardGui.TextLabel.Text = "B2"
Att.CFrame = CFrame.new(extraB)
local HeightSplit = c*math.cos(AngleB)
local HeightPoint = B+c*HeightSplit
local LookVector = (A-HeightPoint).Unit
local UpVector = a.Unit:Cross(c.Unit)
local RightVector = UpVector:Cross(LookVector)
local Center = (B+C)/2
Now, when we have new triangle, which shown as AB2C, we can find height dot from A towards B2C:
local function MakeAtt(Name, Pos)
local Att = workspace.Marks.BaseMark:Clone()
Att.Parent = workspace.Marks
Att.BillboardGui.TextLabel.Text = Name
Att.CFrame = CFrame.new(Pos)
end
local A, B, C = Vector3.new(0, 5, 0), Vector3.new(3, 7, 0), Vector3.new(0, 8, 3)
local c, a, b = B-A, B-C, C-A
local aLen, bLen, cLen = a.Magnitude, b.Magnitude, c.Magnitude
local AngleA = math.acos((bLen^2+cLen^2-aLen^2)/(2*bLen*cLen))
local AngleB = math.acos((aLen^2+cLen^2-bLen^2)/(2*aLen*cLen))
local AngleC = math.pi-AngleA-AngleB
local aH = c*math.sin(AngleB)
local bH = a*math.sin(AngleC)
local cH = b*math.sin(AngleA)
local extrudeB = b.Y/c.Y
local c2 = c*extrudeB
local c2Len = cLen*extrudeB
local extraB = A+c2
MakeAtt("B2", extraB)
local a2 = extraB-C
local a2Len = a2.Magnitude
local AngleC2 = math.acos((bLen^2+a2Len^2-c2Len^2)/(2*bLen*a2Len))
print(math.deg(AngleC2))
local b2cH = bLen*math.cos(AngleC2)
local HeightPoint = C+a2.Unit*b2cH
MakeAtt("h", HeightPoint)
So, finally we have at least a bit more info now abt what to do.
Now, we have info about 1 outer triangle. If needed, another one won’t be hard to get anymore. Cuz now requires less actions and after all division back of what we scaled B to B2.
And guess what? We indeed need second triangle =( due to the fact that total part size is sum of their XY sizes.
So let’s calculate them both.
local function MakeAtt(Name, Pos)
local Att = workspace.Marks.BaseMark:Clone()
Att.Parent = workspace.Marks
Att.BillboardGui.TextLabel.Text = Name
Att.CFrame = CFrame.new(Pos)
end
local A, B, C = Vector3.new(0, 5, 0), Vector3.new(3, 7, 0), Vector3.new(0, 8, 3)
local c, a, b = B-A, B-C, C-A
local aLen, bLen, cLen = a.Magnitude, b.Magnitude, c.Magnitude
local AngleA = math.acos((bLen^2+cLen^2-aLen^2)/(2*bLen*cLen))
local AngleB = math.acos((aLen^2+cLen^2-bLen^2)/(2*aLen*cLen))
local AngleC = math.pi-AngleA-AngleB
local aH = c*math.sin(AngleB)
local bH = a*math.sin(AngleC)
local cH = b*math.sin(AngleA)
local extrudeB = b.Y/c.Y
local c2 = c*extrudeB
local c2Len = cLen*extrudeB
local extraB = A+c2
MakeAtt("B2", extraB)
local a2 = extraB-C
local a2Len = a2.Magnitude
local AngleC2 = math.acos((bLen^2+a2Len^2-c2Len^2)/(2*bLen*a2Len))
local b2cH = bLen*math.cos(AngleC2)
local HeightPoint = C+a2.Unit*b2cH
MakeAtt("h", HeightPoint)
local AH2 = bLen*math.sin(AngleC2)
local b2cH2 = a2Len-b2cH
local SizeX = b2cH2/extrudeB+b2cH
local SizeY = AH2/extrudeB+AH2
local HeightSplit = c*math.cos(AngleB)
local HeightPoint = B+c*HeightSplit
local LookVector = (A-HeightPoint).Unit
local UpVector = a.Unit:Cross(c.Unit)
local RightVector = UpVector:Cross(LookVector)
local Center = (B+C)/2
local Img1 = script.Image:Clone()
Img1.Name = "Image1"
Img1.Transparency = 0.5
Img1.Size = Vector3.new(SizeX, 0.05, SizeY)
Img1.CFrame = CFrame.fromMatrix(Center, RightVector, UpVector, LookVector) * CFrame.Angles(0, 0, 0)-- - UpVector*0.025 - Vector3.new(0, 0.19, 0)
Img1.Parent = workspace
YES, IT GOT CORRECT SIZE!
But needs some rotatings =3
And this “Rotation” is what I need to calculate now.
Well, it was easier that I thought.
Won’t give code this time.
RightVector equals to h->c
UpVector gets calculated from our green triangle itself (BA:Cross(BC)
Now last part - rotation and scale of TextLabel!
Well, I was stupid a bit, but that’s what I came up to:
We can get angle on which label should be rotated, we can use our outer triangles. Let’s not forget that from top view they 4 are all equal, but not when flat. Due to this, we will take 2 sides of 2 triangles on same edge and get angle from that:
local LabelAngle = math.atan(b2cH/(b2cH2/extrudeB))
Now, as we can notice, we also need to scale this label so it will cover entire rectangle.
To simplify, we need to find length from center to part’s edge with respect to corner. And let’s remember that our “Label” has same outer triangles. So at least something is not hard…
local Sum = b2cH+(b2cH2/extrudeB)
local Prop1, Prop2 = b2cH/Sum, (b2cH2/extrudeB)/Sum
local LabelAngle = math.atan(Prop1/Prop2)
local LabelSize = (Prop1^2+Prop2^2)^0.5
Well I placed this label like needed!
Now, I would say I did it, but nope, there’s one situation when this “solution” won’t really work…
And this will occur if one of our corners is below point A:
Image here just got sent to inf coordinates. And I need to fix this
Well, one more issue was found and fixed - division by zero:
--fix part one:
if b.Y > c.Y then
B, C = C, B
b, c = c, b
end
--fix part two:
local extrudeB = math.max(math.abs(b.Y/c.Y), 0.00001)
It won’t really matter if it will have 0.00001 stud gap so that’s absolutely acceptable by me.
But one more problem is still unsolved - way too “obtuse” angle of our right-angled triangle:
It feels like it needs to be… Inversed?