Find where rays intersect with each other?

Hello!

I am currently trying to make a placement system for fun and want to create walls that link up with each other. I’ve been having basically no trouble up to this point but I would want to do it similarly to how the counter system works in Welcome to Bloxburg.

I was thinking I could cast two rays, each part from the RightVector of each wall but I don’t know how I’d find the intersection.

Screenshot of the counter in the game:

For my idea, I was thinking I could cast two rays, one from each part but as I said in the title, I don’t know how to get the intersection between the two rays.

5 Likes

Heya I think this post might help you

1 Like

I am not familiar with this, but I think that I have a vague understanding of what you want.

Here is an illustration of my idea:


The idea is that you can calculate the CFrame of the wall using the the closest faces from A and B.

you need to take the Z axis from the first part and the X axis from the second one or the inverse
your script should be like this:

local Part1  = game.Workspace.Part1
local Part2 = game.Workspace.Part2
local intersection = Vector3.new(Part2.Position.X,Part1.Position.Y,Part1.Position.Z)|
local Part = Instance.new("Part")
Part.Parent = game.Workspace
Part.Position = intersection

the part Position is the intersection

Took a read, I don’t know if it’s what I am looking for as if I cast a ray, there may be an object in the way which would mess up the calculations as I don’t really want a line per se, only a ray. Unless that could work but I don’t know. I am also not the best in math and have no idea how it works.

@Brickman808,
So I would just cast a ray from the desired face, in the direction of the part labelled as “A”?

@NarakuLite,
I think this may work but I might run into some issues with direction and whatnot when not working with 90º angles. I’ll still try it out though.

I will take a shot at all three of your methods and report back.

@Brickman808,
Partially the same reason as below (working with acute angles). If I were to cast a ray, it wouldn’t give the desired effect unfortunately.

@NarakuLite,
Your idea did kind of work when working with 90º angles but trying to experiment with other angles results in this issue when 2 of the axes are the same, the result is not what I was looking for unfortunately.

Hot pink is what the result part would be.


Going to take a look and test yours Crafter215.

1 Like

Took a little but but I did eventually get it, thank you for that!

Just wanted to give my code for anyone who might ever need this.

local module = {
    ['GetIntersectionPoint'] = function(self, line_1_start, line_1_end, line_2_start, line_2_end)
		print(line_1_start, line_1_end, line_2_start, line_2_end)
		local line_1_m = (line_1_end.Z - line_1_start.Z) / (line_1_end.X - line_1_start.X)
		local line_2_m = (line_2_end.Z - line_2_start.Z) / (line_2_end.X - line_2_start.X)
		local line_1_b = line_1_start.Z - (line_1_m * line_1_start.X)
		local line_2_b = line_2_start.Z - (line_2_m * line_2_start.X)
		local intersect_x = (line_2_b - line_1_b) / (line_1_m - line_2_m)
		local intersect_z = (line_1_m * intersect_x) + line_1_b
		return Vector3.new(intersect_x, line_1_start.Y, intersect_z)
	end;
	['GetPointsOnXAxis'] = function(self, part) -- Returns the 2 points on the X axis, relative to the part, on the X axis.
		local partCFrame = part.CFrame
		local size = part.Size.X
		local negative = partCFrame * CFrame.new(-size / 2,0,0)
		local positive = partCFrame * CFrame.new(size / 2,0,0)
		return negative.p, positive.p
	end;
	['CreateIntersectionParts'] = function(self, part1, part2) -- Creates two more parts relative to the part
		local part1Size = part1.Size.X
		local part2Size = part2.Size.X
		local fakePart1 = part1:Clone()
		fakePart1.Parent = ignoreParts
		fakePart1.CFrame *= CFrame.new(part1Size,0,0)
		local fakePart2 = part2:Clone()
		fakePart2.Parent = ignoreParts
		fakePart2.CFrame *= CFrame.new(part2Size,0,0)
		local pointsPart1Negative, pointsPart1Positive = self:GetPointsOnXAxis(part1)
		local pointsPart2Negative, pointsPart2Positive = self:GetPointsOnXAxis(part2)
		print('pp1n', pointsPart1Negative, 'pp1p', pointsPart1Positive, 'pp2n', pointsPart2Negative, 'pp2p', pointsPart2Positive)
		local point = self:GetIntersectionPoint(pointsPart1Negative, pointsPart1Positive, pointsPart2Negative, pointsPart2Positive)
		return fakePart1, fakePart2, point
	end;
}
1 Like