I know a few threads have been written about this before, but they don’t explain the maths.
I’m trying to join two walls together, in such a manner that their corners line up nicely. Here is the ugliness I’m trying to solve.
This instance, when the angle isn’t obtuse, is fairly easy, and other threads have explained the maths behind it. However, I’m confused about when the angle is obtuse.
Here is my expert diagram of what I want to do. The wall on the right should move forward to the yellow line, and the wall on the on the left should move to the blue line.
To do this, it seems that I need to find the point on that diagram where the bottom blue line intersects with the left red line of the left wall. This would involve a lot of dot product and have a lot of stages to it, but it seems like other solutions are far more simply but do the same thing. Any feedback on this would be great
If you want to find the point of intersection of two lines, it’s easiest if you first write them in parametric form: X = Point.X + Direction.X * Dist Y = Point.Y + Direction.Y * Dist
Where Point is a known point on the line , Direction is the unit vector of the direction of the line, and Dist is the distance from the Point to whatever point you’re describing with X and Y.
To find an intersection, all we’d need to do is take the four equations describing our two lines, and then rearrange:
We know everything in these two equations except Dist1 and Dist2. Substitute out Dist1, and a bit of rearranging leaves us with this beast of a line: Dist2 = (Dir1.X*(Point2.Y-Point1.Y) + Dir1.Y*(Point1.X-Point2.X))/(Dir2.X*Dir1.Y - Dir2.Y*Dir1.X)
And we can substitute this back into our original equation to get the point of intersection: Intersection = Point2 + Dir2 * Dist2
As for actually getting the lines, you can use the RightVector property of the wall’s CFrame to get the wall’s “direction”, and use the wall’s position, offset by half it’s width multiplied by the LookVector, to get a point on the wall’s edge.
You can fix this issue with some CFrame manipulation. Think about the axis of intersection. Your parts intersect along the middle of their faces. You want the outer edge to connect with the outer edge of the other part.
First, find the offset from the center axis to the edge:
local currentAxisPoint = part.CFrame * CFrame.new(part.Size.X/2, 0, part.Size.Z/2) -- The center axis position. You'll need to invert the Z axis to change the side if the wrong side connects.
local targetAxisPoint = part2.CFrame * CFrame.new(part2.Size.X/2, 0, part2.Size.Z/2) -- You need to invert the Z axis just like above to flip the angle. You'll probably need to flip it if the walls' angles are above 180 degrees away from each other.
local edgeOffset = targetAxisPoint * currentAxisPoint:Inverse() -- Find the offset CFrame between the target and current
part.CFrame = part.CFrame * edgeOffset -- This positions the part so its left/right edge lines up with the other part's left/right edge
I assume your part follows these rules:
The positive X axis is left (along the length of the wall).
The negative Z axis is inwards towards the other wall.
The Y axis is upwards
If it doesn’t follow these, you can adjust the axese used accordingly.