Skip to content

Commit fc11e0e

Browse files
committed
Reduce erratic movement when changing num_segments
Closes #33
1 parent ad70c74 commit fc11e0e

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

demo/addons/ropesim/Rope.gd

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,16 @@ func _draw() -> void:
176176

177177

178178
# Logic
179-
180179
func _setup(run_reset: bool = true) -> void:
181180
if not is_inside_tree():
182181
return
183182

184-
_points.resize(num_segments + 1)
185-
_oldpoints.resize(num_segments + 1)
186-
_resize_with_default(_simulation_weights, num_segments + 1, 1.0)
183+
var num_points := num_segments + 1
184+
185+
# Initialize all new points with the endpoint's coordinates to reduce erratic movement.
186+
_resize_with_default(_points, num_points, _points[-1] if not _points.is_empty() else Vector2.ZERO)
187+
_resize_with_default(_oldpoints, num_points, _oldpoints[-1] if not _oldpoints.is_empty() else Vector2.ZERO)
188+
_resize_with_default(_simulation_weights, num_points, 1.0)
187189

188190
update_colors()
189191
update_segments()
@@ -444,9 +446,11 @@ func _set_seg_dist(value: Curve) -> void:
444446
update_segments()
445447

446448

447-
func _resize_with_default(arr: PackedFloat32Array, new_size: int, default: float) -> void:
448-
var oldsize := arr.size()
449-
arr.resize(new_size)
449+
static func _resize_with_default(array: Variant, new_size: int, default: Variant) -> void:
450+
@warning_ignore("unsafe_method_access")
451+
var oldsize: int = array.size()
452+
@warning_ignore("unsafe_method_access")
453+
array.resize(new_size)
450454

451455
for i in range(oldsize, new_size):
452-
arr[i] = default
456+
array[i] = default

0 commit comments

Comments
 (0)