Handle corrections

This commit is contained in:
Chris Bell 2024-12-28 16:36:51 -06:00
parent a6ba12bd76
commit 69014169f2

View File

@ -18,6 +18,9 @@ var network_uuid: String = ""
var old_global_rotation_cache: Vector3
var old_global_position_cache: Vector3
var rotation_correction: Vector3
var position_correction: Vector3
@export_range(0.01, 1.0, 0.01) var base_turn_speed: float = 0.35
@export var max_turn_speed: float = 0.5
@export_range(0.01, 10.0, 0.01) var base_lift_speed: float = 2.0
@ -68,6 +71,15 @@ func _physics_process(_delta):
global_rotation.x = lerpf(global_rotation.x, 0, 0.1)
global_rotation.z = lerpf(global_rotation.z, 0, 0.1)
# Apply corrections if available
if position_correction != Vector3.ZERO:
_handle_position_correction(position_correction)
position_correction = Vector3.ZERO
if rotation_correction != Vector3.ZERO:
_handle_rotation_correction(rotation_correction)
rotation_correction = Vector3.ZERO
# Send network updates periodically
_send_ship_sync()
@ -182,11 +194,7 @@ func _handle_ship_sync_position(pos: Vector3):
# Correct prediction with server data
var correction_threshold = 0.5 # Adjust based on your desired tolerance
if (pos - global_position).length() > correction_threshold:
predicted_position = pos
linear_velocity = (pos - global_position) / delta_time
# Send correction update to the client
var correction = {"position": pos}
NetworkManager.sync_property_to_peer(network_uuid, correction, piloting_player.network_uuid)
position_correction = pos
func _handle_ship_sync_rotation(rot: Vector3):
if piloting_player and not piloting_player.is_network_authority:
@ -194,11 +202,22 @@ func _handle_ship_sync_rotation(rot: Vector3):
var received_rotation = Quaternion.from_euler(Vector3(rot.x, rot.y, rot.z))
var delta_rot = received_rotation * global_rotation.inverse()
if delta_rot.length() > correction_threshold:
predicted_rotation = received_rotation
angular_velocity = delta_rot.get_euler() / delta_time
# Send correction update to the client
var correction = {"rotation": rot}
NetworkManager.sync_property_to_peer(network_uuid, correction, piloting_player.network_uuid)
rotation_correction = rot
# Apply server rotation directly
global_rotation = Quaternion.from_euler(rot).get_euler()
func _handle_position_correction(corrected_position: Vector3):
if piloting_player and not piloting_player.is_network_authority:
predicted_position = corrected_position
linear_velocity = (corrected_position - global_position) / delta_time
func _handle_rotation_correction(corrected_rotation: Vector3):
if piloting_player and not piloting_player.is_network_authority:
var received_rotation = Quaternion.from_euler(corrected_rotation)
var delta_rot = received_rotation * global_rotation.inverse()
predicted_rotation = received_rotation
angular_velocity = delta_rot.get_euler() / delta_time
# Apply server rotation directly
global_rotation = Quaternion.from_euler(corrected_rotation).get_euler()