diff options
author | dusoleil <howcansocksbereal@gmail.com> | 2022-09-12 20:45:03 -0400 |
---|---|---|
committer | dusoleil <howcansocksbereal@gmail.com> | 2022-09-12 20:45:03 -0400 |
commit | 99a70a8677f8d14749bdcd374f1631cd9cbbd3df (patch) | |
tree | 9d3cc5334ceb4e698009449c070bec3b4850629e | |
parent | 5dbc9c05c1ebd33e37315c096d2c9e1c6b96a8f2 (diff) | |
download | godot_wildjam_49-99a70a8677f8d14749bdcd374f1631cd9cbbd3df.tar.gz godot_wildjam_49-99a70a8677f8d14749bdcd374f1631cd9cbbd3df.zip |
Add buoyancy simulation to water and floaties on the raft
Diffstat (limited to '')
-rw-r--r-- | Float.gd | 17 | ||||
-rw-r--r-- | Float.tscn | 18 | ||||
-rw-r--r-- | Main.tscn | 24 | ||||
-rw-r--r-- | Water.gd | 32 |
4 files changed, 87 insertions, 4 deletions
diff --git a/Float.gd b/Float.gd new file mode 100644 index 0000000..7855bea --- /dev/null +++ b/Float.gd @@ -0,0 +1,17 @@ +extends Spatial + +var linear_drag = 0.99 +var angular_drag = 0.5 + +func _physics_process(_delta): + var parent = get_parent() + var water = get_node("/root/Main/Water") + #self.global_transform.origin.y = water.height(self.global_transform.origin) + var height = self.global_transform.origin.y + var wave_height = water.height(self.global_transform.origin) + if height < wave_height: + var pos = self.global_transform.origin-parent.global_transform.origin + var buoyancy = clamp(wave_height-height,0.0,1.0) + parent.add_force(Vector3.UP*9.8*buoyancy, pos) + parent.add_central_force(buoyancy * -parent.linear_velocity * linear_drag) + parent.add_torque(buoyancy * -parent.angular_velocity * angular_drag) diff --git a/Float.tscn b/Float.tscn new file mode 100644 index 0000000..6d1ba7e --- /dev/null +++ b/Float.tscn @@ -0,0 +1,18 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://Float.gd" type="Script" id=1] + +[sub_resource type="SpatialMaterial" id=1] +albedo_color = Color( 0.823529, 0.796078, 0.0588235, 1 ) + +[sub_resource type="SphereMesh" id=2] +material = SubResource( 1 ) +radius = 0.5 +height = 1.0 + +[node name="Float" type="Spatial"] +script = ExtResource( 1 ) + +[node name="MeshInstance" type="MeshInstance" parent="."] +visible = false +mesh = SubResource( 2 ) @@ -1,7 +1,8 @@ -[gd_scene load_steps=18 format=2] +[gd_scene load_steps=19 format=2] [ext_resource path="res://character/fps_controller/fps_controller.tscn" type="PackedScene" id=1] [ext_resource path="res://icon.png" type="Texture" id=2] +[ext_resource path="res://Float.tscn" type="PackedScene" id=4] [ext_resource path="res://Water.gd" type="Script" id=5] [ext_resource path="res://Wave.gdshader" type="Shader" id=7] @@ -29,11 +30,11 @@ noise = SubResource( 17 ) [sub_resource type="ShaderMaterial" id=11] shader = ExtResource( 7 ) -shader_param/wave1 = Quat( 0.25, 0.10472, 2.38732, 1080.08 ) +shader_param/wave1 = Quat( 0.25, 0.10472, 2.38732, 1200.04 ) shader_param/wave1_dir = Vector2( 1, 1 ) -shader_param/wave2 = Quat( 0.25, 0.202683, 1.23345, 776.359 ) +shader_param/wave2 = Quat( 0.25, 0.202683, 1.23345, 862.583 ) shader_param/wave2_dir = Vector2( 1, 0.6 ) -shader_param/wave3 = Quat( 0.25, 0.349066, 0.716197, 591.586 ) +shader_param/wave3 = Quat( 0.25, 0.349066, 0.716197, 657.289 ) shader_param/wave3_dir = Vector2( 1, 1.3 ) shader_param/water_colour = Color( 0.694118, 0.945098, 0.941176, 0.368627 ) shader_param/deep_water_colour = Color( 0.25098, 0.623529, 0.788235, 0.54902 ) @@ -69,6 +70,9 @@ transform_array = PoolVector3Array( 0.35759, 0, -0.933879, 0, 1, 0, 0.933879, 0, transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.074, 0 ) [node name="Raft" type="RigidBody" parent="."] +mass = 2.0 +linear_damp = 0.8 +angular_damp = 2.0 [node name="CollisionShape" type="CollisionShape" parent="Raft"] shape = SubResource( 1 ) @@ -77,6 +81,18 @@ shape = SubResource( 1 ) mesh = SubResource( 2 ) material/0 = SubResource( 3 ) +[node name="Float" parent="Raft" instance=ExtResource( 4 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 4.98274, 0, -5.0045 ) + +[node name="Float2" parent="Raft" instance=ExtResource( 4 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 4.98274, 0, 5.02626 ) + +[node name="Float3" parent="Raft" instance=ExtResource( 4 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -5.0045, 0, -5.0045 ) + +[node name="Float4" parent="Raft" instance=ExtResource( 4 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -5.0045, 0, 5.02626 ) + [node name="DirectionalLight" type="DirectionalLight" parent="."] transform = Transform( 0.863334, 0.0994421, -0.494738, 0, 0.980392, 0.197058, 0.504633, -0.170127, 0.846406, -12.2908, 5.96757, 0 ) @@ -78,4 +78,36 @@ func _set_time(val): func _physics_process(delta): self._time += delta + #self._time = 0.0 +func gertsner_wave(pos, idx): + if idx >= waves.size(): return 0.0 + var wave = waves[idx] + var k = wave._k + var a = wave._a + var t = wave._t + + var d = wave.direction.normalized() + var f = k * (d.dot(Vector2(pos.x,pos.z)) - t) + + var tmp = a * cos(f) + var new_pos = Vector3(d.x * tmp, a * sin(f), d.y * tmp) + + return new_pos + +func sample_wave(pos): + var new_pos = Vector3(pos.x,0,pos.z) + for idx in waves.size(): + new_pos += gertsner_wave(pos, idx) + return new_pos + +func height(pos): + var old_pos = pos + var y = old_pos.y + for _i in range(2): + var new_pos = sample_wave(old_pos) + y = new_pos.y + new_pos = old_pos - (new_pos - old_pos) + old_pos = new_pos + y += self.global_transform.origin.y + return y |