aboutsummaryrefslogtreecommitdiff
path: root/game/testing/Buoyancy-in-Godot-4-master/Cube.gd
diff options
context:
space:
mode:
Diffstat (limited to 'game/testing/Buoyancy-in-Godot-4-master/Cube.gd')
-rw-r--r--game/testing/Buoyancy-in-Godot-4-master/Cube.gd34
1 files changed, 34 insertions, 0 deletions
diff --git a/game/testing/Buoyancy-in-Godot-4-master/Cube.gd b/game/testing/Buoyancy-in-Godot-4-master/Cube.gd
new file mode 100644
index 0000000..075e532
--- /dev/null
+++ b/game/testing/Buoyancy-in-Godot-4-master/Cube.gd
@@ -0,0 +1,34 @@
+extends RigidBody3D
+
+@export var float_force := 1.0
+@export var water_drag := 0.05
+@export var water_angular_drag := 0.05
+
+@onready var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
+@onready var water = get_node('/root/Main/Water')
+
+@onready var probes = $ProbeContainer.get_children()
+
+var submerged := false
+
+# Called when the node enters the scene tree for the first time.
+func _ready():
+ pass # Replace with function body.
+
+
+# Called every frame. 'delta' is the elapsed time since the previous frame.
+func _process(delta):
+ pass
+
+func _physics_process(delta):
+ submerged = false
+ for p in probes:
+ var depth = water.get_height(p.global_position) - p.global_position.y
+ if depth > 0:
+ submerged = true
+ apply_force(Vector3.UP * float_force * gravity * depth, p.global_position - global_position)
+
+func _integrate_forces(state: PhysicsDirectBodyState3D):
+ if submerged:
+ state.linear_velocity *= 1 - water_drag
+ state.angular_velocity *= 1 - water_angular_drag