How to make a simple triangle through scripting

Point a = This tower thingy
image

Point b = This Red Thingy
Point C = This Orange Thingy
image

That will result in this being created
image

It must always be in that format though : being two wedges placed side by side.
So all I need to know is how I would kind of connect the points of a wedge to positions.


Here is a selection of all of the parts

Any help is always appreciated :grin:

9 Likes

I have this one:

local function Part()
	local p = Instance.new("WedgePart")
	p.Anchored = true
	
	return p
end

--Make triangle from three vertices
local function FindBase(v1, v2, v3)
	--local p1 = Part()
	--local p2 = Part()
	
	--Project one point onto the other two to form base
	--local p = line.origin - line.origin:Dot(line.dir.Unit)*line.dir.Unit
	
	local function TryThing(start, finish, top)
		local d12 = finish - start
		local n = (top - start)*d12
		local u = (n.X + n.Y + n.Z) / (math.pow(d12.X, 2.0) + math.pow(d12.Y, 2.0) + math.pow(d12.Z, 2.0))
		
		return u, start + u * d12
	end
	
	--Try each of three layouts, one must work
	local u, p = TryThing(v1, v2, v3)
	if 0.0 < u and u < 1.0 then
		return p, v1, v2, v3
	end
	
	local u, p = TryThing(v2, v3, v1)
	if 0.0 < u and u < 1.0 then
		return p, v2, v3, v1
	end
	
	local u, p = TryThing(v3, v1, v2)
	if 0.0 < u and u < 1.0 then
		return p, v3, v1, v2
	end
end

local pb = workspace.P1:Clone()
pb.Name = "P4"
pb.Parent = workspace
pb.BrickColor = BrickColor.Red()


	
local tri1 
local tri2

local function RemakeTris()
	wait(1)
	
	if tri1 ~= nil then
		tri1:Remove()
	end
	
	if tri2 ~= nil then
		tri2:Remove()
	end
	
	local p, v1, v2, v3 = FindBase(workspace.P1.Position, workspace.P2.Position, workspace.P3.Position)
	pb.Position = p
	
	tri1 = Part()
	tri1.Parent = workspace
	local up = (v3 - p).Unit
	local cf = CFrame.lookAt(p, v1, up)
	local length = (v1-p).Magnitude
	local height = (v3-p).Magnitude
	tri1.CFrame = cf + cf:VectorToWorldSpace(Vector3.new(0, height / 2.0, -length / 2.0))
	tri1.Size = Vector3.new(1.0, height, length)

	tri2 = Part()
	tri2.Parent = workspace
	local up = (v3 - p).Unit
	local cf = CFrame.lookAt(p, v2, up)
	local length = (v2-p).Magnitude
	local height = (v3-p).Magnitude
	tri2.CFrame = cf + cf:VectorToWorldSpace(Vector3.new(0, height / 2.0, -length / 2.0))
	tri2.Size = Vector3.new(1.0, height, length)
end

RemakeTris()

workspace.P1:GetPropertyChangedSignal("Position"):Connect(RemakeTris)
workspace.P2:GetPropertyChangedSignal("Position"):Connect(RemakeTris)
workspace.P3:GetPropertyChangedSignal("Position"):Connect(RemakeTris)
4 Likes

EgoMoose has made an article on this topic: Articles/3d triangles/3D triangles.md at master · EgoMoose/Articles · GitHub

3 Likes

Yeah, but it doesn’t make a triangle always in the shape that I have displayed it as.

2 Likes

What does this mean? What shape?

2 Likes

Look at the last image, it has 2 wedges of the same size, but using the code that you sent makes it so it sometimes changes format, such as there being a really big wedge and a tiny wedge.

2 Likes

What you want is obviously a subset of what we both gave you.

2 Likes

I will try this soon, when I get to my computer

1 Like

If you are strictly prohibited from using two differently sized triangles, you can only make symmetrical triangles.

1 Like

Yes, exactly. With two of the same sized wedges that form a symmetrical triangle

1 Like

If that is the case, it might not be wise to use three points to define the triangle, as there are many possibilities with this setup that lead to unsymmetrical triangles.

3 Likes

You need three points to define a triangle.

1 Like

Yeah as @goldenguy9 said your image shows an isosceles triangle two of the sides have the same length, only in that case can you divide the triangle into two equal right triangles.

The general case is for a scalene triangle is EgoMooses, it is not possible to divide it in two of the same size right triangle pieces in that case.

1 Like

Not if the triangle is symmetrical.

1 Like

You need three points to define a triangle no matter what kind of triangle it is. You could define a class of isoceles triangles with just a base and a height, but that would not specify where the triangle is in space or which way its facing; there would be infinite triangles which would fit that class, just all with the same area.

1 Like

You are correct. I was assuming that in OP’s face the triangle would always be “facing” the same direction. In which case, the circled point could be inferred without actually being defined.
image
And then you could use this to find the point symmetrical to the point on the bottom left

1 Like

Your solution does the same as this one: Articles/3d triangles/3D triangles.md at master · EgoMoose/Articles · GitHub. But with more usage

1 Like

Ok. I have used @BilonGamer’s method of using that documentation, but now I am just struggling on trying to make it look not funny because of the transparent parts.

METHOD 1: Set the wedges transparency to 0.75 (what I want it to be at.)
Result:


A weird rendering bug :x:

METHOD 2: Set an image / decal on the sides of the wedge.
Result:


Some wedges being different shapes etc. Resulting in it looking like method 1 with some of the parts. :x:

Is there anything else I could do to just make it look okay.
This guy managed to do it:

I CANNOT UNION THEM

1 Like

That is not really a bug at all, it’s just because of the way transparent parts are displayed. When a part is transparent, you can see through it. If you can see through it, it can overlap with other transparent parts resulting in these darker regions.

As to how to actually achieve this effect, I’m not really sure right now. Maybe you could use an Outline object, but I haven’t tested that.

1 Like

what exactly is an outline object?

1 Like