fix comprenetration train-wagons

This commit is contained in:
2026-07-07 09:42:23 +02:00
parent 4aaf8e3fd3
commit b87be91521
3 changed files with 94 additions and 7 deletions

View File

@@ -3,6 +3,12 @@ extends Path3D
const STEAM_DISTANCE_STAT: String = "stat_distance_km"
const DISTANCE_KM_PER_UNIT: float = 0.01
const STATION_STOP_GROUP: StringName = &"railway_station"
const TRACK_ZERO_SPACING_FIX_DISTANCE: float = 45.0
const TRACK_ZERO_SPACING_BLEND_DISTANCE: float = 18.0
const TRACK_ZERO_SPACING_SEARCH_EXTRA: float = 35.0
const TRACK_ZERO_SPACING_SEARCH_STEP: float = 0.5
const TRACK_ZERO_SPACING_TARGET_RATIO: float = 0.97
const TRACK_ZERO_SPACING_MIN_DISTANCE: float = 1.0
enum TrainStartMode { RANDOM_POSITION, SPECIFIED_STATION, SPECIFIED_POSITION }
@@ -420,7 +426,10 @@ func train_move(delta: float) -> void:
stop_multiply = 1.0
var current_speed = train_speed * stop_multiply
train_progress += current_speed * delta
var next_progress: float = train_progress + current_speed * delta
var wrapped_forward: bool = current_speed > 0.0 and next_progress >= total_length
var wrapped_back: bool = current_speed < 0.0 and next_progress < 0.0
train_progress = wrapf(next_progress, 0.0, total_length)
_track_steam_distance(abs(current_speed) * delta)
if enable_stops and stop_offset.size() > 0 and has_valid_stop:
@@ -432,11 +441,9 @@ func train_move(delta: float) -> void:
train_progress = target_offset
_execute_stop(current_speed > 0)
if train_progress > total_length:
train_progress -= total_length
if wrapped_forward:
if stop_offset.size() > 0: next_stop_index = 0
elif train_progress < 0:
train_progress += total_length
elif wrapped_back:
if stop_offset.size() > 0: next_stop_index = stop_offset.size() - 1
var center_position = to_global(curve.sample_baked(train_progress, true))
@@ -659,6 +666,10 @@ func _snap_wagons_to_progress(total_length: float = 0.0) -> void:
if total_length <= 0.0:
return
var previous_progress: float = wrapf(train_progress, 0.0, total_length)
var previous_position: Vector3 = to_global(curve.sample_baked(previous_progress, true))
var previous_wagon_offset: float = 0.0
for i in range(wagon_instances.size()):
var wagon: Node3D = wagon_instances[i]
if not is_instance_valid(wagon):
@@ -669,9 +680,17 @@ func _snap_wagons_to_progress(total_length: float = 0.0) -> void:
wagon_offset = wagon_progress_offsets[i]
var wagon_progress: float = train_progress - wagon_offset
var segment_spacing: float = maxf(wagon_offset - previous_wagon_offset, 0.1)
var vehicle_progress: float = wrapf(wagon_progress, 0.0, total_length)
var center_position: Vector3 = to_global(curve.sample_baked(vehicle_progress, true))
var zero_spacing_fix_weight: float = _get_zero_spacing_fix_weight(previous_progress, vehicle_progress, total_length)
if zero_spacing_fix_weight > 0.0:
var target_distance: float = _get_zero_spacing_target_distance(segment_spacing)
var corrected_progress: float = _find_zero_spacing_progress(previous_progress, previous_position, segment_spacing, target_distance, total_length)
vehicle_progress = _lerp_progress_on_track(vehicle_progress, corrected_progress, zero_spacing_fix_weight, total_length)
center_position = to_global(curve.sample_baked(vehicle_progress, true))
var prog_forward: float = wrapf(vehicle_progress + 2.0, 0.0, total_length)
var forward_position: Vector3 = to_global(curve.sample_baked(prog_forward, true))
@@ -680,6 +699,73 @@ func _snap_wagons_to_progress(total_length: float = 0.0) -> void:
wagon.look_at(forward_position, Vector3.UP)
wagon.rotate_y(PI)
previous_progress = vehicle_progress
previous_position = center_position
previous_wagon_offset = wagon_offset
func _get_zero_spacing_fix_weight(previous_progress: float, vehicle_progress: float, total_length: float) -> float:
var distance_to_zero: float = minf(
_get_progress_distance_to_zero(previous_progress, total_length),
_get_progress_distance_to_zero(vehicle_progress, total_length)
)
var full_fix_distance: float = maxf(TRACK_ZERO_SPACING_FIX_DISTANCE - TRACK_ZERO_SPACING_BLEND_DISTANCE, 0.0)
return 1.0 - smoothstep(full_fix_distance, TRACK_ZERO_SPACING_FIX_DISTANCE, distance_to_zero)
func _get_progress_distance_to_zero(progress: float, total_length: float) -> float:
var wrapped_progress: float = wrapf(progress, 0.0, total_length)
return minf(wrapped_progress, total_length - wrapped_progress)
func _get_zero_spacing_target_distance(segment_spacing: float) -> float:
return minf(maxf(segment_spacing * TRACK_ZERO_SPACING_TARGET_RATIO, TRACK_ZERO_SPACING_MIN_DISTANCE), segment_spacing)
func _find_zero_spacing_progress(previous_progress: float, previous_position: Vector3, segment_spacing: float, minimum_distance: float, total_length: float) -> float:
var start_distance: float = minf(minimum_distance, segment_spacing)
var best_progress: float = wrapf(previous_progress - start_distance, 0.0, total_length)
var search_limit: float = minf(maxf(segment_spacing, minimum_distance) + TRACK_ZERO_SPACING_SEARCH_EXTRA, total_length)
var search_distance: float = start_distance
var previous_search_distance: float = start_distance
while search_distance <= search_limit:
var candidate_progress: float = wrapf(previous_progress - search_distance, 0.0, total_length)
var candidate_position: Vector3 = to_global(curve.sample_baked(candidate_progress, true))
best_progress = candidate_progress
if previous_position.distance_to(candidate_position) >= minimum_distance:
if is_equal_approx(search_distance, start_distance):
return candidate_progress
return _refine_zero_spacing_progress(
previous_progress,
previous_position,
previous_search_distance,
search_distance,
minimum_distance,
total_length
)
previous_search_distance = search_distance
search_distance += TRACK_ZERO_SPACING_SEARCH_STEP
return best_progress
func _refine_zero_spacing_progress(previous_progress: float, previous_position: Vector3, min_search_distance: float, max_search_distance: float, minimum_distance: float, total_length: float) -> float:
var low_distance: float = min_search_distance
var high_distance: float = max_search_distance
for i in range(8):
var mid_distance: float = (low_distance + high_distance) * 0.5
var mid_progress: float = wrapf(previous_progress - mid_distance, 0.0, total_length)
var mid_position: Vector3 = to_global(curve.sample_baked(mid_progress, true))
if previous_position.distance_to(mid_position) >= minimum_distance:
high_distance = mid_distance
else:
low_distance = mid_distance
return wrapf(previous_progress - high_distance, 0.0, total_length)
func _lerp_progress_on_track(from_progress: float, to_progress: float, weight: float, total_length: float) -> float:
var wrapped_delta: float = wrapf(to_progress - from_progress + total_length * 0.5, 0.0, total_length) - total_length * 0.5
return wrapf(from_progress + wrapped_delta * clampf(weight, 0.0, 1.0), 0.0, total_length)
func _get_vehicle_length(vehicle: Node3D) -> float:
var bounds: AABB = _get_node_local_bounds(vehicle, vehicle)
if bounds.size == Vector3.ZERO: