Minesweeper and trigonometry problem

Hello guys. I wanted to make minesweeper game. But as usual, I wanted to do something unique and interesting, like realistic terrain:


But, placing numbers over such terrain is pain:

I have realized that I can do that by using 2 SurfaceGuis, over weirdly rotated parts, and TextLabel with number in that SurfaceGui also needs to be rotated, and clipped by half with UIGradient to form triangle:

If I won’t fully clip with UIGradient, and will keep only 1 of number parts:

For 2 days now I tried to find formula which will let me calculate this placement with failure. So I come here for some math help now…

Strict constraints:

  1. Each tile uses exactly 2 triangles.
  2. Each 3 points of each triangle have equal shift on XZ plane (in my case equal to 3). Y axis can fluctuate in any way.
  3. TextLabel with number should cover entire triangle.

NOTE: decals won’t work here due to 2 reasons:

  1. They don’t support such triangles - each one is made from 2 wedges. Offsetting and rotating decals is impossible.
  2. Also I use TextLabels to make numbers, so if I’ll want to make 9+ mine tile number I won’t need to upload another image.

So, I guess the best thing I can do so far is begin writing my own thoughts. Maybe, something will solve this…

  1. 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:
  2. Place rectangle with SurfaceGui over it, so it will cover it fully.
  3. 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 :sob:

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?

If I could understand this lol. I’m too dumb for it

something like that?

Yep. That’s waht I’m trying to do. But not with meshes and with SurfaceGuis due to some reasons.

That was made in Blender, and it’s one of those cases where a grid cell in the game gets split into two triangles. Because of terrain distortion, they’re no longer right triangles.

The good thing is, any triangle can be made from two right triangles. In your case, you could create a grid using four projected TextLabels.

You’d need the three points of each right triangle, then calculate the fourth one (on the same plane) to form a square, but the square should be four times bigger, since only a quarter of the TextLabel will be visible.

You could generate the terrain (the plane) from a flat mesh like a matrix of Vector3s, then add some noise to the Y axis to make it look like terrain. After that, just calculate the midpoint of each grid cell, and that gives you the three points for the right triangles.

Just an idea :slight_smile:

Right angled triangles in this particular case won’t work due to one reason:
It’s impossible to align them properly due to varying sizes and stretches due to diffirent sizes of all triangles.
I thought about this and tried to implement, and it’s how my terrain itself generates. But numbers themselves fail to generate this way.

As final step, I decided to invert crazy cases by switching 1->2->3->4 vertex for triangles themselves instead of moving all work to numbers math.
That’s final result in crazy conditions: