Finding intersecting parts and getting a position based off of it

In a nutshell, how would I get the cross based on the two parts present.

image

So long as you only care about the 2D position along XZ then it’s pretty simple to get the intersections but which one do you want? It’s really just a matter of grabbing the X from one part and the Z from the other or vice versa. There are two possible intersects, up and left (as shown in your image) or right and down. Both intersects are exactly the same in size, shape and magnitude so you’ll need something (a Vector3 reference point, 0,0,0, the player or an just arbitrary point) to compare the distances of both intersects from the reference point and pick which one you want.

Here’s a quick example script. Create a Script in the ServerScriptService folder, next add two parts in the workspace named Part1 and Part2, positon them so they are a bit spaced out and press play. A ball will be created at each intersect, the near one orange and the far one blue. Don’t forget, the Vector3.Z becomes Vector2.Y when used this way.

--Script in ServerScriptService

local part1 = game.Workspace.Part1 --Add part to the Workspace name it "Part1" and position nearby.
local part2 = game.Workspace.Part2 --Add a second part to the Workspace name it "Part2" and position nearby but away from part1.

print("Part1 is @ " .. tostring(part1.Position))
print("Part2 is @ " .. tostring(part2.Position))

local refPoint = Vector3.new(0,0,0) --point in space to compare against

local part1PosV2 = Vector2.new(part1.Position.X, part1.Position.Z) --Create a Vector2 position from part1
local part2PosV2 = Vector2.new(part2.Position.X, part2.Position.Z) --Create a Vector2 position from part2

local i1 = Vector3.new(part1PosV2.X, 0, part2PosV2.Y) --Get one of the intersections
local i2 = Vector3.new(part2PosV2.X, 0, part1PosV2.Y) --Get the other intersection

print("Intersect 1 is @ " .. tostring(i1))
print("Intersect 2 is @ " .. tostring(i2))

local i1Dist = (i1 - refPoint).Magnitude --Get distance to the reference point for intersect 1
local i2Dist = (i2 - refPoint).Magnitude --Get distance to the reference point for intersect 2

local near --Vector3 of the closest intersect
local far --Vector3 of the farthest intersect
if i1Dist < i2Dist then 
	near = i1 --if i1 is closer
	far = i2 --i2 is farther
else
	near = i2 --else i2 is closer
	far = i1 --i1 is farther
end

print("Intersect @ " .. tostring(near) .. " is closer to refPoint.")

local nIntersect = Instance.new("Part") --Create a new part for the near intersect
nIntersect.Name = "Near Intersect" --Name the intersect
nIntersect.Size = Vector3.new(2, 2, 2) --Give it a size
nIntersect.Shape = Enum.PartType.Ball --Give it a shape
nIntersect.Color = Color3.fromRGB(255, 175, 47) --A splash of color
nIntersect.Parent = game.Workspace --Parent to Workspace
nIntersect.Position = near --Move to near intersection

local fIntersect = Instance.new("Part") --Create a new part for the far intersect
fIntersect.Name = "Far Intersect" --Name the intersect
fIntersect.Size = Vector3.new(2, 2, 2) --Give it a size
fIntersect.Shape = Enum.PartType.Ball --Give it a shape
fIntersect.Color = Color3.fromRGB(74, 113, 255) --A splash of color
fIntersect.Parent = game.Workspace --Parent to Workspace
fIntersect.Position = far --Move to far intersection

Not included but tested, you can add dragdetectors to the parts and reposition the intersects on DragEnd, by grabbing the moved parts new position, recalculating the intersects and then move nIntersect/fIntersect to the new intersects. Not sure if you wanted any interactivity so omitted.

Hope that helps

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.