Skip to content

Commit

Permalink
show/hide physics controls appropriately depending on project settings
Browse files Browse the repository at this point in the history
  • Loading branch information
lokimckay committed May 12, 2024
1 parent 80c3080 commit 424995c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
27 changes: 16 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,26 @@ It is _not_ a drop-in replacement for the Godot physics engine. Rapier nodes ope

## Quickstart

1. Download or clone [addons/godot-rapier-3d](addons/godot-rapier-3d/) to the same directory within your Godot project - [download-directory.github.io/](https://download-directory.github.io/) is great for this
1. (temporary! downloadable release binaries coming soon) - Install rust and run `cargo build` in the `addons/godot-rapier-3d/rust` directory
1. Add some RapierRigidBody3D nodes to your scene and add a RapierCollider3D node as a child of each
1. Call `Rapier3D.step()` from within a `_physics_process()` function, or as often as you like

```gdscript
func _physics_process(_delta):
Rapier3D.step()
```

1. Download the latest release
1. Extract the release archive into your godot project's root directory
1. If your Godot project is already open, you may get console errors. Don't panic, just reload your project
1. Add RapierRigidBody3D nodes to your scene and some RapierCollider3D + MeshInstance3D nodes as children of the rigid bodies
1. Run your game

Your physics objects should simulate! 🎉

Configure simulation and debug options by searching for `Rapier 3D` in your project settings
## Configuring

By default, the Rapier3DDebugger autoload will start the physics simulation for you. To get more control over when you simulate, search for `Rapier 3D` in your project settings.

Disable either the `Run in Game` or `Show UI` settings to prevent the debugger from simulating for you.

Now you can call `Rapier3D.step()` from within any `_physics_process()` function, or as often as you like. This function advances the physics simulation by 1 step.

```gdscript
func _physics_process(_delta):
Rapier3D.step()
```

## Saving and loading state

Expand Down
14 changes: 11 additions & 3 deletions addons/godot-rapier-3d/gdscript/physics_controls.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extends SubViewportContainer

var play = false
var should_show = false
var initial_snapshot
var snapshot
var snapshot_hash
Expand All @@ -10,6 +11,8 @@ var snapshot_hash
@onready var canvas = $SubViewport/CanvasLayer

func _ready():
_update_should_show()
if !should_show: return
Rapier3D.physics_ready.connect(_on_physics_ready)
if !Engine.is_editor_hint():
play_button.set_pressed(true)
Expand All @@ -19,6 +22,7 @@ func _on_physics_ready():
initial_snapshot = _save()

func _physics_process(_delta):
if !should_show: return
if play: Rapier3D.step()

func _save() -> PackedByteArray:
Expand All @@ -30,13 +34,17 @@ func _save() -> PackedByteArray:
func _load(snap):
Rapier3D.set_state(snap)

func _on_settings_changed():
var should_show = true
func _on_settings_changed(): _update_should_show()

func _update_should_show():
var run_in_game = ProjectSettings.get_setting("debug/rapier_3d/debug_in_game")
var run_in_editor = ProjectSettings.get_setting("debug/rapier_3d/debug_in_editor")
canvas.visible = ProjectSettings.get_setting("debug/rapier_3d/show_ui")
var show_ui = ProjectSettings.get_setting("debug/rapier_3d/show_ui")
if Engine.is_editor_hint() and !run_in_editor: should_show = false
elif !Engine.is_editor_hint() and !run_in_game: should_show = false
elif !show_ui: should_show = false
else: should_show = true
canvas.visible = should_show

func _on_reset_pressed(): _load(initial_snapshot)
func _on_step_button_pressed(): Rapier3D.step()
Expand Down

0 comments on commit 424995c

Please sign in to comment.