Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions addons/netfox.extras/physics/rapier_driver_2d.gd.off
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@

extends PhysicsDriver

class_name RapierDriver2D

var _state: StateManager2D

var _stored_states: int = 0


func _init_physics_space() -> void:
physics_space = get_viewport().world_2d.space
PhysicsServer2D.space_set_active(physics_space, false)

_state = StateManager2D.new()
_state.root_node = self
_state.set_max_cache_length(ProjectSettings.get_setting("netfox/rollback/history_limit", 64))
_state.set_rolling_cache(true)
add_child(_state)


func _physics_step(delta) -> void:
RapierPhysicsServer2D.space_step(physics_space, delta)
RapierPhysicsServer2D.space_flush_queries(physics_space)



func _snapshot_space(tick: int) -> void:
snapshots[tick] = RapierPhysicsServer2D.export_binary(physics_space)
_state.cache_state(physics_space, tick)


func _rollback_space(tick: int) -> void:
# With rolling cache, tick states are ordered by age with the newest at 0
var offset = NetworkTime.tick - tick
if (offset >= _stored_states):
return

func _rollback_space(tick) -> void:
if snapshots.has(tick):
RapierPhysicsServer2D.import_binary(physics_space, snapshots[tick])
_stored_states = min(_stored_states + 1, _state.max_cache_length)
_state.load_cached_state(physics_space, offset)
31 changes: 25 additions & 6 deletions addons/netfox.extras/physics/rapier_driver_3d.gd.off
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,36 @@ extends PhysicsDriver

class_name RapierDriver3D

var _state: StateManager3D

var _stored_states: int = 0


func _init_physics_space() -> void:
physics_space = get_viewport().world_3d.space
physics_space = get_viewport().world_2d.space
PhysicsServer3D.space_set_active(physics_space, false)

_state = StateManager3D.new()
_state.root_node = self
_state.set_max_cache_length(ProjectSettings.get_setting("netfox/rollback/history_limit", 64))
_state.set_rolling_cache(true)
add_child(_state)


func _physics_step(delta) -> void:
RapierPhysicsServer3D.space_step(physics_space, delta)
RapierPhysicsServer3D.space_flush_queries(physics_space)



func _snapshot_space(tick: int) -> void:
snapshots[tick] = RapierPhysicsServer3D.export_binary(physics_space)
_state.cache_state(physics_space, tick)


func _rollback_space(tick: int) -> void:
# With rolling cache, tick states are ordered by age with the newest at 0
var offset = NetworkTime.tick - tick
if (offset >= _stored_states):
return

func _rollback_space(tick) -> void:
if snapshots.has(tick):
RapierPhysicsServer3D.import_binary(physics_space, snapshots[tick])
_stored_states = min(_stored_states + 1, _state.max_cache_length)
_state.load_cached_state(physics_space, offset)