Curve3D: How to get the direction at a given point?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MikeMikeMike

Hi…,

I’ve figured out how Curve3D.get_closest_point(...) works.

How to get the direction at this closest point?

Thanks

Mike

Short answer is(closest_point - current_point).normalize() … but that is a guess. Please add some code in your question (edit)

clemens.tolboom | 2021-07-25 16:05

hi clemens,

the code lools similar to this:

var curve := Curve3D()
var closest_point = curve.get_closest_point( point_somewhere )

I don’t have a current_point on the curve.

Thanks

Mike

MikeMikeMike | 2021-07-25 17:45

I exactly don’t know what you want, but i think you need to use curve’s
get_point_position(point_index) to get position, and then simply calculate direction vector as CLEMENS.TOLBOOM said:

var curve := Curve3D()
var direction_vector = (curve.get_closest_point( point_somewhere ) -  curve.get_point_position(any_point_number)).normalize()

See more info about Curve3d

Falcon | 2021-07-25 19:05

hi Falcon,

from where do I get your any_point_number value, and does this make sense?

Thanks

Mike

MikeMikeMike | 2021-07-25 19:54

hi clemens and Falcon,

I have changed my code from get_closest_point to get_closest_offset, which seams to work. Then with interpolate_baked I can calculate the direction like this:

var curve := Curve3D()
var offset = curve.get_closest_offset( point_somewhere )
var point_1 = curve.interpolate_baked( offset, true )
var point_2 = curve.interpolate_baked( offset + 0.01, true )
var direction : Vector3 = ( point_2 - point_1 ).normalized()

Thank you again

Mike

MikeMikeMike | 2021-07-25 20:13

:bust_in_silhouette: Reply From: MikeMikeMike

Hi again,

I have found a simpler solution. I use a Path with a PathFollow. The PathFollow can be set like this:

pathfollow.offset = path.curve.get_closest_offset( point_somewhere )
var basis = pathfollow.global_transform.basis
var y_direction: float = Vector2( basis.z.x,basis.z.z ).angle_to( Vector2( 0,-1 ) )

I use PathFollow with Rotation Mode = Y, and I can calculate the y_direction by the transform.basis.

Thanks

Mike