Recently, I am creating a CFrame-based moving system in roblox to completely minimize the derailment of the trains to zero. However, another problem floats to the surface. After various times of testing, since CFrame-based system requires the main node part to be anchored, therefore the rod constraint, or any constraint becomes useless following the anchor. As a result, the train carriages often keep leaving behind apart.
What it should be
If it’s a fixed relative distance with a fixed relative rotation (a whole fixed CFrame), and the track of the train does not turn (the carriages just move forwards and backwards in a straight line) you can use something like this (part1 is the base part, part2 is the relative part):
-- Get relative CFrame:
local relativeCFrame = part1.CFrame:ToObjectSpace(part2.CFrame)
-- Apply relative CFrame:
part2.CFrame = part1.CFrame:ToWorldSpace(relativeCFrame)
This is essentially how a WeldConstraint works without using their physics properties, though.
Here’s how that looks when wrapped in the Heartbeat:
In case otherwise, if your track does turn at some point and it’s not a straight line…
There’s a fundamental flaw: If you take a look at the video above, at around second 19, you may notice how rotation is also fixed to the origin, not just position; This is problematic because the rotation isn’t fixed in case the tracks turn, just the position is (the distance from the center of one carriage to another is practically the same all the time), the rotation is actually not from the origin/center, but from the connection between the two carriages.
There is a relatively simple way of solving that, though. It’s a completely different approach but serves the purpose (let me know if you would need script snippet for this one):
Record the movement of the carriage at the front of the motion (each x seconds you save its CFrame to an array), and then just playback that recording (go iterate through each element of the CFrame array, waiting x seconds between each CFrame) with the following carriages, maintaining exactly one carriage of distance between playbacks (you can manually check how many CFrames of the array it needs to wait to exactly account for one carriage).
Thanks for your suggestion! Now, I have just created a solution. Since the game uses CFrame,Lerp to approach certain position between nodes, here is my solution.
By obtaining d1 using coordinate deduction in magnitude, since the carriage distance is always constant, therefore, by subtracting d1 with the constant distance, you obtain d3.