Getting Tangent line of specific point on a 3 or n point Bezier Curve

Hey there devs.

I previously made a post about something similar to this. I am working on a simple train, and i am using Bezier Curves to allow the train to turn corners. Right now, the train just moves along the curve, but doesn’t actually turn. I believe what i need to do is get the tangent line of the point the train is currently at, and position the train to it.

The problem is, i have been unable to find any solution to this problem. The ways that i have attempted it do not work. Sometimes the train begins facing the sky at the first point, then slowly comes down, and also faces the wrong way.

I have tried to find the derivative of the curve, but my lack of knowledge in quadratics and such is holding me back, and i haven’t found any resources i can understand.

Thank you for any help!

  • Mezza
1 Like

As a lateral thinking solution, why don’t you just get the position that the train’s front and back wheels should be at, and then position the train between those two? So get two positions with different distances along your curve, and have the train between them and facing towards the front one.

This method is easy to implement, and it’s kind of how train’s actually move, so you can get a bit of added realism from it too lol. Since it’s realistic it will also easily allow you do uphill sections and other types of track that could present unusual edge cases and difficulties with a simple tangent implementation.

1 Like

I guess i didn’t think of that. I’m really overthinking this thing. So you mean, grab the 2 points the front set of wheels and the back set of wheels should be at, then divide it? or subtract? Im so sorry, its 12am and i dont usually stay up late haha

If you have the front and back positions, then the main “body” of the train carriage would be halfway between them, that is to say the average. You can also easily point the carriage at the front position using CFrame.new(pos,lookAt)

So the CFrame of the train carriage would be given by:

CFrame.new((front+back)/2,front)

1 Like

Thanks! Was really over complicating this. I can now go to bed in peace.

2 Likes

If you still want to calculate the Tangent line on a Bezier curve, here is my explanation from another thread: Finding rotation of a point on Bezier Curve - #7 by 0x_7777