I am currently trying to make a robotic arm with inverse kinematics using the script below to calculate it. But there is an issue when you bring the arm into certain positions which I think is caused by the position from P1 to P2 being less than the distance to the midpoint.
I’ve scanned the devforum a bit but couldnt find a solution.
Oh, that’s very confusing for what it does. Just for my own understanding I simplified it a bit. This does the same thing with less CFrame math:
-- Returns the CFrame for the front/back end of `Segment` which is
-- closer to `point`
function FindEndpoint(Segment, point)
local halfLength = Segment.Size.Z / 2
local relative = Segment.CFrame:PointToObjectSpace(point)
if relative.Z > 0 then
return Segment.CFrame * Vector3.new(0, 0, halfLength)
return Segment.CFrame * Vector3.new(0, 0, -halfLength)
But that doesn’t matter anyway, because FindEndpoint is the problem.
You don’t want to decide which endpoint is closest to the joint. You already know which one it is.
In other words, your whole Solve function can just be: