godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
91.38k stars 21.26k forks source link

Curve2D.get_closest_offset() doesn't work properly #99500

Closed TestAk123 closed 3 days ago

TestAk123 commented 4 days ago

Tested versions

System information

Godot Engine v4.3.stable.official.77dcf97d8 - https://godotengine.org Vulkan 1.3.217 - Forward Mobile - Using Device #0: AMD - Radeon RX 560 Series

Issue description

While I played around with a curve2d, I've noticed that getting closest offset via get_closest_offset() gives very difirent results than from grabing closest point via get_closest_point() and doing basic math. Image In this case difference is around 90%.

Steps to reproduce

  1. Create new 2d project
  2. Attach scipt to node2d
  3. Grab the code: extends Node2D func _ready(): var curve2d_from_fs_points = Curve2D.new() var first_point: PackedVector2Array = [Vector2(63,32), Vector2(0,0), Vector2(0,0)] var last_point: PackedVector2Array = [Vector2(242,139), Vector2(0,0), Vector2(0,0)] curve2d_from_fs_points.add_point(first_point[0],first_point[1],first_point[2],0) curve2d_from_fs_points.add_point(last_point[0],last_point[1],last_point[2],-1) curve2d_from_fs_points.bake_interval = 0.1 var point = Vector2(62.255, 49.224) var _sum_offset = curve2d_from_fs_points.get_closest_offset(point) var cp = curve2d_from_fs_points.get_closest_point(point) var _cl_of: float = sqrt((cp[0]-point[0])(cp[0]-point[0])+(cp[1]-point[1])(cp[1]-point[1])) print(point, cp, _sum_offset,' ', _cl_of)
  4. Paste in node2D in new project. And no it's not those values specific, you can play around with them.

Minimal reproduction project (MRP)

console.zip No idea if I done it right but here you go.

reach-satori commented 3 days ago

Offset isn't the distance from point to curve, it's the distance along the curve (i.e. offset) in pixels at which the closest point is located. This will give you what you want: curve2d_from_fs_points.sample_baked(_sum_offset)