How can I calculate intersection point of two CFrames?

Here is an example of how you could do that

```
-- Assume you have two CFrames: cframe1 and cframe2
-- Extract the position vectors from the CFrames
local position1 = cframe1.Position
local position2 = cframe2.Position
-- Calculate the direction vector between the two positions
local direction = (position2 - position1).Unit
-- Calculate the distance between the two positions
local distance = (position2 - position1).Magnitude
-- Calculate the intersection point by moving from position1 in the direction of direction vector by the distance
local intersectionPoint = position1 + (direction * distance)
-- Print the intersection point
print("Intersection Point:", intersectionPoint)
```

Isn’t it the code that ChatGPT gives?

It’s not working. The intersectionPoint vector is the same as position2 vector.

Please don’t use Chat GPT for code as it’s unreliable, if you do use Chat GPT then check your answer to ensure that you are not giving a incorrect answer.

The math behind Nogalo (Chat GPT)'s code might be correct but you may need to adapt it into your own.

I don’t think so. I already tested this exact code with different input positions and none of the results were correct.

My apologies, I used chatGPT because it provides comments explaining the logic behind the code, and in my rush and confidence i skimmed through the code and thinking that it should work didn’t test it before posting.

Firstly, i asked chatGPT the question in a wrong way.

I asked “How can I calculate intersection point of two CFrames?”

instead of asking “How to find an intersection between two lines?”

I reworked its response to work with parts and this is the result

```
local A = workspace.A.CFrame
local B = workspace.B.CFrame
local point = workspace.Point.Position
local _A = A.LookVector * 1000
local _B = B.LookVector * 1000
local directionA = (_A - A.Position).Unit
local directionB = (_B - B.Position).Unit
if directionA:Cross(directionB).Magnitude == 0 then
print("Lines are parallel, no intersection")
return
end
local t = (B.Position - A.Position):Cross(directionB).Magnitude / directionA:Cross(directionB).Magnitude
local u = (B.Position - A.Position):Cross(directionA).Magnitude / directionA:Cross(directionB).Magnitude
local intersectionPoint = A.Position + directionA * t
--We use this function because math.round/ceil/floor round numbers smaller than 1 to 0
local function roundValue(value)
if (value < 1 and value > 0) or (value < 0 and value > -1) then
local decimalPlaces = 1
local roundedNumber = math.round(value * (10 ^ decimalPlaces)) / (10 ^ decimalPlaces)
return roundedNumber
else
return math.round(value)
end
end
local fixedintersectionPoint = Vector3.new(roundValue(intersectionPoint.X),roundValue(intersectionPoint.Y), roundValue(intersectionPoint.Z))
print("original", intersectionPoint)
print("Intersection Point:", fixedintersectionPoint)
print("actual point", point)
```

this is the actual position of the parts in the workspace

OUTPUT:

original 19.93024444580078, 0.498256117105484, -27.902341842651367

Intersection Point: 20, 0.5, -28

actual point 20, 0.5, -28

ok so this should hopefully work, i can’t test all the possible cases but i think it should. Let me know if you encounter problems.

That’s actually what I needed! Thank you so much!

But I have an issue: the output vector is mirrored around A point somehow. I don’t know why is it happening.

I am not sure i understand and the screenshot is only confusing me more. If the arrows represent look vectors then there would be no intersection

The lines can intersect in the way of arrow look vector and in the opposite way so it doesn’t matter.

The problem was fixed by changing

```
local intersectionPoint = A.Position + directionA * t
```

to

```
local intersectionPoint = B.Position + directionB * t
```

I see, well if it works then I guess my job here is done, just mark it as solved so others can find it and best of luck to you with whatever it is you’re doing

ChatGPT can still give incorrect answers that look or sound like the right answer.

Thanks to @Nogalo, I created this function:

```
local function intersection(a: CFrame, b: CFrame): Vector3?
local directionA = (a.LookVector * 1000 - a.Position).Unit
local directionB = (b.LookVector * 1000 - b.Position).Unit
if directionA:Cross(directionB).Magnitude == 0 then
return
end
local ax = directionA.X + directionA.Y + directionA.Z > directionB.X + directionB.Y + directionB.Z
local intersectionPoint = (if ax then a else b).Position + (if ax then directionA else directionB) * (b.Position - a.Position):Cross(directionB).Magnitude / directionA:Cross(directionB).Magnitude
return Vector3.new(math.round(intersectionPoint.X), math.round(intersectionPoint.Y), math.round(intersectionPoint.Z))
end
```

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