Curve3D get the tangent at a point?

Godot Version

4.6

Question

Is there an easy way to get the index of the two closest points to another point on a Curve3D?

Basically I want the index of the original points a random point on the curve falls between.

I assume I could loop over the original points and check the distance between each and the closest point returned by get_closest_point until I find the index of the closest two?

To save gertkeno having to link to XY problem :wink: The underlying thing I’m trying to do relates to my older post Calculate the curve angle of a Path3D to calculate ā€œcentrifugal forceā€? - #3 by gentlemanhal Yes, I’m still having trouble finding a nice way to apply tilt to my trains on corners. I got something working that was good enough, until I introduced grades (i.e. the track going up and down on the Y axis)

I’m thinking maybe if I get the original control points I’d be able to calculate the tangent to the curve? :confounded_face:

Hey, never take away someone’s opportunity to link the XY problem :stuck_out_tongue:

You can get the parameter by calling get_closest_offset() and then pass this to sample_baked_with_rotation() to get the basis. The tangent should be in basis.z in the returned transform object.

2 Likes

I read the docs for that method (before your reply), and didn’t internalise at all that ā€œbasis.z as forward vectorā€ would be the tangent, and basically disregarded this would be useful for me!

Thanks I don’t get a lot of time to code on my game, but will see what I can do using this method.

Edit: I decided to rename the thread and mark your reply as the solution, in case anyone wants to find the tangent in the future.

2 Likes